原题链接: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 3 3 1
M 1 2 0 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 (并查集之点的删除)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 向强大的SVG迈进

    作者:凹凸曼 - 暖暖 SVG 即 Scalable Vector Graphics 可缩放矢量图形,使用XML格式定义图形. 一.SVG印象 SVG 的应用十分广泛,得益于 SVG 强大的各种特性. ...

  2. Halcon斑点分析BlobAnalysis解析

    斑点分析的算法非常简单:在图像中,相关对象的像素(也称为前景)通过其灰度值来识别.例如,图中示例显示了液体中的组织颗粒.这些粒子是明亮的,液体(背景)是暗的.通过选择明亮的像素(阈值),可以很容易检测 ...

  3. ECSHOP后台左侧添加菜单栏

    比如我们在后台中增加 “活动管理”功能,方法如下 在ECSHOP 管理中心共用语言文件 language\zh_cn\admin\commn.php ,添加我们的自定义菜单: $_LANG['17_a ...

  4. swagger ui demo

    前言 前几天一个朋友公司在用Springboot集合swagger时候总是从浏览器看不了接口,我两找了问题,但是他还是没有找到,于是我就自己从http://start.spring.io/上下载了一个 ...

  5. C#数据结构与算法系列(十八):冒泡排序算法(BubbleSort)

    1.介绍 冒泡排序的基本思想就是:通过对待排序序列从前向后(从下标较小的元素开始),依次比较相邻元素的值,若发现逆序则交换,使值较大的元素逐渐从前移向后部,就像水底的气泡一样逐渐向上冒泡. 因为排序的 ...

  6. Python实用笔记 (14)函数式编程——匿名函数

    当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x)的函数外, ...

  7. IIS 发布页面后或者vs平台运行后显示“未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。”

    一般情况下出现这样的问题是因为.dll文件不存在或者路径不正确. 但今天我遇到的情况都不在这两个内. 我确定.dll文件是存在的,路径也是正确的. 但是程序死活都是“未能加载文件或程序集“xxx”或它 ...

  8. Solaris 11.4安装,映像包管理系统(IPS)搭建

    文章目录 1.下载地址 2. IPS安装准备 2.1 repo包 2.1 install-repo.ksh 2.2 校验文本 3. Solaris系统安装 3.1 虚拟机软件 3.2 安装os 3.3 ...

  9. MyBatis执行流程的各阶段介绍

    目录 一.mybatis极简示例 1.1 创建mybatis配置文件 1.2 创建数据库表 1.3 创建javabean 1.4 创建mapper映射文件 1.5 运行测试 二.mybatis的几大“ ...

  10. 每日一题 - 剑指 Offer 44. 数字序列中某一位的数字

    题目信息 时间: 2019-07-01 题目链接:Leetcode tag: 规律 难易程度:中等 题目描述: 数字以0123456789101112131415-的格式序列化到一个字符序列中.在这个 ...