Junk-Mail Filter

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6276    Accepted Submission(s): 1981

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
 
Source
 

题意: 给你n封邮件,有两种操作,M x,y 这是说明x,y属于同一类,S x ,则是将x孤立出来, 问最后有多少种类(共同特征)

思路: 并查集应用之点的删除

额,思路就是将这个要删除的尸体,依旧放在原来的集合中,将其灵魂移到新的点上,并用这个点代替死掉的点。

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. const int maxn=;
  6.  
  7. int father[maxn];
  8. int rep[maxn];
  9. bool tag[maxn];
  10. int n,m,num;
  11.  
  12. void init(){
  13. num=n;
  14. for(int i=;i<n;i++){
  15. father[i]=i;
  16. rep[i]=i;
  17. }
  18. }
  19.  
  20. int fin(int x)
  21. {
  22. int t=x;
  23. while(x!=father[x])
  24. x=father[x];
  25. //不用加速,就得无限的tle ,醉了醉了
  26. while(t!=x){
  27. t=father[t];
  28. father[t]=x;
  29. }
  30. return x;
  31. }
  32.  
  33. void unin(int a ,int b){
  34.  
  35. a=fin(a);
  36. b=fin(b);
  37. if(a!=b)
  38. father[a]=b;
  39. }
  40.  
  41. void delet(int val){
  42.  
  43. rep[val]=num;
  44. father[num]=num;
  45. num++;
  46. }
  47.  
  48. int func_cnt(){
  49.  
  50. int res=;
  51. memset(tag,,sizeof(bool)*(num+));
  52. for(int i=;i<n;i++){
  53. int x=fin(rep[i]);
  54. if(!tag[x]){
  55. res++;
  56. tag[x]=;
  57. }
  58. }
  59. return res;
  60. }
  61.  
  62. int main()
  63. {
  64. int a,b;
  65. char ss[];
  66. int t=;
  67. while(scanf("%d%d",&n,&m)!=EOF&&n+m){
  68. init();
  69. for(int i=;i<m;i++){
  70. scanf("%s",ss);
  71. if(ss[]=='M'){
  72. scanf("%d%d",&a,&b);
  73. unin(rep[a],rep[b]);
  74. }
  75. else{
  76. scanf("%d",&a);
  77. delet(a);
  78. }
  79. }
  80. printf("Case #%d: %d\n",t++,func_cnt());
  81. }
  82. return ;
  83. }

hdu 2473 Junk-Mail Filter (并查集之点的删除)的更多相关文章

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

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

  2. HDU 2473 Junk-Mail Filter(并查集的删除操作)

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

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

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

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

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

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

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

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

  7. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  8. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  9. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

随机推荐

  1. Cheatsheet: 2013 09.10 ~ 09.21

    .NET Lucene.Net – Custom Synonym Analyzer Using FiddlerCore to Capture Streaming Audio Immutable col ...

  2. R语言画图基础参数设置

    Graphical Parameters You can customize many features of your graphs (fonts, colors, axes, titles) th ...

  3. 安全漏洞API接口

    这个是avfisherapi写的API,经常用,每次找他的博客都搜到AV,尴尬..在这里记下来. 0x01 查询最新安全事件和漏洞的接口 接口URL: 乌云网: http://avfisherapi. ...

  4. Hibernate——基础及XML配置

    1.入门 hibernate是跟数据库打交道的,一般跟数据库打交道的都不简单 原始.底层直接的一些操作.编码量比较大.费时.用框架高效 把原来一点一点实现的东西,现在给个半成品,不用在这上边发时间,把 ...

  5. 微博传播数量和传播深度的预测--基于pyspark和某个回归算法

    8-28决定参加一下这个千万条的数据处理任务,因为场景和自己做过的一个回归分析预测差不多,第一天开始在小规模的数据上做准备工作. 第二次大修改版本 date 20160829 星期一¶ 原始数据处理, ...

  6. ZOJ-2366 Weird Dissimilarity 动态规划+贪心

    题意:现给定一个字符集中一共Z个元素的环境,给出一个Z*Z的数组,表示从i到j之间的距离.给定两组字符串,分别问包含着两个字符串(给定的字符串为所求字符串的子序列不是子串)对应位的距离和值最小为多少? ...

  7. 管道寄售库存MRKO结算后,冲销问题

    管道寄售库存MRKO结算后,冲销问题 1.通常使用MIRO对采购订单进行发票校验后,若发现校验错误,直接使用MR8M取消发票校验,同时手工F-03对借发票校验借方GRIR和取消发票校验的贷方GRIR进 ...

  8. mysql概要(九)字符集和校对集

    1.mysql 字符集有细致设置: 2.mysql字符处理机制是:数据库和客户端之间存在一个字符集转换器(后文简称转换器)将客户端字符编码(必须告诉服务端的)转换成一种中间编码的数据(可自定义的但保证 ...

  9. thinkphp分页效果的制作,按查询条件分页正确做法

    PHP代码: <?php namespace Home\Controller; use Think\Controller; use Home\Clas\Cate; class IndexCont ...

  10. Python学习笔记15—mysql的操作

    安装 Python-MySQLdb pip install mysql-Python Python对mysql的操作 建立一个实验数据库demo mysql> create database d ...