题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2473

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.

题目分析:这个题目是一个典型的考查并查集的题目,主要思路是:遇到'M'则合并集合,遇到S则将 S 后面的 X 结点变成独立的结点!主要的是把父亲结点为X 的结点,修改为父亲结点为自己!!并且把X的父亲结点置为X!

例如:

            

所以我用一个replace数组来标志所有的结点,来处理上述问题。

代码如下:

View Code

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<memory.h>
 4 int set[1100002] , flag[1100002] ;
 5 int replace[100002] ;
 6 int inf = 0 ;
 7 int find_index(int x)  //查 根
 8 {
 9     int r = x , a = x , b ;
10     while(set[r] != r)
11     {
12         r = set[r] ;
13     }
14     while(a != r)
15     {
16         b = set[a] ;
17         set[a] = b ;
18         a = b ;
19     }
20     return r ;
21 }
22
23 void merg(int x , int y)   //并 集合
24 {
25     int fx , fy ;
26     fx = find_index(x) ;
27     fy = find_index(y) ;
28     if(fx > fy)
29         set[fx] = fy ;
30     else
31         set[fy] = fx ;
32 }
33
34 void del(int x)       //分离结点
35 {
36     replace[x] = inf ;
37     set[inf] = inf ;
38     inf++ ;
39 }
40 int main(void)
41 {
42     int n , m , lines = 1 ;
43     while(scanf("%d%d" , &n , &m) != EOF && !(m == 0 && n == 0))
44     {
45         int i , j , x , y , cnt = 0 ;
46         char ch ;
47         for(i = 0 ; i < n ; ++i)
48             set[i] = replace[i] = i ;
49         inf = i ;
50         for(i = 0 ; i < m ; ++i)
51         {
52             getchar() ;
53             scanf("%c" , &ch) ;
54             if(ch == 'M')
55             {
56                 scanf("%d%d" , &x , &y) ;
57                 merg(replace[x] , replace[y]) ;
58             }
59             else
60             {
61                 scanf("%d" , &x) ;
62                 del(x) ;
63             }
64         }
65         memset(flag , 0 , sizeof(flag)) ;
66         for(i = 0 ; i < n ; ++i)
67         {
68             x = find_index(replace[i]) ;
69             if(!flag[x])
70             {
71                 cnt++ ;
72                 flag[x] = 1 ;
73             }
74         }
75         printf("Case #%d: %d\n" , lines++ , cnt) ;
76     }
77     return 0 ;
78 }

转载于:https://www.cnblogs.com/CqlLiliang/archive/2012/07/05/2578320.html

Junk-Mail Filter_并查集(hdu 2473)相关推荐

  1. 带权并查集 HDU - 3047

    题意: 一圈座位有n个,给出m组序号之间的关系,比如,1 2 150 代表2号坐在1号位置序号+150,看m组数据有多少组冲突的. 思路: 带权并查集模板. #include<stdio.h&g ...

  2. 并查集 删除节点 求集合的个数set hdu 2473

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2473 Junk-Mail Filter Time Limit: 15000/8000 MS (Java ...

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

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

  4. *【HDU - 2473】Junk-Mail Filter (并查集--删点操作)

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

  5. hdu 2473(并查集+删除操作)

    解题思路:这道题有并查集的删除操作,如果直接对这一棵树进行删除节点操作肯定是很困难的.所以可以建立虚拟节点,只要有一个节点要被删除,就直接把它投影到虚拟节点上,即用这个虚拟节点来代替我们要删除的节点. ...

  6. 暑期集训5:并查集 线段树 练习题G: HDU - 1754

    2018学校暑期集训第五天--并查集 线段树 练习题G  --   HDU - 1754 I Hate It 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让 ...

  7. 暑期集训5:并查集 线段树 练习题F:  HDU - 1166 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题F  --   HDU - 1166 敌兵布阵 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A ...

  8. 暑期集训5:并查集 线段树 练习题B: HDU - 1213 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题B  --   HDU - 1213 How Many Tables Today is Ignatius' birthday. He invites ...

  9. 暑期集训5:并查集 线段树 练习题A:  HDU - 1232 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题A  --   HDU - 1232 畅通工程 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅 ...

最新文章

  1. pg数据库生成随机时间_postgresql 时区与时间函数-阿里云开发者社区
  2. vue-cli3.0 Typescript 项目集成环信WebIM 群组聊天
  3. boost::process::child相关的测试程序
  4. 简单工厂模式(Simple Factory)
  5. PHP json_decode 对 JSON 格式的字符串进行编码并获取对应的值
  6. eeglab使用与错误解决
  7. ISO 4217 货币(货币符号)及基金编码
  8. Android - 警告:it is always overridden by the value specified in the Gradle build script
  9. #1829 : Tomb Raider(哈希)
  10. 上市只是开端,库客音乐用版权打出组合拳
  11. Centos 安装 glib
  12. 国内如何下载并使用LINE(免费提供apk安装包)
  13. 程序员五一被拉去相亲,结果彻底搞懂了HTTP常用状态码
  14. (二)什么是Reactor模式
  15. 关于全球苹果手机的型号版本介绍
  16. 花2个月面过华为测开岗,拿个30K不过分吧?
  17. Java的Mysql数据库的面试题
  18. 改善Unity编辑器对Lua文件的支持
  19. 36岁转行学java_年纪大了还想转行当程序员,现在学java还来得及吗
  20. 海报创意|十月的节日热点:国庆、重阳和万圣节

热门文章

  1. SSH Secure Shell Client中文乱码的解决办法
  2. 【简讯】ISO确定C++的升级
  3. 不知道能不能赶上用这技术的那一天
  4. 循环 Request.ServerVariables
  5. 不能断点调试python_为Python调试构建一个不间断的断点
  6. 符号标志位.进位标志位_功能标志的成本是多少?
  7. 服务器协议热更_汽车和电话的开放协议,以及更多开放源新闻
  8. CSS3单词及属性大全
  9. 实践 + 理论 | API 接口安全性设计
  10. Bootstrap3 工具提示插件的使用方法