HDU 2473 Junk-Mail Filter 并查集删除
Posted Roni
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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): 8640 Accepted Submission(s): 2735
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
【题意】:给定n个点,刚进行两种操作,将两个点合并,以及将一个点孤立,问最后点有几堆
【分析】:由于并查集是一种树结构,无法在树中删除一个点后还让树继续保持着之前的样子,所以我们删除采取的操作是使用映射,一开始所有点的映射都是自己,然后删除操作就是把这个点映射到另一个不存在的点上,这样原来的点还在原来的集合中,用来保持着原先集合的各种属性,之后的所有对这个点的操作都是对它映射的点的操作。与一般的并查集不同的是,找父亲的操作,找的不是这个点本身的父亲,而是所有点的映射的父亲。每一个点都设立一个虚拟父亲比如1,2,3的父亲分别是4,5,6,现在合并1,2,3都在一个集合,那他们的父亲都是4,现在删除1,那就给1重新申请一个节点7。现在2,3的父亲是4,1的父亲是7,删除成功。
//这是关键, 虽然空间的消耗比较大, 但是节省了大量时间, 这样处理的目的是将0 -> N-1 的 节点处理成叶子节点,这样在对这些节点做 S 操作的时候就不会影响到其他的节点, 而 find 操作是带路径压缩的, 所以就保证了我们所有要处理的节点一直是叶子节点 !!!
for(i=0;i<n;i++) bin[i]=i+n; //虚拟父节点 for(i=n;i<=n+n+m;i++) bin[i]=i //最多可能删除m个节点
【代码】:
#include<stdio.h> #include<string.h> using namespace std; int vis[1200000]; int f[1200000]; int find(int a) { int r=a; while(f[r]!=r) r=f[r]; int i=a; int j; while(i!=r) { j=f[i]; f[i]=r; i=j; } return r; } void merge(int a,int b) { int A,B; A=find(a); B=find(b); if(A!=B) f[B]=A; } int main() { int n,m; int kase=0; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; int cont=n*2; for(int i=0;i<n;i++) { f[i]=i+n; } for(int i=n;i<2*n+m+1;i++) { f[i]=i; } for(int i=0;i<m;i++) { char op[5]; scanf("%s",op); if(op[0]==\'M\') { int x,y; scanf("%d%d",&x,&y); merge(x,y); } if(op[0]==\'S\') { int x; scanf("%d",&x); f[x]=cont++; } } int output=0; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { int tmp=find(i); if(vis[tmp]==0) { vis[tmp]=1; output++; } } printf("Case #%d: %d\\n",++kase,output); } }
以上是关于HDU 2473 Junk-Mail Filter 并查集删除的主要内容,如果未能解决你的问题,请参考以下文章
HDU 2473 Junk-Mail Filter (并查集的删除操作)
HDU 2473 Junk-Mail Filter 并查集删除
hdu 2473 Junk-Mail Filter 并查集删点,模板题
HDU 2473 Junk-Mail Filter(并查集的删除操作)