Junk-Mail Filter
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9364 Accepted Submission(s): 2961
Problem Description
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.
Input
There are multiple test cases in the input file.
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.
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.
Output
For each test case, please print a single integer, the number of distinct common characteristics, to the console. Follow the format as indicated in the sample below.
Sample Input
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
Sample Output
Case #1: 3 Case #2: 2题目大意:给N个元素,m个指令,M表示两个数在一个集合,s表示将这个数从集合中删除,但不改变之前建立集合中的其他元素思路:一看到题目应该会想到并查集,但并查集里没有删除元素的操作。开始想着建立一个假根,把所有元素连到假根上,但代码不精,居然t了。后参考别人的博客看到新思路,可以为每个元素建立自己的id,删除元素时并不真的删除,而是为此元素建立一个新的id,这样等于新建了一个和所删元素相等的元素来代替它,后续操作在新元素上进行,相当于把它删除了,再记录一下每个集合的个数,如果删除就--,最后为0的集合没有元素(虽然它实际上是有元素的,但已经都被删除了)。感觉很神奇,第一次做并查集删除元素的题#include<iostream> #include<cstdio> #include<cstring> #define manx 1000005 using namespace std; int f[manx],num[manx],id[manx]; int n,m; void init() { memset(num,0,sizeof(num)); //每个集合的元素个数 memset(id,0,sizeof(id)); //每个元素的代表id for(int i=0; i<n; i++){ f[i]=i; id[i]=i; num[i]=1; } } int Find(int x) { if (f[x] == x) return x; else return f[x]=Find(f[x]); } void unit(int x,int y) { int fx=Find(x); int fy=Find(y); if(fx==fy) return; else { f[fx]=fy; num[fy]+=num[fx]; num[fx]=0; } } void delet(int x) { int fx=Find(id[x]); num[fx]--; id[x]=n; //建立新的代替元素 f[id[x]]=n; num[n++]++; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif char c; int t=1,x,y; while(~scanf("%d%d",&n,&m) && (n+m)){ init(); n*=2; for (int i=0; i<m; i++){ getchar(); scanf("%c",&c); if(c=='M'){ scanf("%d%d",&x,&y); unit(id[x],id[y]); } else if(c=='S'){ scanf("%d",&x); delet(x); //注意是x } } int ans=0; for (int i=0; i<=n; i++) if(num[i]) ans++; printf("Case #%d: %d\n",t++,ans); } return 0; }