标签:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 4332 | Accepted: 1419 |
Description
Input
Output
Sample Input
3 3 0 1 0 2 2 1 4 2 0 1 2 3 3 1 1 0 0 0
Sample Output
1 2 2
Source
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<vector> 7 #include<map> 8 using namespace std; 9 const int maxn=11007; 10 vector<int> G[maxn]; 11 int pre[maxn],ans,root,sum,n,m,cnt[maxn]; 12 int tarjan(int u,int fa) 13 { 14 int lowu=pre[u]=sum++; 15 int l=G[u].size(); 16 int child=0; 17 for(int i=0;i<l;i++) 18 { 19 int v=G[u][i]; 20 if(v==fa) continue; 21 if(!pre[v]){ 22 child++; 23 int lowv=tarjan(v,u); 24 lowu=min(lowv,lowu); 25 if((u==root&&child>1)||(u!=root&&pre[u]<=lowv)){ 26 cnt[u]++; 27 } 28 } 29 else lowu=min(lowu,pre[v]); 30 } 31 return lowu; 32 } 33 int main() 34 { 35 int a,b; 36 while(1){ 37 for(int i=0;i<maxn;i++) G[i].clear(); 38 scanf("%d%d",&n,&m); 39 if(n==0) break; 40 if(m==0){printf("%d\n",n-1);continue;} 41 for(int i=0;i<m;i++) 42 { 43 scanf("%d%d",&a,&b); 44 G[a].push_back(b); 45 G[b].push_back(a); 46 } 47 int t=0;ans=0; 48 memset(pre,0,sizeof(pre)); 49 memset(cnt,0,sizeof(cnt)); 50 for(int i=0;i<n;i++) 51 { 52 if(!pre[i]){ 53 t++;root=i; 54 sum=1; 55 tarjan(i,-1); 56 } 57 ans=max(ans,cnt[i]); 58 } 59 printf("%d\n",t+ans); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/codeyuan/p/4385728.html