HDU 2473——junk mails
Recognizing junk mails is a tough task. The method used here consists of two steps:
1) Extract the common characteristics from the incoming email.
2) Use a filter matching the set of common characteristics extracted to determine whether the email is a spam.
We want to extract the set of common characteristics from the N sample junk emails available at the moment, and thus having a handy data-analyzing tool would be helpful. The tool should support the following kinds of operations:
a) “M X Y”, meaning that we think that the characteristics of spam X and Y are the same. Note that the relationship defined here is transitive, so
relationships (other than the one between X and Y) need to be created if they are not present at the moment.
b) “S X”, meaning that we think spam X had been misidentified. Your tool should remove all relationships that spam X has when this command is received; after that, spam X will become an isolated node in the relationship graph.
Initially no relationships exist between any pair of the junk emails, so the number of distinct characteristics at that time is N.
Please help us keep track of any necessary information to solve our problem.
1) Extract the common characteristics from the incoming email.
2) Use a filter matching the set of common characteristics extracted to determine whether the email is a spam.
We want to extract the set of common characteristics from the N sample junk emails available at the moment, and thus having a handy data-analyzing tool would be helpful. The tool should support the following kinds of operations:
a) “M X Y”, meaning that we think that the characteristics of spam X and Y are the same. Note that the relationship defined here is transitive, so
relationships (other than the one between X and Y) need to be created if they are not present at the moment.
b) “S X”, meaning that we think spam X had been misidentified. Your tool should remove all relationships that spam X has when this command is received; after that, spam X will become an isolated node in the relationship graph.
Initially no relationships exist between any pair of the junk emails, so the number of distinct characteristics at that time is N.
Please help us keep track of any necessary information to solve our problem.
Each test case starts with two integers, N and M (1 ≤ N ≤ 10 5 , 1 ≤ M ≤ 10 6), the number of email samples and the number of operations. M lines follow, each line is one of the two formats described above.
Two successive test cases are separated by a blank line. A case with N = 0 and M = 0 indicates the end of the input file, and should not be processed by your program.
5 6 M 0 1 M 1 2 M 1 3 S 1 M 1 2 S 3 3 1 M 1 2 0 0
Case #1: 3 Case #2: 2
此题删除节点就是用题目用不到的点代替要删除的节点,re[x]=newnode,用此映射,最后统计根结点的个数。
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 1110000 using namespace std; int fa[maxn],re[maxn]; int R[maxn]; int flag[maxn]; int id; void init(int n) { for(int i=0;i<n;i++){ fa[i]=i; re[i]=i; R[i]=1; } } int find(int x) { if(fa[x]==x) return x; else return fa[x]=find(fa[x]); } void unit(int x,int y) { x=find(x); y=find(y); if(x==y) return ; if(R[x]<R[y]) fa[x]=y; else{ fa[y]=x; if(R[x]==R[y]) R[x]++; } } void de(int x) { re[x]=id; fa[id]=id; id++; } int main() { int N,M; int k=1; while(scanf("%d %d",&N,&M),N||M){ memset(flag,0,sizeof(flag)); getchar(); init(N); id=N; int a,b; char s; for(int i=0;i<M;i++){ scanf("%c",&s); if(s=='M'){ scanf("%d %d",&a,&b); unit(re[a],re[b]); } if(s=='S'){ scanf("%d",&a); de(a); } getchar(); } int ans=0,h; for(int i=0;i<N;i++){ h=find(re[i]); if(flag[h]==0){ ans++; flag[h]=1; } } printf("Case #%d: %d\n",k,ans); k++; } return 0; }