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

2008 Asia Regional Hangzhou

【题意】:给定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 【并查集删除】的更多相关文章

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

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

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

  3. HDU2473 Junk-Mail Filter - 并查集删除操作(虚父节点)

    传送门 题意: 每次合并两份邮件,或者将某一份邮件独立出来,问最后有多少个邮件集合. 分析: 考虑初始化每个节点的祖先为一个虚父节点(i + n),虚父节点指向它自己.这样可以进行正常的合并操作. 而 ...

  4. hdu 2473 Junk-Mail Filter (并查集之点的删除)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

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

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

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

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

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

  8. Hdu 2473(并查集删除操作) Junk-Mail Filter

    有木有非常吊 加强 加强版   啊  ,看了都不敢做了   .后来先做了食物链这个我还是看过的.但还是A不掉,没明确神魔意思 .总而言之.大牛的博客是个好东西.我就那么看了一下,还是不懂怎莫办啊,哎, ...

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

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

随机推荐

  1. oracle 引用类型声明

  2. hbase(0.94) get、scan源码分析

    简介 本文是需要用到hbase timestamp性质时研究源码所写.内容有一定侧重.且个人理解不算深入,如有错误请不吝指出. 如何看源码 hbase依赖很重,没有独立的client包.所以目前如果在 ...

  3. [洛谷P2147][SDOI2008]洞穴勘测

    题目大意:有$n$个洞穴,$m$条指令,指令有三种 $Connect\;u\;v$:在$u,v$之间连一条边 $Destroy\;u\;v$:切断$u,v$之间的边 $Query\;u\;v$:询问$ ...

  4. day10_plus

    刚才发奖哈哈哈 想不到被惨虐的我还能混个牌子哈哈哈好开心

  5. 【NOIP模拟赛】与非 乱搞

    biubiu~~~ 正解是线段树维护真值表,但是我觉得对于这道题来说乱搞就够了....... 我们发现如果我们把每一个数都一开始取反就会发现对于最后结果来说 x=x^1,x nand x=x|x ,x ...

  6. gcc用法小记

    By francis_hao    Feb 13,2017 概要 这里只列出了最常用的选项   选项解释 -c|-S|-E 启动gcc编译器时,它会顺序执行预处理.编译.汇编和连接(四个阶段的详细介绍 ...

  7. 关于 WizTools.org RESTClient的使用

    今天分享一个很好用的测试service的工具,很好用 提供两种方法使用这个东东. 第一种方法 通过cmd命令窗口. (1)cd C:\Users\li_weifeng\Desktop\c4d2(文件存 ...

  8. GoogleMap在js中的应用

    <html> <head> <meta name="viewport" content="initial-scale=1.0, user-s ...

  9. 前端面试js题

    var a=10; (function(){ console.log(a); var a=100; })(); 结果:输出undefined 解释: function中有var a=100; 声明会提 ...

  10. 前端面试:提升web性能

    1,减少HTTP请求数 A,从设计实现层简化页面 B,合理设置HTTP缓存 C,资源合并与压缩.如果可以的话,尽可能的将外部脚本,央视进行合并,多个合为一,css,javascript,image都可 ...