HDU 2473 - Junk-Mail Filter ,并查集的删点

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

大致题意:

  有n封邮件现在要将其含有相同的特征的放在一起,还要删掉一些误判的;

  现在问你这两种操作完成后还有多少种的信。

解题思路:

  主要是并查集的删点操作,建立虚拟节点。

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 int f[2*1000005],flag[4*1000005],x,y,ans,cnt,n,m,k=1; char c;
 5 int sf(int x){ return x==f[x]? x:f[x]=sf(f[x]); }
 6 int main(){
 7     while(scanf("%d%d",&n,&m),n+m){
 8         ans=0,cnt=2*n;
 9         for(int i=0;i<n;i++) f[i]=n+i;
10         for(int i=n;i<2*n+m;i++) f[i]=i;
11         memset(flag,0,sizeof(flag));
12         for(int i=1;i<=m;i++){
13             scanf(" %c",&c);
14             if(c==‘M‘){
15                 scanf("%d%d",&x,&y);
16                 f[sf(x)]=sf(y);
17             } else {
18                 scanf("%d",&x);
19                 f[x]=cnt++;
20             }
21         }
22         for(int i=0;i<n;i++)
23         if(flag[sf(i)]==0){
24             ans++;
25             flag[sf(i)]=1;
26         } printf("Case #%d: %d\n",k++,ans);
27     } return 0;
28 }
时间: 2025-01-06 04:27:45

HDU 2473 - Junk-Mail Filter ,并查集的删点的相关文章

HDU 3635 延缓更新的并查集

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2839    Accepted Submission(s): 1097 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

HDU 2473 Junk-Mail Filter (并查集的删除操作)

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 w

hdu 2473 Junk-Mail Filter 并查集删除

点击打开链接 http://acm.hdu.edu.cn/showproblem.php?pid=2473 题意:给出n种操作,M a b表示a和b是同一个并查集,S a表示在并查集中删除a,要注意的是,如果1和2是一个并查集,2和3是一个并查集,那么1和3也是一个并查集,即使删除2之后,我们依然可以得到1和3还是一个集合里的. 思路:由于并查集是一种树结构,无法在树种删除一个点后还让树继续保持着之前的样子,所以我们删除是采取的操作是使用映射,一开始所有点的映射都是自己,然后删除操作就是把这个点

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): 7254    Accepted Submission(s): 2307 Problem Description Recognizing junk mails is a tough task. The method used here consists o

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 o

HDU 2473 Junk-Mail Filter 并查集,虚拟删除操作

给定两种操作 第一种是合并X Y 第二种是把X分离出来,就是从原来的集合中分离出来,其它的关系不变. 关键是怎么分离,可以考虑把它变成一个其它值.HASH[i] = other_val 然后用新值去做并查集即可. 需要注意的一点就是 加入现在根是1,fa[1] = 1, fa[2] = 1, fa[3] = 1 那么如果你删除了1,这应该输出2.但是现在是fa[2] = 1,是一个不存在的根了,这个时候ans应该+1,但是按照并查集的思路 if (find(HASH[i]) == HASH[i]

HDU 2473 并查集的删点

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 t