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
 
大致题意:
  有n封邮件现在要将其含有相同的特征的放在一起,还要删掉一些误判的;
  现在问你这两种操作完成后还有多少种的信。
解题思路:
  主要是并查集的删点操作,建立虚拟节点。
 #include <cstdio>
#include <cstring>
using namespace std;
int f[*],flag[*],x,y,ans,cnt,n,m,k=; char c;
int sf(int x){ return x==f[x]? x:f[x]=sf(f[x]); }
int main(){
while(scanf("%d%d",&n,&m),n+m){
ans=,cnt=*n;
for(int i=;i<n;i++) f[i]=n+i;
for(int i=n;i<*n+m;i++) f[i]=i;
memset(flag,,sizeof(flag));
for(int i=;i<=m;i++){
scanf(" %c",&c);
if(c=='M'){
scanf("%d%d",&x,&y);
f[sf(x)]=sf(y);
} else {
scanf("%d",&x);
f[x]=cnt++;
}
}
for(int i=;i<n;i++)
if(flag[sf(i)]==){
ans++;
flag[sf(i)]=;
} printf("Case #%d: %d\n",k++,ans);
} return ;
}

HDU 2473 - Junk-Mail Filter ,并查集的删点的更多相关文章

  1. hdu4496并查集的删边操作

    题意:       给你一个图,问你删除一些边后还有几个连通快.. 思路:       典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ...

  2. hdu 2473 Junk-Mail Filter (并查集之点的删除)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 单击事件的处理方式及注册窗体的创建之(四)Intent实现界面跳转传值

    跳转开发步骤: 创建Intent对象  设置Intent对象的跳转路径  启动Intent //Register_Activity.java case R.id.btnRegister: Inte ...

  2. JQ选择器

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...

  3. ORACLE 横表与纵表

    一.横表和纵表 横表:通常指我们平时在数据库中建立的表,是一种普通的建表方式.       (主键.字段1.字段2......)如:时间.客户ID,基本通话费.漫游通话费,国内长途费.国际长途费... ...

  4. java基础之 第一步 :jdk安装配置

    Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. window系统安装java 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:http://www.ora ...

  5. CentOS Apache服务器安装与配置

    原文地址:http://www.linuxidc.com/Linux/2014-01/95256.htm 一.安装Apache程序,一般有三种安装方式: Apache在centos下httpd1.直接 ...

  6. Servlet 学习总结-2

    #重定向与转发的区别 开发Web应用中会遇到从一个页面跳转到另一个页面的问题,在JSP中有两种跳转方式: 1.重定向 2.转发(转向) 重定向:首先服务器受到浏览器客户端请求之后,服务器发送新的链接到 ...

  7. Android listview 的应用

    ListView作为Android最常用但是却最难用的控件之一,有很多神奇的用法.我之前也有写过一个例子,稍微不那么简单了一点. [Android原生item的伸缩效果]:http://www.cnb ...

  8. android sdk manager无法更新

    问题描述:       Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示.   解决方法:     第一,我们先修改下hosts文件.该文件的位置在系统盘 ...

  9. 优酷播放器demo

    <!DOCTYPE html> <html lang="en-US"> <head> <meta http-equiv="Con ...

  10. c语言 列出系统进程

    #include <stdio.h> #include "stdafx.h" #include <Windows.h> #include <strin ...