标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3838 Accepted Submission(s): 1379
#include <iostream> #include <cstdio> using namespace std; const int MAX=1e4+10; const int MAX1=1e5+10; int f[MAX]; int sum[MAX1]; struct ss { int a,b; }s[MAX1]; int find(int n) { if(n!=f[n]) f[n]=find(f[n]); return f[n]; } int main() { int n,m; while(cin>>n>>m) { for(int i=0;i<n;i++) f[i]=i; for(int i=0;i<m;i++) scanf("%d%d",&s[i].a,&s[i].b); sum[m]=n; for(int i=m-1;i>=0;i--) { int a1=find(s[i].a); int a2=find(s[i].b); if(a1!=a2) {f[a1]=a2;sum[i]=sum[i+1]-1;} else sum[i]=sum[i+1]; } for(int i=1;i<=m;i++) cout<<sum[i]<<endl; } }
#include <iostream> #include <cstdio> using namespace std; const int MAX=1e4+10; const int MAX1=1e5+10; int f[MAX]; int sum[MAX1]; struct ss { int a,b; }s[MAX1]; int find(int n) { int res=n; while(n!=f[n]) n=f[n]; int j; while(res!=n) { j=f[res]; f[res]=n; res=j; } return n; } int main() { int n,m; while(cin>>n>>m) { for(int i=0;i<n;i++) f[i]=i; for(int i=0;i<m;i++) scanf("%d%d",&s[i].a,&s[i].b); sum[m]=n; for(int i=m-1;i>=0;i--) { int a1=find(s[i].a); int a2=find(s[i].b); if(a1!=a2) {f[a1]=a2;find(a1);sum[i]=sum[i+1]-1;} else sum[i]=sum[i+1]; } for(int i=1;i<=m;i++) cout<<sum[i]<<endl; } }
标签:
原文地址:http://www.cnblogs.com/llsq/p/5881009.html