HDU——2473Junk-Mail Filter(并查集删点)
Junk-Mail Filter
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8687 Accepted Submission(s): 2753
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 ≤ 10 5, 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
并查集删点的操作看看了很久,找了无数篇博客,但是感觉除了一个有画图的博客之外其他的讲的都不是很详细,因此在有一点理解之后想写一下自己的看法
首先题目中分M(合并)和S(分离)两种操作,显然前者非常简单,后者有一点难理解。由于我一开始连第一个样例都看不懂,先解释一下题目第一组数据样例吧。
操作 | 集合关系 |
---|---|
M 0 1 | {0 1} {2} {3} {4} |
M 1 2 | {0 1 2} {3} {4} |
M 1 3 | {0 1 2 3} {4} |
S 1 | {0 2 3} {1} {4} |
M 1 2 | {0 1 2 3} {4} |
S 3 | {0 1 2} {3} {4} |
题目中说Your tool should remove all relationships that spam X has when this command is received,可能会误认为是把它所连的边全部抹掉,其实题目中集合的概念可以看成一些泡泡,M就是融合,S就是分裂,显然分裂出去一定是出去一个,但是融合就不一定了,比如{0 2 3}和{4 5 6},两边集合中任意地取出两个数进行融合就会使得两个集合融合在一起形成{0 2 3 4 5 6},就是说虽然M A B连接的是一个,但是实际上会把A所在的集合和B所在的集合联合到一起。然后这样就可以解释第一组样例了。1出去之后2又把1拉回来,然后把3分裂了出去。因此结果为3组如上表所示。
然后重点就是如何进行这样的S操作,其他很多人的博客已经解释过仅仅把祖先改掉是没用的,因此需要另一种思路:最普通的并查集是把数组元素进行合并,而数组元素是不会变的,永远都是最大编号的范围内——只认人。
而删点操作要换一换,不是把人合并,而是把这个位置的人合并——认位置不认人。放到题目里就是把分裂出去的人本身替换掉,若再拉回来就是另一个实体,但是回去之后的位置却是分裂之前的那个人所在的位置。比如下列这组数据
6 7
M 0 1
M 1 2
M 2 3
S 3
M 3 4
M 4 5
M 0 3
最后3的位置会被6替换掉(题目中N=6指的是编号范围从0~N-1)
然后后面的派3去融合其实是派6去融合。但是算的还是3的位置。
代码:
- #include<iostream>
- #include<algorithm>
- #include<cstdlib>
- #include<sstream>
- #include<cstring>
- #include<cstdio>
- #include<string>
- #include<deque>
- #include<stack>
- #include<cmath>
- #include<queue>
- #include<set>
- #include<map>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define MM(x) memset(x,0,sizeof(x))
- #define MMINF(x) memset(x,INF,sizeof(x))
- typedef long long LL;
- const double PI = acos(-1.0);
- const int N = 1100010;
- int pre[N], ran[N]; //pre记录父亲是谁,ran集合元素个数
- int vir[N], mark[N]; //vir记录某个位置的孩子是谁,mark统计集合个数用的数组mark
- void init()
- {
- for (int i = 0; i < N; i++)
- {
- pre[i] = i;
- ran[i] = 1;
- vir[i] = i;
- }
- MM(mark);
- }
- int find(int n)
- {
- if (n != pre[n])
- return pre[n] = find(pre[n]);
- return pre[n];
- }
- void joint(int a, int b)
- {
- int fa = find(a), fb = find(b);
- if (fa != fb)
- {
- if (ran[fa] >= ran[fb])
- {
- ran[fa] += ran[fb];
- pre[fb] = fa;
- ran[fb] = 0;
- }
- else
- {
- ran[fb] += ran[fa];
- pre[fa] = fb;
- ran[fa] = 0;
- }
- }
- }
- int main(void)
- {
- int n, m, i, j, a, b, c, k, cas = 0;
- char ops[5];
- while (~scanf("%d%d", &n, &m) && (n || m))
- {
- init();
- k = n;
- for (i = 0; i < m; i++)
- {
- scanf("%s", ops);
- if (ops[0] == 'M')
- {
- scanf("%d%d", &a, &b);
- joint(vir[a], vir[b]); //合并这个两个位置的人
- }
- else
- {
- scanf("%d", &c);
- ran[find(vir[c])]--;//分离出去一个原来的集合个数减一
- vir[c] = k; //这个位置的人换成无关的人
- pre[k] = k; //刚分离出去刚形成的新点,把祖先先改为自己
- k++;//更新已用人员
- }
- }
- int r = 0;
- for (i = 0; i < n; i++)
- {
- int f = find(vir[i]);
- if (!mark[f])
- {
- r++;
- mark[f] = 1;
- }
- }
- printf("Case #%d: %d\n", ++cas, r);
- }
- return 0;
- }
HDU——2473Junk-Mail Filter(并查集删点)的更多相关文章
- HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)
题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...
- HDU 2473 Junk-Mail Filter 并查集,虚拟删除操作
http://acm.hdu.edu.cn/showproblem.php?pid=2473 给定两种操作 第一种是合并X Y 第二种是把X分离出来,就是从原来的集合中分离出来,其它的关系不变. 关键 ...
- hdu 2473 Junk-Mail Filter (并查集之点的删除)
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- (step5.1.2)hdu 2473(Junk-Mail Filter——并查集)
题目大意:输入两个整数n,m(n表示点的个数,m表示操作数).在接下来的m行中,对点的操作有两种 1)M a b . 表示将a.b并到一个集合中 2)S a .表示将a从原来的集合中去除,而成为一个单 ...
- HDU 2473 Junk-Mail Filter(并查集的删除操作)
题目地址:pid=2473">HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了 ...
- 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~ ...
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
随机推荐
- [windows]解决Win7访问Windows 2003、XP共享慢的问题
解决方法: 1. 修改网卡配置打开本地连接属性,点击"配置"在"高级"选项卡中,将"大型发送分载(IPv4)"的值设置成"禁用&q ...
- sql语句执行碰到的问题
问题:传递给 LEFT 或 SUBSTRING 函数的长度参数无效 原因:在LEFT或SUBSTRING 中计算出来的长度是负数导致的 解决方法: 1)逐个排查法,2)先把语句执行一下,查看中断的地 ...
- POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30725 Accepted: 8389 Description &quo ...
- swift 接水果游戏ios源码
初学swift,写来练手的,游戏很简单 ,顾名思义就是接水果 ,菠萝不能接,接到一个水果得一分,接到菠萝扣五分,漏一个水果扣一分,初始分0分,当分数低于0分 就Game Over了,暂时适用5s的模拟 ...
- es的插件 ik分词器的安装和使用
今天折腾了一天,在es 5.5.0 上安装ik.一直通过官方给定的命令没用安装成功,决定通过手工是形式进行安装.https://github.com/medcl/elasticsearch-analy ...
- 强化学习_Deep Q Learning(DQN)_代码解析
Deep Q Learning 使用gym的CartPole作为环境,使用QDN解决离散动作空间的问题. 一.导入需要的包和定义超参数 import tensorflow as tf import n ...
- 《3+1团队》第八次团队作业:Alpha冲刺
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...
- c语言产生随机数的方法
在C语言中,rand()函数可以用来产生随机数,但是这不是真真意义上的随机数,是一个伪随机数,是根据一个数,我们可以称它为种子,为基准以某个递推公式推算出来的一系数,当这系列数很大的时候,就符合正态公 ...
- Luogu P2397 yyy loves Maths VI (mode)
题目传送门 虽然只是一道黄题,但还是学到了一点新知识-- 摩尔投票法 用\(O(1)\)的内存,\(O(n)\)的时间来找出一串长度为n的数中的众数,前提是众数出现的次数要大于\(n/2\) 方法很简 ...
- div section article区分--20150227
div section article ,语义是从无到有,逐渐增强的.div 无任何语义,仅仅用作样式化或者脚本化的钩子(hook),对于一段主题性的内容,则就适用 section,而假如这段内容可以 ...