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[];
- int f[];
- 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=;
- while(~scanf("%d%d",&n,&m))
- {
- if(n==&&m==)break;
- int cont=n*;
- for(int i=;i<n;i++)
- {
- f[i]=i+n;
- }
- for(int i=n;i<*n+m+;i++)
- {
- f[i]=i;
- }
- for(int i=;i<m;i++)
- {
- char op[];
- scanf("%s",op);
- if(op[]=='M')
- {
- int x,y;
- scanf("%d%d",&x,&y);
- merge(x,y);
- }
- if(op[]=='S')
- {
- int x;
- scanf("%d",&x);
- f[x]=cont++;
- }
- }
- int output=;
- memset(vis,,sizeof(vis));
- for(int i=;i<n;i++)
- {
- int tmp=find(i);
- if(vis[tmp]==)
- {
- vis[tmp]=;
- output++;
- }
- }
- printf("Case #%d: %d\n",++kase,output);
- }
- }
HDU 2473 Junk-Mail Filter 【并查集删除】的更多相关文章
- HDU 2473 Junk-Mail Filter 并查集删除(FZU 2155盟国)
http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~ ...
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- HDU2473 Junk-Mail Filter - 并查集删除操作(虚父节点)
传送门 题意: 每次合并两份邮件,或者将某一份邮件独立出来,问最后有多少个邮件集合. 分析: 考虑初始化每个节点的祖先为一个虚父节点(i + n),虚父节点指向它自己.这样可以进行正常的合并操作. 而 ...
- hdu 2473 Junk-Mail Filter (并查集之点的删除)
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2473 Junk-Mail Filter 并查集,虚拟删除操作
http://acm.hdu.edu.cn/showproblem.php?pid=2473 给定两种操作 第一种是合并X Y 第二种是把X分离出来,就是从原来的集合中分离出来,其它的关系不变. 关键 ...
- HDU 2473 Junk-Mail Filter(并查集的删除操作)
题目地址:pid=2473">HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了 ...
- HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)
题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...
- Hdu 2473(并查集删除操作) Junk-Mail Filter
有木有非常吊 加强 加强版 啊 ,看了都不敢做了 .后来先做了食物链这个我还是看过的.但还是A不掉,没明确神魔意思 .总而言之.大牛的博客是个好东西.我就那么看了一下,还是不懂怎莫办啊,哎, ...
- (step5.1.2)hdu 2473(Junk-Mail Filter——并查集)
题目大意:输入两个整数n,m(n表示点的个数,m表示操作数).在接下来的m行中,对点的操作有两种 1)M a b . 表示将a.b并到一个集合中 2)S a .表示将a从原来的集合中去除,而成为一个单 ...
随机推荐
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- BZOJ4563 HAOI2016放棋子(高精度)
没看清题还以为是要求数最大匹配数量……注意到任意障碍不在同一行同一列,且恰好有n个障碍,不妨通过交换列使得第i行第i列均有障碍.那么就是个错排了.居然wa了一发简直没救. #include<io ...
- Codeforce 721C DP+DAG拓扑序
题意 在一个DAG上,从顶点1走到顶点n,路径上需要消费时间,求在限定时间内从1到n经过城市最多的一条路径 我的做法和题解差不多,不过最近可能看primer看多了,写得比较复杂和结构化 自己做了一些小 ...
- 莫比乌斯反演题表II
bzoj3994:[SDOI2015]约数个数和 **很好推+有个小结论bzoj3309:DZY Loves Math ***很好推+线筛某函数/卡常bzoj4816:[Sdoi2017]数字表格 * ...
- [NOIP2012] 文化之旅 dfs
这道题就体现了聪明的搜索策略的重要性,如果我们正着搜,判断效率会明显下滑,所以我们就采用倒着搜索.(其实很玄学.....) #include <cstdio> #include <b ...
- 【BZOJ 3669】 [Noi2014]魔法森林 LCT维护动态最小生成树
这道题看题意是在求一个二维最小瓶颈路,唯一可行方案就是枚举一维在这一维满足的条件下使另一维最小,那么我们就把第一维排序利用A小的边在A大的情况下仍成立来动态加边维护最小生成树. #include &l ...
- Any gotchas at all with converting from MyISAM to InnoDB?
Q: I'm ready to move from MyISAM to InnoDB but wanted to know if there was a full list of things to ...
- Oulipo HDU - 1686
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- 模拟实现jdk动态代理
实现步骤 1.生成代理类的源代码 2.将源代码保存到磁盘 3.使用JavaCompiler编译源代码生成.class字节码文件 4.使用JavaCompiler编译源代码生成.class字节码文件 5 ...
- 常见通用的 JOIN 查询
SQL执行循序: 手写: SELECT DISTINCT <query_list> FROM <left_table> <join type> JOIN <r ...