TWO NODES
Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2354 Accepted Submission(s): 780
Problem Description
Suppose that G is an undirected graph, and the value of stab is defined as follows:
Among the expression,G-i, -j is the remainder after removing node i, node j and all edges that are directly relevant to the previous two nodes. cntCompent is the number of connected components of X independently.
Thus, given a certain undirected graph G, you are supposed to calculating the value of stab.
Input
The
input will contain the description of several graphs. For each graph,
the description consist of an integer N for the number of nodes, an
integer M for the number of edges, and M pairs of integers for edges
(3<=N,M<=5000).
Please note that the endpoints of edge is marked in the range of [0,N-1], and input cases ends with EOF.
Output
For each graph in the input, you should output the value of stab.
Sample Input
4 5
0 1
1 2
2 3
3 0
0 2
Sample Output
2
Source
题意:在一个无向图中,删除两个点,使的连通分量最大 输出最大的联通分量
枚举一个点 然后在剩下的n个点中选取一个点做顶点 然后利用割顶的性质判断这n-1个点的可以拆成多少个联通分量
其实就是在判断割顶的地方iscut[u]++;
但我们需要注意的是当选取的点是根节点时:incut[u]=0,因为他是没有父亲的,意思就是上面没有联通分量了
所以不是根节点的初始值就是iscut[u]=1,因为他的父亲那块是一个联通分量
由于删点会导致这个图不联通,所以需要DFS,看有几个联通块sum
最后的答案就是 max(sum+iscut[i]-1)
这个减1是因为将一个联通块拆成iscut[i]个 那么本身的联通块就没有了
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdlib> 6 #include<string.h> 7 #include<set> 8 #include<vector> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<cmath> 13 typedef long long ll; 14 typedef unsigned long long LL; 15 using namespace std; 16 const double PI=acos(-1.0); 17 const double eps=0.0000000001; 18 const int INF=1e9; 19 const int N=10000+100; 20 int head[N]; 21 int t,tot; 22 struct node{ 23 int to,next; 24 }edge[N<<1]; 25 int low[N]; 26 int dfn[N]; 27 int iscut[N]; 28 void init(){ 29 memset(low,0,sizeof(low)); 30 memset(dfn,0,sizeof(dfn)); 31 t=0; 32 } 33 void add(int u,int v){ 34 edge[tot].to=v; 35 edge[tot].next=head[u]; 36 head[u]=tot++; 37 } 38 int DFS(int u,int fa,int flag){ 39 low[u]=dfn[u]=++t; 40 int child=0; 41 for(int i=head[u];i!=-1;i=edge[i].next){ 42 int v=edge[i].to; 43 if(v==flag)continue; 44 if(dfn[v]==0){ 45 child++; 46 int lowv=DFS(v,u,flag); 47 low[u]=min(low[u],lowv); 48 if(lowv>=dfn[u]){ 49 iscut[u]++; 50 } 51 } 52 else if(dfn[v]<dfn[u]&&v!=fa){ 53 low[u]=min(low[u],dfn[v]); 54 } 55 } 56 if(fa<0&&child==1)low[u]=0; 57 return low[u]; 58 } 59 int main(){ 60 int n,m; 61 while(scanf("%d%d",&n,&m)!=EOF){ 62 memset(head,-1,sizeof(head)); 63 tot=0; 64 for(int i=1;i<=m;i++){ 65 int u,v; 66 scanf("%d%d",&u,&v); 67 add(u,v); 68 add(v,u); 69 } 70 int ans=0; 71 for(int i=0;i<n;i++){ 72 int sum=0; 73 init(); 74 for(int j=0;j<n;j++){ 75 iscut[j]=1; 76 } 77 iscut[i]=0; 78 for(int j=0;j<n;j++){ 79 if(i==j)continue; 80 if(dfn[j])continue; 81 iscut[j]=0; 82 sum++; 83 DFS(j,-1,i); 84 85 } 86 for(int j=0;j<n;j++){ 87 ans=max(ans,iscut[j]+sum-1); 88 } 89 } 90 cout<<ans<<endl; 91 } 92 }