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

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 条评论
登录 后参与评论

相关文章

来自专栏osc同步分享-java技术分享站

springmvc 其他

<!-- 配置直接转发的页面 --> <!-- 可以直接相应转发的页面, 而无需再经过 Handler 的方法. --> <mvc:view-contr...

36480
来自专栏GopherCoder

『Go 内置库第一季:json』

在日常开发过程中,使用最频繁的当然是内置库,无数的开源项目,无不是在内置库的基础之上进行衍生、开发,所以其实是有很大的必要进行梳理学习。

11920
来自专栏程序生活

Leetcode-Medium 338. Counting Bits题目描述代码实现

题目描述 Given a non negative integer number num. For every numbers i in the range ...

26550
来自专栏算法修养

PAT 甲级 1024 Palindromic Number

1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 ...

41760
来自专栏用户2442861的专栏

HttpEntity的类型及其使用

  代表底层流的基本实体。通常是在http报文中获取的实体。他只有一个空参的构造方法。刚创建时没有内容,长度为负值。需要通过两个方法,把值赋进去。

1.2K10
来自专栏算法修养

PAT 甲级 1068 Find More Coins(0,1背包)

1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B ...

324110
来自专栏计算机视觉与深度学习基础

Leetcode 268. Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

231100
来自专栏青青天空树

springboot+security整合3

  这篇讲解如何自定义鉴权过程,实现根据数据库查询出的url和method是否匹配当前请求的url和method来决定有没有权限。security鉴权过程如下:...

53020
来自专栏清晨我上码

基于session的国际化实现方式

14820
来自专栏小樱的经验随笔

HDU 3782 xxx定律

xxx定律 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...

36890

扫码关注云+社区

领取腾讯云代金券