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 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封邮件,有两种操作,M x,y 这是说明x,y属于同一类,S x ,则是将x孤立出来, 问最后有多少种类(共同特征)

思路: 并查集应用之点的删除

额,思路就是将这个要删除的尸体,依旧放在原来的集合中,将其灵魂移到新的点上,并用这个点代替死掉的点。

代码:

 #include<stdio.h>
#include<string.h>
#include<stdlib.h> const int maxn=; int father[maxn];
int rep[maxn];
bool tag[maxn];
int n,m,num; void init(){
num=n;
for(int i=;i<n;i++){
father[i]=i;
rep[i]=i;
}
} int fin(int x)
{
int t=x;
while(x!=father[x])
x=father[x];
//不用加速,就得无限的tle ,醉了醉了
while(t!=x){
t=father[t];
father[t]=x;
}
return x;
} void unin(int a ,int b){ a=fin(a);
b=fin(b);
if(a!=b)
father[a]=b;
} void delet(int val){ rep[val]=num;
father[num]=num;
num++;
} int func_cnt(){ int res=;
memset(tag,,sizeof(bool)*(num+));
for(int i=;i<n;i++){
int x=fin(rep[i]);
if(!tag[x]){
res++;
tag[x]=;
}
}
return res;
} int main()
{
int a,b;
char ss[];
int t=;
while(scanf("%d%d",&n,&m)!=EOF&&n+m){
init();
for(int i=;i<m;i++){
scanf("%s",ss);
if(ss[]=='M'){
scanf("%d%d",&a,&b);
unin(rep[a],rep[b]);
}
else{
scanf("%d",&a);
delet(a);
}
}
printf("Case #%d: %d\n",t++,func_cnt());
}
return ;
}

hdu 2473 Junk-Mail Filter (并查集之点的删除)的更多相关文章

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2473 给定两种操作 第一种是合并X Y 第二种是把X分离出来,就是从原来的集合中分离出来,其它的关系不变. 关键 ...

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

    题目地址:pid=2473">HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了 ...

  3. 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~ ...

  4. HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)

    题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...

  5. (step5.1.2)hdu 2473(Junk-Mail Filter——并查集)

    题目大意:输入两个整数n,m(n表示点的个数,m表示操作数).在接下来的m行中,对点的操作有两种 1)M a b . 表示将a.b并到一个集合中 2)S a .表示将a从原来的集合中去除,而成为一个单 ...

  6. hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩

    Description Recognizing junk mails is a tough task. The method used here consists of two steps:  1) ...

  7. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  8. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  9. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

随机推荐

  1. jquery 之height(),innerHeight(),outerHeight()方法区别详解

    在jQuery中,获取元素高度的函数有3个,它们分别是height(). innerHeight().outerHeight(). 与此相对应的是,获取元素宽度的函数也有3个,它们分别是width() ...

  2. git生成SSH key

    使用https:// 提交GitHub上的私有项目时,需要每次都输入帐号和密码,比较麻烦. 比如我自己的笔记本,在push的时候我可不想每次都输入这些. 那就使用SSH吧,这个需要在GitHub的帐号 ...

  3. Highlighting Text Item On Entry In Oracle Forms

    Highlight a Text Item in Oracle Forms With Visual Attribute It is very necessary to highlight the cu ...

  4. 大神写的一个纯CSS圆角框,膜拜!(支持IE9一下的低版本)

    留着提醒自己,底层才是最重要的,不要一直傻瓜的编程下去! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  5. 在Spring中使用脚本

    Spring支持3中不同的脚本语言(看来支持地还挺多的嘛):JRuby.Groovy和BeanShell. 这三个都是java社区的脚本语言(反正到目前为止我一个都没用过,可见我有多挫). JRuby ...

  6. 常用ADB命令

    adb devices          查看当前已与pc端连接的设备序列号 adb install -r   +apk名称     安装应用程序(带-r参数表示强制安装,可以覆盖安装) adb un ...

  7. Jconsole 监控tomcat

    通过jconsole监控可以获取监控tomcat的相关的数据信息 如何通过代码来获取其中的线程和内存状况呢? 首先要配置好jconsole监控的相关配置,一搜基本就是那一个, 配置配不好的话接下来的工 ...

  8. Win7路由器设置过程

    随着应用win7系统的人越来越多,对于这个系统的应用就更多了,其中大家最关注的就是这个系统和路由器上网的问题.下面,我们就来讲解一下win7系统的路由器的设置过程. 首先打开浏览器,在地址栏输入192 ...

  9. [转载] 彻底学习STL中的Allocator

    原文: http://cissco.iteye.com/blog/379093 帮助我们理解allocator的原理 Allocator是C++语言标准库中最神秘的部分之一.它们很少被显式使用,标准也 ...

  10. linux之od命令

    od [OPTION]... [FILE]...  把文件用8进制或者其他的格式显示出来.通常用于查看特殊格式文件的内容.  这个命令默认把文件的内容用八进制的形式清晰地写在标准输出上.如果是多个文件 ...