前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu 2473 Junk-Mail Filter (并查集之点的删除)

hdu 2473 Junk-Mail Filter (并查集之点的删除)

作者头像
Gxjun
发布2018-03-26 15:43:40
7310
发布2018-03-26 15:43:40
举报
文章被收录于专栏:mlml

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孤立出来, 问最后有多少种类(共同特征)

思路: 并查集应用之点的删除

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

代码:

代码语言:javascript
复制
 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-11-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Junk-Mail Filter
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档