标签:
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7254 Accepted Submission(s): 2307
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<string> #include<iostream> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<vector> #include<map> #include<stdlib.h> #include<algorithm> #define LL __int64 using namespace std; const int MAXN=1e6+1e5+5; int p[MAXN]; int vis[MAXN]; int vir[MAXN]; //存储虚拟点的编号 int n,m,cnt; int init() { for(int i=0;i<=MAXN;i++) p[i]=i; for(int i=0;i<n;i++) vir[i]=i; memset(vis,0,sizeof(vis)); cnt=n; } int findfa(int x) { return p[x]==x?x:p[x]=findfa(p[x]); } void Union(int u,int v) { int x=findfa(u); int y=findfa(v); if(x!=y) p[x]=y; } void del(int u) { vir[u]=cnt++; } int main() { freopen("in.txt","r",stdin); int Case=0; while(scanf("%d %d",&n,&m) && (n||m)) { init(); while(m--) { char opt[10]; scanf("%s",opt); if(opt[0]==‘M‘) { int u,v; scanf("%d %d",&u,&v); Union(vir[u],vir[v]); } else { int u; scanf("%d",&u); del(u); } } for(int i=0;i<n;i++) vis[findfa(vir[i])]=1; int ans=0; for(int i=0;i<cnt;i++) if(vis[i]) ans++; printf("Case #%d: %d\n",++Case,ans); } return 0; }
HDU 2473 Junk-Mail Filter (并查集节点删除)
标签:
原文地址:http://www.cnblogs.com/clliff/p/4694255.html