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. Eclipse不能自动编译 java文件,不会生成CLASS

    每次修改类代码后都得重启 Tomcat 花了1天终于解决,网上所说基本是下面1和2的方法,使用之后还是不行最后重新建工作环境导入项目对比了一下找到第三种方法 1.Project 下有个 "B ...

  2. 在Windows下用MingW 4.5.2编译OpenCV 2.3.0

    需要的工具:1.安装QT SDK环境2.安装CMake for Windows3.OpenCV最新Windows源码步骤:1.将QT SDK安装目录下的{QtSDK}\mingw\bin添加到系统环境 ...

  3. Ubuntu1404+Django1.9+Apache2.4部署配置2配置文件设置

    转载注明出处,个人博客:http://www.cnblogs.com/wdfwolf3/ Django首要的部署平台是WSGI,它是Python Web服务器和应用的标准.使用Apache和mod_w ...

  4. 校省选赛第一场D题TwoDecks题解

    今天晚上第二场比赛,现在还是赛后刷上次的题目,越刷越伤心,发现我赛后一次AC的功力很强大啊!!!(希望今晚变成是赛中一次AC啊!!) 好啦,回归正题. 看题目 D. Merging Two Decks ...

  5. [LeetCode OJ] Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  6. 【清橙A1084】【FFT】快速傅里叶变换

    问题描述 离散傅立叶变换在信号处理中扮演者重要的角色.利用傅立叶变换,可以实现信号在时域和频域之间的转换. 对于一个给定的长度为n=2m (m为整数) 的复数序列X0, X1, …, Xn-1,离散傅 ...

  7. php 单引号与双引号区别

    一.单引号与双引号区别 1." "双引号里面的字段会经过编译器解释,然后再当作HTML代码输出. 2.' '单引号里面的不进行解释,直接输出. 从字面意思上就可以看出,单引号比双引 ...

  8. Windows Phone 之下拉菜单ListPicker

    默认情况下,Visual Studio的ToolBox里没有任何下拉菜单的控件可供使用,虽然可以手工输入代码使用隐藏的ComboBox来实现下拉菜单,但是显示出来的菜单与Metro UI主题不匹配.S ...

  9. Apache提示You don't have permission to access / on this server问题解决

    测试时遇到将一本地目录设置为一apache的虚拟主机,在httpd-vhosts.conf文件中进行简单设置,然后在hosts文件中将访问地址指向本地,启动apache,进行访问,却出现了You do ...

  10. python运维开发之第二天

    一.模块初识: 1.模块定义 python是由一系列的模块组成的,每个模块就是一个py为后缀的文件,同时模块也是一个命名空间,从而避免了变量名称冲突的问题.模块我们就可以理解为lib库,如果需要使用某 ...