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. 【bzoj4720】[NOIP2016]换教室

    题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的课程同时在不同的 ...

  2. PHP合并2个数字键数组的值

    先要了解一个基础知识点:PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧 <?php /** * PHP合并2个数字键数组的值 * * @param arr ...

  3. C和指针 第十章 结构和联合 (二)

    结构体传值: 结构体也是标量,像字符和整数一样,可以传递给一个函数,但是传入整个结构体效率很低,可以传入指向结构体的指针来提高效率.如果不希望程序对结构体变量改变可以加入const关键词. typed ...

  4. h5 hdf5 文件转 tif 流程

    由于需要对h5(hdf5)格式的dem数据进行拼接,但是arcgis不能识别h5的地理参考信息,所以先将h5文件转为带地理参考的tif文件,然后再进行拼接. 工具:arcgis+envi 1.用arc ...

  5. ubuntu和centos安装RRDTool——cacti前置技能

    Installing Pre-Requisites Note that RRDTool 1.0.x versions included all dependancies, but 1.2.x vers ...

  6. jquery手写实现单页滚动导航

    效果说明:点击tab导航,页面滑动到下方相应板块.并且当页面通过鼠标滚动下去时,上方的tab也可以自动切换到当前位置的板块上. 代码说明:js中对两个动作分别写,一个是tab点击下滑到相应板块位置:一 ...

  7. ubuntu下安装chrome

    首先.题主在试过直接ubuntu终端命令安装chrome失败. 把经历过的错误稍微提一下: 在终端输入 下载安装包 sudo wget https://dl.google.com/linux/dire ...

  8. Python之路,Day2 - Python基础2

    def decode(self, encoding=None, errors=None): """ 解码 """ ""& ...

  9. Python 开发轻量级爬虫06

    Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...

  10. poj1988_Cube Stacking

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 24130   Accepted: 8468 Ca ...