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. 批量翻转PNG图片

    用了好几个软件都不好用. 要么不能翻转PNG, 要么翻转之后没有透明度了. 基本上全是图形界面, 要鼠标批量拖放. 所以, 还是自己动手, 写一个批量png翻转工具. #include <ios ...

  2. C#一个字符串的加密与解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  3. CentOS如何查看端口是被哪个应用/进程占用

    有时启动应用时会发现端口已经被占用,或者是感觉有些端口自己没有使用却发现是打开的.这时我们希望知道是哪个应用/进程在使用该端口. CentOS下可以用netstat或者lsof查看,Windows下也 ...

  4. Android新建项目 默认布局改为 LinearLayout

    目前此方法仅适用于eclipse 需要修改SDK 目录 android-sdk/tools/templates/activities/BlankActivity/root/res/layout 文件: ...

  5. PYTHON开发--面向对象基础入门

    面向对象 一:面向对象初级 1.思考:首先在python中,以前我们以前用到的几乎都是函数式编程,但是有时候函数式编程其中代码重复利用率太高,我们往往会把这些重复代码写进一个函数日后去调用,所以呢,今 ...

  6. Python 手册——开胃菜

    如果你写过大规模的Shell脚本,应该会有过这样的体会:你还非常想再加一些别的功能进去,但它已经太大. 太慢.太复杂了:或者这个功能需要调用一个系统函数,或者它只适合通过C来调用……通常这些问题还不足 ...

  7. 练习2 J题 - 多项式求和

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 多项式 ...

  8. 机器视觉工具箱-Machine Vision Toolbox for Matlab

    发现了一个机器视觉的Matlab工具箱,分享一下. 机器视觉工具箱(MVT的)规定,在机器视觉和基于视觉的控制有益的多种功能.这是一个有点折衷收集反映作者在光度学,摄影测量,色度学 方面的个人利益.它 ...

  9. Java实现Http服务器(二)

    上节讲到的JDK自带的HttpServer组件,实现方法大概有三十个类构成,下面尝试着理解下实现思路. 由于Java的source代码中有很多注释,粘贴上来看着费劲,自己写个程序消除注释. impor ...

  10. 第一个deeplearning4jproject跑起

    deeplearning4j是基于java的深度学习库,当然,它有许多特点,但暂时还没学那么深入,所以就不做介绍了 需要学习dl4j,无从下手,就想着先看看官网的examples,于是,下载了exam ...