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

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
 
并查集的删除详解见:点我打开链接
#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【并查集节点的删除】的更多相关文章

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

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

  2. nyoj 1022 合纵连横【并查集节点的删除】

    合纵连横 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...

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

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

  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(并查集的删除操作)

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

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

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

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

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

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

  9. hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. IOS-图片上传到服务器

    //获取document 路径- (NSString *)getDocumentPath{    NSArray *paths = NSSearchPathForDirectoriesInDomain ...

  2. MVC5学习笔记

    买了一本MVC5的书:ASP.NET MVC 5 高级编程(第5版).边学边记录一下 1.快速创建模型类,如:自动实现的属性 {get;set;} 输入“prop",按Tab两次,默认属性值 ...

  3. linux下安装svn(基于编码的方式)

    svn是什么,相信能看到这里的同学应该不会有这个问题了,费话不多说,开始: 1.创建目录 mkdir /home/svn/ 2.获取安装svn所需源文件(svn的官方网址是http://subvers ...

  4. java静态代理,动态代理,cglib代理

    代理模式在我们的应用中是很常见的,例如拦截器,spring的事务管理等.之所以能被代理,是因为java允许我们通过反射机制动态构造目标对象,并调用相应的方法. 就好像拿到了目标对象的引用,自然可以在目 ...

  5. 使用APPLICATION制作缓存,转存一下,有一段写的还可以。

    public sealed class CacheManager   {   private HttpApplicationState appPool = null;   /// <summar ...

  6. 彻底弄清c标准库中string.h里的常用函数用法

    在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...

  7. mysql基本介绍和优化技巧

    一. mysql框架和基本介绍 1. 框架图 更详细: 2. 存储引擎 MYISAM与INNODB对比: MYISAM:mysql5.1及以前版本的默认存储引擎.支持全文检索,压缩,表级锁等,但不支持 ...

  8. learn-python3

    # learn-python3   这是我初学Python时写的一套Python基础示例程序.主要基于廖雪峰老师的Python3教程和<<深入理解Python>>. 感谢! 下 ...

  9. 【转】深入 char * ,char ** ,char a[ ] ,char *a[] 内核

    原文出处:http://blog.csdn.net/daiyutage/article/details/8604720    C语言中由于指针的灵活性,导致指针能代替数组使用,或者混合使用,这些导致了 ...

  10. iOS性能优化

    最近采用Instruments 来分析整个应用程序的性能.发现很多有意思的点,以及性能优化和一些分析性能消耗的技巧,小结如下. Instruments使用技巧 关于Instruments官方有一个很有 ...