*HDU2473 并查集
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
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.
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.
//并查集删点,可以将每一个点映射到对应的一个虚拟点上,在这个虚拟点上操作,如果删除某一点就把这个点指向另一个点如: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 并查集的更多相关文章
- HDU2473 Junk-Mail Filter 【可删除的并查集】
HDU2473 Junk-Mail Filter Problem Description Recognizing junk mails is a tough task. The method used ...
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- HDU-2473 Junk-Mail Filter(并查集的使用)
原题链接:https://vjudge.net/problem/11782/origin Description: Recognizing junk mails is a tough task. Th ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
随机推荐
- LInux升级Python版本2.7.11所遇问题汇总
首先请原谅我使用校园网络,基本上打不开谷歌,网络搜取得帮助均来自度娘. 对于我这个linux新手 IT 新手来说,自己升级点东西好担心,万一出错,可能都要重来.... 参照度娘内容和自己摸索,今天晚上 ...
- jQuery如何判断元素是否是隐藏的?
jQuery函数简介: is(expr) 用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true. 如果没有元素符合,或者表达式无效,都返回'false'. 注 ...
- Python正则表达式详解
我用双手成就你的梦想 python正则表达式 ^ 匹配开始 $ 匹配行尾 . 匹配出换行符以外的任何单个字符,使用-m选项允许其匹配换行符也是如此 [...] 匹配括号内任何当个字符(也有或的意思) ...
- NameError: name 'sys_platform' is not defined
pip install --upgrade distribute
- 使用EmBitz开发STM32项目的环境配置
一.EmBitz软件获取与安装 1.EmBitz软件的获取 EmBitz原名Em::Blocks,是基于Code::Blocks开发的,面向嵌入式的C/C++集成开发环境.支持J-Link和ST-Li ...
- 51. N-Queens
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- Entity Framework 中的in操作实例
var listimportDataEOIResoults = importDataEOIResoults.Select(i => i.ProductSelectionId).ToList(); ...
- C之按位运算符
http://www.cnblogs.com/Kazaf/archive/2012/03/19/2406006.html
- AgileEAS.NET SOA 中间件平台5.2版本下载、配置学习(一):下载平台并基于直连环境运行
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- 转载:稀疏矩阵存储格式总结+存储效率对比:COO,CSR,DIA,ELL,HYB
http://www.cnblogs.com/xbinworld/p/4273506.html 稀疏矩阵是指矩阵中的元素大部分是0的矩阵,事实上,实际问题中大规模矩阵基本上都是稀疏矩阵,很多稀疏度在9 ...