# Junk-Mail Filter

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6276    Accepted Submission(s): 1981

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.

Input

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.

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

Source

2008 Asia Regional Hangzhou

题意： 给你n封邮件，有两种操作，M x,y 这是说明x，y属于同一类，S x ,则是将x孤立出来， 问最后有多少种类（共同特征）

额，思路就是将这个要删除的尸体，依旧放在原来的集合中，将其灵魂移到新的点上，并用这个点代替死掉的点。

``` 1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4
5 const int maxn=1010000;
6
7 int father[maxn];
8 int rep[maxn];
9 bool tag[maxn];
10 int n,m,num;
11
12 void init(){
13     num=n;
14   for(int i=0;i<n;i++){
15      father[i]=i;
16      rep[i]=i;
17   }
18 }
19
20 int fin(int x)
21 {
22     int t=x;
23     while(x!=father[x])
24         x=father[x];
25      //不用加速，就得无限的tle ，醉了醉了
26       while(t!=x){
27         t=father[t];
28         father[t]=x;
29     }
30     return  x;
31 }
32
33 void unin(int a ,int b){
34
35      a=fin(a);
36      b=fin(b);
37     if(a!=b)
38       father[a]=b;
39 }
40
41 void delet(int val){
42
43     rep[val]=num;
44     father[num]=num;
45     num++;
46 }
47
48 int func_cnt(){
49
50   int res=0;
51   memset(tag,0,sizeof(bool)*(num+10));
52   for(int i=0;i<n;i++){
53       int x=fin(rep[i]);
54       if(!tag[x]){
55         res++;
56         tag[x]=1;
57     }
58  }
59   return res;
60 }
61
62 int main()
63 {
64  int  a,b;
65  char ss[2];
66  int  t=1;
67  while(scanf("%d%d",&n,&m)!=EOF&&n+m){
68       init();
69     for(int i=0;i<m;i++){
70      scanf("%s",ss);
71      if(ss[0]=='M'){
72         scanf("%d%d",&a,&b);
73         unin(rep[a],rep[b]);
74      }
75      else{
76         scanf("%d",&a);
77         delet(a);
78      }
79     }
80    printf("Case #%d: %d\n",t++,func_cnt());
81  }
82   return 0;
83 }```

0 条评论

• ### hdu 4009 Transfer water（最小型树图）

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/657...

• ### hdu----(1528)Card Game Cheater(最大匹配/贪心)

Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...

• ### hdu----（5045）Contest（数位dp）

Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J...

• ### POJ-1276-Cash Machine（多重背包）

Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions:...

• ### 【Codeforces】1213B - Bad Prices

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。 ...

• ### HDU 3032 Nim or not Nim?(Multi-Nim)

Problem Description Nim is a two-player mathematic game of strategy in which ...

• ### Codeforces 626E Simple Skewness(暴力枚举+二分)

E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megab...

• ### 2017 ACM-ICPC 亚洲区（乌鲁木齐赛区）网络赛- A. Banana

In the forest, there is a Banana Company that provides bananas from different pl...

• ### POJ 1012 Joseph

Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53862 ...