hdu 2473 Junk-Mail Filter (并查集之点的删除)
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=1010000;
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=0;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=0;
51 memset(tag,0,sizeof(bool)*(num+10));
52 for(int i=0;i<n;i++){
53 int x=fin(rep[i]);
54 if(!tag[x]){
55 res++;
56 tag[x]=1;
57 }
58 }
59 return res;
60 }
61
62 int main()
63 {
64 int a,b;
65 char ss[2];
66 int t=1;
67 while(scanf("%d%d",&n,&m)!=EOF&&n+m){
68 init();
69 for(int i=0;i<m;i++){
70 scanf("%s",ss);
71 if(ss[0]=='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: %dn",t++,func_cnt());
81 }
82 return 0;
83 }
- WCF后续之旅(11): 关于并发、回调的线程关联性(Thread Affinity)
- WCF后续之旅(11): 关于并发、回调的线程关联性(Thread Affinity)
- 解决文本框在updatepanel中得到焦点,输入法不能切换到中文的问题
- 得到真实外网IP、IP所在国家、省份、地区
- 机器学习在智能制造中的应用!
- sql2008 附加数据库时 错误5123
- Logistic Regression Models分析交互式问答译
- 照虎画猫写自己的Spring——依赖注入
- Logistic Regression Models分析交互式问答译
- Asp.Net开发等级星使用(Jquery Rating)
- Enterprise Library Policy Injection Application Block 之四:如何控制CallHandler的执行顺序
- 人工智能时代的艺术
- asp.net生成静态页
- Enterprise Library深入解析与灵活应用(1):通过Unity Extension实现和Policy Injection Application Block的集成
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 预处理命令
- IntelliJ IDEA 调试 Java 8 Stream,实在太香了!
- 仅需四步,写一个 Spring Boot Starter
- 一个简单案例,带你看懂GC日志!
- C#中ref和out的区别使用
- 图解 MySQL 索引,写得实在太好了!
- 10、图像的几何变换——平移、镜像、缩放、旋转、仿射变换 OpenCV2:图像的几何变换,平移、镜像、缩放、旋转(1)OpenCV2:图像的几何变换,平移、镜像、缩放、旋转(2)数字图像
- String、StringBuiler、StringBuffer,谁性能最高?
- 图像的几何变换——平移、镜像、缩放、旋转、仿射变换 OpenCV2:图像的几何变换,平移、镜像、缩放、旋转(1)OpenCV2:图像的几何变换,平移、镜像、缩放、旋转(2)数字图像处理笔
- 详说C#中的结构struct
- c#结构体总结
- 跨域问题(CORS / Access-Control-Allow-Origin)
- C#中的结构体与类的区别
- c#结构体与类的区别,及使用技巧 C#中的结构体与类的区别
- hikvision SDK使用(转)