2014
01-26

Junk-Mail Filter

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.

There are multiple test cases in the input file.
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.

There are multiple test cases in the input file.
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.

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

#include<map>
#include<Set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 100010
#define Maxm 2000010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x3f3f3f3f
#define Mod 1000000007
using namespace std;
int Set[Maxn*2],cnt,vi[Maxn*2],n;
void init()
{
for(int i=0;i<Maxn*2;i++)
Set[i]=i;
memset(vi,0,sizeof(vi));
cnt=n;
}
int Find(int x)
{
if(Set[x]!=x)
Set[x]=Find(Set[x]);
return Set[x];
}
void merg(int a,int b)
{
int x,y;
x=Find(a);
y=Find(b);
if(Set[x]==a&&Set[y]==b){
Set[x]=++cnt,Set[y]=cnt;
return ;
}
if(Set[x]==Set[y])
return ;
if(x<=n)
Set[x]=y;
else
Set[y]=x;
}
int main()
{
int m,i,j,u,v,Case=0;
char str[3];
while(scanf("%d%d",&n,&m),n||m){
init();
for(i=1;i<=m;i++){
scanf("%s",str);
if(str[0]=='M'){
scanf("%d%d",&u,&v);
merg(u,v);
}
else{
scanf("%d",&u);
Set[u]=u;
}
}
int ans=0;
for(i=0;i<n;i++){
u=Find(i);
if(!vi[u]){
ans++;
vi[u]=1;
}
}
printf("Case #%d: %d\n",++Case,ans);
}
return 0;
}

1. 吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱吱

2. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

3. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮