Junk-Mail Filter
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9135 Accepted Submission(s): 2885
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 ≤ 105 , 1 ≤ M ≤ 106),
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, please print a single integer, the number of distinct
common characteristics, to the console. Follow the format as indicated
in the sample below.
//并查集删点,可以将每一个点映射到对应的一个虚拟点上,在这个虚拟点上操作,如果删除某一点就把这个点指向另一个点如:1,2,3
//在一个并查集中时他们的根节点是1,但是当删去1时被分成了3个集合实际上是2个,我们把他们分别映射到4,5,6 即1-4,2-5,3-6 这样
//根节点就是4,删去1时就把1映射到7这样以4为根节点的这个集合中就只有两个点了.最后统计时找有几个根节点就行。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int fat[]; //数组开大一些
int num[];
int find(int x)
{
int rt=x;
while(fat[rt]!=rt)
rt=fat[rt];
int i=x,j;
while(i!=rt)
{
j=fat[i];
fat[i]=rt;
i=j;
}
return rt;
}
void connect(int x,int y)
{
int a=find(x),b=find(y);
if(a!=b)
{
fat[b]=a;
}
}
int main()
{
char ch[];
int a,b,k=;
while(scanf("%d%d",&n,&m)&&(n+m))
{
for(int i=;i<=n;i++) //映射到虚拟点
fat[i]=i+n;
for(int i=n+;i<=*n+m;i++)
fat[i]=i;
int tem=*n+;
for(int i=;i<=m;i++)
{
scanf("%s",ch);
if(ch[]=='M')
{
scanf("%d%d",&a,&b);
connect(a+,b+);
}
else
{
scanf("%d",&a);
fat[a+]=tem++; //指向另一个点
}
}
int ans=;
memset(num,,sizeof(num));
for(int i=;i<=n;i++) //统计
{
int nu=find(i);
if(num[nu]==)
{
num[nu]=;
ans++;
}
}
printf("Case #%d: %d\n",k++,ans);
}
return ;
}