标签:
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7515 Accepted Submission(s):
2368
#include<stdio.h> #include<string.h> #define MAX 1001000 int set[MAX],vis[MAX]; int mark[MAX]; int n,m,count; void init() { int i,j; for(i=0;i<n;i++) { set[i]=i; vis[i]=i; } } int find(int fa) { int t; int ch=fa; while(fa!=set[fa]) fa=set[fa]; while(ch!=fa) { t=set[ch]; set[ch]=fa; ch=t; } return fa; } void mix(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fx!=fy) set[fx]=fy; } void getmap() { int i,j,k=n; char a[2]; memset(a,‘\0‘,sizeof(a)); for(i=0;i<m;i++) { scanf("%s",a); if(a[0]==‘M‘) { int b,c; scanf("%d%d",&b,&c); mix(vis[b],vis[c]); } else { int b; scanf("%d",&b); vis[b]=k; set[k]=k; k++; } } } void solve() { int i,ans=0; memset(mark,0,sizeof(mark)); for(i=0;i<n;i++) { int stem=find(vis[i]); if(!mark[stem]) { ans++; mark[stem]=1; } } printf("Case #%d: %d\n",count++,ans); } int main() { count=1; while(scanf("%d%d",&n,&m),n|m) { init(); getmap(); solve(); } return 0; }
hdoj 2473 Junk-Mail Filter【并查集节点的删除】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4733216.html