hdoj 2473 Junk-Mail Filter【并查集节点的删除】
Junk-Mail Filter
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7515 Accepted Submission(s):
2368
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.
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.
number of distinct common characteristics, to the console. Follow the format as
indicated in the sample below.
#include<stdio.h>
#include<string.h>
#define MAX 1001000
int set[MAX],vis[MAX];
int mark[MAX];
int n,m,count;
void init()
{
int i,j;
for(i=0;i<n;i++)
{
set[i]=i;
vis[i]=i;
}
}
int find(int fa)
{
int t;
int ch=fa;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
void getmap()
{
int i,j,k=n;
char a[2];
memset(a,'\0',sizeof(a));
for(i=0;i<m;i++)
{
scanf("%s",a);
if(a[0]=='M')
{
int b,c;
scanf("%d%d",&b,&c);
mix(vis[b],vis[c]);
}
else
{
int b;
scanf("%d",&b);
vis[b]=k;
set[k]=k;
k++;
}
}
}
void solve()
{
int i,ans=0;
memset(mark,0,sizeof(mark));
for(i=0;i<n;i++)
{
int stem=find(vis[i]);
if(!mark[stem])
{
ans++;
mark[stem]=1;
}
}
printf("Case #%d: %d\n",count++,ans);
}
int main()
{
count=1;
while(scanf("%d%d",&n,&m),n|m)
{
init();
getmap();
solve();
}
return 0;
}
hdoj 2473 Junk-Mail Filter【并查集节点的删除】的更多相关文章
- HDU 2473 Junk-Mail Filter 并查集,虚拟删除操作
http://acm.hdu.edu.cn/showproblem.php?pid=2473 给定两种操作 第一种是合并X Y 第二种是把X分离出来,就是从原来的集合中分离出来,其它的关系不变. 关键 ...
- nyoj 1022 合纵连横【并查集节点的删除】
合纵连横 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...
- 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 (并查集之点的删除)
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2473 Junk-Mail Filter(并查集的删除操作)
题目地址:pid=2473">HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了 ...
- 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~ ...
- HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)
题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...
- (step5.1.2)hdu 2473(Junk-Mail Filter——并查集)
题目大意:输入两个整数n,m(n表示点的个数,m表示操作数).在接下来的m行中,对点的操作有两种 1)M a b . 表示将a.b并到一个集合中 2)S a .表示将a从原来的集合中去除,而成为一个单 ...
- hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- ios专题 - CocoaPods - 初次体验
[原创]http://www.cnblogs.com/luoguoqiang1985 这CocoaPods怎么用呢? 参考官方文章:guides.cocoapods.org/using/using-c ...
- MySQL全文检索笔记 转载
1. MySQL 4.x版本及以上版本提供了全文检索支持,但是表的存储引擎类型必须为MyISAM,以下是建表SQL,注意其中显式设置了存储引擎类型 CREATE TABLE articles ( id ...
- 《cut命令》-linux命令五分钟系列之十九
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- ListView复用和优化详解
我们每一个Android开发人员对ListView的使用肯定是很熟悉的,然而多少人能真正的懂ListView的缓存机制呢,说白了就是ListView为了提高效率,而内部实现的一种优化,牺牲一点内存.而 ...
- python第一次上机遇到的困难
正确 10 58 27412 2-1019 长度转换程序(10分) 完善下面的程序,能够: (1) 将用户输入的公制长度单位(米.千米)转换成英制长度单位(英寸.英里): (2) 将用户输入的英制 ...
- C语言实现五子棋简单功能
/******************************************************************** C-4.29-1: 实现五子棋游戏 操作说明:用方向键或者& ...
- code jam训练
https://code.google.com/codejam/contests.html http://student.csdn.net/mcs/programming_challenges
- Contest20140710 sequence
sequence|sequence.in|sequence.out 题目描述: 给定一个整数K和长为N的数列{Ai},求有多少个子串(不含空串)的和为K的倍数.(在这里子串表示{A[i]..A[j]} ...
- 【HDU4859】 海岸线(网络流-最小割)
Problem Description 欢迎来到珠海! 由于土地资源越来越紧张,使得许多海滨城市都只能依靠填海来扩展市区以求发展.作为Z市的决策人,在仔细观察了Z市地图之后,你准备通过填充某些海域来扩 ...
- ibatis 中isNull, isNotNull与isEmpty, isNotEmpty区别
在iBATIS中isNull用于判断参数是否为Null,isNotNull相反 isEmpty判断参数是否为Null或者空,满足其中一个条件则其true isNotEmpty相反,当参数既不为Null ...