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. Lucene索引的初步创建

    从百度上知道的,Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的 ...

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

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

  3. canonical 标签介绍

    rel=”canonical” 这个标签已经推出很久了,canonical 是 Google.雅虎.微软等搜索引擎一起推出的一个标签,它的主要作用是用来解决由于网址形式不同内容相同而造成的内容重复问题 ...

  4. SpringMVC源码阅读(三)

    先理一下Bean的初始化路线 org.springframework.beans.factory.support.AbstractBeanDefinitionReader public int loa ...

  5. 使用OPCDAAuto.dll编写C# OPC采集程序

    在一台新机器上运行使用OPC自动化接口编写的C#程序报错如下: 索 COM 类工厂中 CLSID 为 {28E68F9A-8D75-11D1-8DC3-3C302A000000} 的组件失败,原因是出 ...

  6. webserver<1>

    1. 实现基础的信号处理 sigaction使用前一定内存清零 2. 实现基础的进程模型 wait 等待子进程结束 #include <stdio.h> #include <unis ...

  7. css学习笔记一

    1.在css开头用* {margin:0;padding:0;}可以清除所有样式 2.在css中table,th,td {padding:0;}效果等同于cellpadding="0″. 3 ...

  8. jinfo命令(Java Configuration Info)

    jinfo可以输出并修改运行时的java 进程的opts.用处比较简单,用于输出JAVA系统参数及命令行参数.用法是jinfo -opt  pid 如:查看2788的MaxPerm大小可以用  jin ...

  9. Haskell函数的语法

    本章讲的就是 Haskell 那套独特的语法结构,先从模式匹配开始.模式匹配通过检查数据的特定结构来检查其是否匹配,并按模式从中取得数据. 在定义函数时,你可以为不同的模式分别定义函数本身,这就让代码 ...

  10. 实例模拟struts核心流程

    Struts,经典框架之一,每个java  web 开发人员都应该晓得它的大名.这里,我就用一个简单实例来模拟一下struts的核心流程.具体实例如下: 主界面: 点击提交后,程序根据具体的actio ...