HDU 2473 - Junk-Mail Filter ,并查集的删点
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.
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.
M 0 1
M 1 2
M 1 3
S 1
M 1 2
S 3
3 1
M 1 2
0 0
Case #2: 2
#include <cstdio>
#include <cstring>
using namespace std;
int f[*],flag[*],x,y,ans,cnt,n,m,k=; char c;
int sf(int x){ return x==f[x]? x:f[x]=sf(f[x]); }
int main(){
while(scanf("%d%d",&n,&m),n+m){
ans=,cnt=*n;
for(int i=;i<n;i++) f[i]=n+i;
for(int i=n;i<*n+m;i++) f[i]=i;
memset(flag,,sizeof(flag));
for(int i=;i<=m;i++){
scanf(" %c",&c);
if(c=='M'){
scanf("%d%d",&x,&y);
f[sf(x)]=sf(y);
} else {
scanf("%d",&x);
f[x]=cnt++;
}
}
for(int i=;i<n;i++)
if(flag[sf(i)]==){
ans++;
flag[sf(i)]=;
} printf("Case #%d: %d\n",k++,ans);
} return ;
}
HDU 2473 - Junk-Mail Filter ,并查集的删点的更多相关文章
- hdu4496并查集的删边操作
题意: 给你一个图,问你删除一些边后还有几个连通快.. 思路: 典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ...
- 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 并查集删除(FZU 2155盟国)
http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~ ...
- HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)
题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...
- (step5.1.2)hdu 2473(Junk-Mail Filter——并查集)
题目大意:输入两个整数n,m(n表示点的个数,m表示操作数).在接下来的m行中,对点的操作有两种 1)M a b . 表示将a.b并到一个集合中 2)S a .表示将a从原来的集合中去除,而成为一个单 ...
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
随机推荐
- JS实现给页面表单设置触发默认按钮
var defaultBtnId; function setDefaultButton(id) { defaultBtnId = id; } document.onkeydown = function ...
- android——背景颜色渐变(梯度变化)
首先在drawable文件夹下面新建一个xml文件,起名为bgcolor.xml. 代码如下: <?xml version="1.0" encoding="utf- ...
- JQ 复制节点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 表空间使用情况统计[z]
SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", D.TOT_GROOT ...
- 代码初始化 故事板初始化 xib初始化总结
对象的初始化有三种方式 // 代码创建 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { ...
- SQL case when
jack 1tom 2anni 3poly 4 select buyer_name, ( case ' then '东' ' then '南' ' then '西' ' the ...
- c# 打开、保存文件对话框 和 文件夹选择对话框
1. OpenFileDialog openImageDialog = new OpenFileDialog(); openImageDialog.Filter = "Image Files ...
- Redis 作为缓存服务器的配置
随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...
- js 去掉字符串最后一个字符
var str = str.substring(0,str.length-1); alert(str); 注:length 需小写.
- Linux Power(一): kernel/power/earlysuspend.c
/* kernel/power/earlysuspend.c * * Copyright (C) 2005-2008 Google, Inc. * * This software is license ...