Junk-Mail Filter

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9135    Accepted Submission(s): 2885

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个邮件,编号0~n-1,M a b 表示将a,b邮件归为一类,S a 表示将a从所属类中分离出来,问最后有几个分类。
代码:
 //并查集删点,可以将每一个点映射到对应的一个虚拟点上,在这个虚拟点上操作,如果删除某一点就把这个点指向另一个点如:1,2,3
//在一个并查集中时他们的根节点是1,但是当删去1时被分成了3个集合实际上是2个,我们把他们分别映射到4,5,6 即1-4,2-5,3-6 这样
//根节点就是4,删去1时就把1映射到7这样以4为根节点的这个集合中就只有两个点了.最后统计时找有几个根节点就行。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int fat[]; //数组开大一些
int num[];
int find(int x)
{
int rt=x;
while(fat[rt]!=rt)
rt=fat[rt];
int i=x,j;
while(i!=rt)
{
j=fat[i];
fat[i]=rt;
i=j;
}
return rt;
}
void connect(int x,int y)
{
int a=find(x),b=find(y);
if(a!=b)
{
fat[b]=a;
}
}
int main()
{
char ch[];
int a,b,k=;
while(scanf("%d%d",&n,&m)&&(n+m))
{
for(int i=;i<=n;i++) //映射到虚拟点
fat[i]=i+n;
for(int i=n+;i<=*n+m;i++)
fat[i]=i;
int tem=*n+;
for(int i=;i<=m;i++)
{
scanf("%s",ch);
if(ch[]=='M')
{
scanf("%d%d",&a,&b);
connect(a+,b+);
}
else
{
scanf("%d",&a);
fat[a+]=tem++; //指向另一个点
}
}
int ans=;
memset(num,,sizeof(num));
for(int i=;i<=n;i++) //统计
{
int nu=find(i);
if(num[nu]==)
{
num[nu]=;
ans++;
}
}
printf("Case #%d: %d\n",k++,ans);
}
return ;
}

*HDU2473 并查集的更多相关文章

  1. HDU2473 Junk-Mail Filter 【可删除的并查集】

    HDU2473 Junk-Mail Filter Problem Description Recognizing junk mails is a tough task. The method used ...

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

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

  3. HDU-2473 Junk-Mail Filter(并查集的使用)

    原题链接:https://vjudge.net/problem/11782/origin Description: Recognizing junk mails is a tough task. Th ...

  4. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  5. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  6. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  7. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  8. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  9. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

随机推荐

  1. JAVA关键字与保留字说明及使用

    1.abstract 2.boolean 3.break 4.byte 5.case 6.catch 7.char 8.class 9.continue 10.default 11.do 12.dou ...

  2. 使用ajax技术实现txt弹出在页面上

    使用ajax技术实现txt弹出在页面上   使用ajax技术实现点击按钮,将TXT文本里的内容通过弹出框显示到页面上 /*事件会在页面加载完成后触发.*/ <script> window. ...

  3. Hydra用户手册

    Hydra 参数: -R继续从上一次进度接着破解 -S大写,采用SSL链接 -s <PORT>小写,可通过这个参数指定非默认端口 -l <LOGIN>指定破解的用户,对特定用户 ...

  4. asp:DataGrid之添加asp:CheckBox做全选功能时涉及到绑值问题解决

    最大的意图是为asp:CheckBox的value绑定上自己需要的value值,而不是默认的字符串"on" 参考了这篇文章带Value属性的扩展CheckBox控件,意义不大,换了 ...

  5. PHP - xhprof+Graphviz 安装配置

    简介:XHProf是Facebook放出的轻量级调试工具.和Xdebug相比,XHProf更加易用和可控,尤其是生成流程图和调试数据对比的功能很好很强大. 参考:http://us2.php.net/ ...

  6. PowerDesigner使用

    首先我们需要创建一个测试数据库,为了简单,我们在这个数据库中只创建一个Student表和一个Major表.其表结构和关系如下所示. 看看怎样用PowerDesigner快速的创建出这个数据库吧. 1. ...

  7. 我常用的Vi命令

    Vi对于linux的重要性和受欢迎的程度在此一律不表.此刻互联网上不少介绍vi的文章和博客,相信写得比我好的也不在少数.然而为什么我依然写这样一篇文章呢?我对linux知识和了解也都来自于互联网,很难 ...

  8. Effective C++阅读笔记_条款2:尽量以const,enum,inline替换#define

    1.#define缺点1 #define NUM 1.2 记号NUM可能没有进入记号表,在调试或者错误信息中,无法知道1.2的含义. 改善:通过const int NUM = 1.2; 2.#dein ...

  9. MySQL5.6 新特性之GTID

    背景: MySQL5.6在5.5的基础上增加了一些改进,本文章先对其中一个一个比较大的改进"GTID"进行说明. 概念: GTID即全局事务ID(global transactio ...

  10. 面向切面编程AOP

    本文的主要内容(AOP): 1.AOP面向切面编程的相关概念(思想.原理.相关术语) 2.AOP编程底层实现机制(动态代理机制:JDK代理.Cglib代理) 3.Spring的传统AOP编程的案例(计 ...