原题链接:https://vjudge.net/problem/11782/origin

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 ≤ 10 5 , 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 33 1
M 1 20 0

Sample Output

Case #1: 3
Case #2: 2

题意:有n个点,M a,b代表a,b是一个集合,S a代表从集合中删除a点,经过m个指令后,最后输出有几个集合。

题解:本题是学习并查集的好题,题意很简单(hdu和codeforces的题意比uva上的好懂多了。。。),M指令相当于并查集中的集合合并函数,S指令需要自己补充函数,此题关键是初始化时不能像并查集那样将自己设为父结点,而是应该设置虚拟父结点。话不多说,AC代码如下:

#include <queue>
#include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <string>
#include <sstream>
#include <string.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#define fast                                ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll                                  long long
#define _for(i,a,b)                         for(int i = a;i < b;i++)
#define rep(i,a,b)                          for(int i = a;i <= b;i++)
#define all(s)                              s.begin(), s.end()const int maxn = 1e5 + 100;
const int maxm = 1e6 + 100;
int n, m,ans=0,flag,a,b;int par[2*maxn+maxm];
//初始化时虚拟父结点为n个
//S删除指令后,被删除的元素需要重新获得一个虚拟父结点(最多有1e6个)char cmd[2];int find_(int x) { // 寻找树的根if (par[x] == x) {return x;}else {return par[x] = find_(par[x]); // 直接连向祖先结点, 查找的时候可以省时间}
}
void unite(int x, int y)
{ // 合并 x 和 y 所在的集合x = find_(x); // 寻找祖先y = find_(y); // 寻找祖先if (x == y) return;else par[y] = x;
}
void del(int x)
{par[x] = flag++;
}int main()
{int kase = 1;while ((scanf("%d%d", &n, &m) == 2) && n){_for(i, 0, n) par[i] = i + n;//虚拟父结点_for(i, n, n + n + m)par[i] = i;//初始化虚拟父结点flag = n + n;//用于S后的虚拟父结点的赋值while (m--){scanf("%s", cmd);if (cmd[0] == 'M')//合并{scanf("%d%d", &a, &b);unite(a, b);}else//删除{scanf("%d", &a);del(a);}}map<int, int>vis;ans = 0;_for(i, 0, n)//遍历{int x = find_(i);if (!vis.count(x))//新集合{ans++;vis[x]++;}}printf("Case #%d: %d\n", kase++, ans);}return 0;
}

HDU-2473 Junk-Mail Filter(并查集的使用)相关推荐

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

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

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

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

  3. How Many Answers Are Wrong HDU - 3038(带权并查集经典题,满满的都是注释)

    How Many Answers Are Wrong HDU - 3038  点击打开链接 题意:现在有n个数(你并不知道这n个数是什么),m次查询,每次查询给出u,v,w.表示从第u个数到第v个数的 ...

  4. hdu 1232 畅通工程 最小生成树 并查集

    1232的连接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 #include <iostream>#include <cstdio& ...

  5. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  6. hdu 3234 Exclusive-OR 题解(并查集,思维)

    该死的期末复习终于结束了... 暑假来了\color{#ff0000}{暑假来了}暑假来了!!! 所以我就珂以非常开心的写博客了. 原题链接: hdu 题意简述 多组数据.你有一个没有确定的数列.有一 ...

  7. HDU 1272 小希的迷宫 (并查集)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  8. hdu 1811 Rank of Tetris (并查集+拓扑排序)

    Problem - 1811 感觉这题的并查集以及拓扑排序并不难,但是做题的时候必须理解到矛盾(CONFLICT)与不确定(UNCERTAIN)直接的优先关系. 做这题的时候,构图什么的很简单,就是没 ...

  9. hdu 3047 Zjnu Stadium(并查集)

    题意: 300个座位构成一个圈. 有N个人要入座. 共有M个说明 :A B X ,代表B坐在A顺时针方向第X个座位上.如果这个说明和之前的起冲突,则它是无效的. 问总共有多少个无效的. 思路: 并查集 ...

  10. HDU - 2874 Connections between cities(并查集+LCA)

    题目链接:点击查看 题目大意:给出n个点代表城市,再给出m条边将其连接,每条边都有边权,题目保证给出的图无环,现在给出两个点,首先询问两个点是否互相连通,若可以连通,询问两点之间的距离 题目分析:判断 ...

最新文章

  1. 警惕!Cisco产品的假冒和水货
  2. php 数据显示,数据显示处理,该怎么处理
  3. ios7 uuid的获取方法
  4. Acwing 1085. 不要62
  5. node 安装 webpack
  6. java字符串反转及替换_字符串的替换(str_replace)
  7. ios UIlabel
  8. 农村这么好,为什么感觉大家都不想在农村生活呢?
  9. Android开发学习之录音同步播放的实现
  10. 千万级用户直播APP——服务端结构设计和思考
  11. linux|计划任务
  12. 获取git的当前分支名称
  13. 分享个三国志2017挂机脚本 可玩性很高占用小
  14. Leetcode之Teemo Attacking 问题
  15. python拦截广告弹窗_电脑总是有弹窗广告,真的烦人,一招教你解决!
  16. 谈谈我职业生涯中的三次潦倒 Leo病中的思考 续
  17. bzoj1052 覆盖问题 二分答案 dfs
  18. SQL学习(五):lastday函数(返回指定日期所在月份的最后一天)
  19. git pull (merge远程分支到本地)
  20. 【深度学习篇】---CNN和RNN结合与对比,实例讲解

热门文章

  1. python能做什么程序-Python可以被用来做哪些神奇好玩的事情
  2. 自学python好找工作么-非计算机专业自学Python好找工作吗?
  3. python免费教程视频-微软推出 Python 免费在线教程视频
  4. 零基础学python图文版-教到你会为止的Python入门课程即将开班
  5. python趣味编程100例-Python趣味编程与精彩实例,码高少儿编程 编
  6. python爬虫菜鸟教程-Python爬虫学习100练001
  7. 小颗粒积木步骤图纸_【小颗粒积木赛回顾】参与者图鉴,中华街带来耐心与热情...
  8. python老师 课时费_花10分钟写一个Python脚本,搞定了初中老师一下午的工作
  9. Error: ER_ACCESS_DENIED_ERROR: Access denied for user ‘root‘@‘localhost‘ (using password: YES)解决办法
  10. 运筹学状态转移方程例子_逆转的薛定谔方程,美俄科学家实现量子时间“倒流”,令人兴奋...