hdoj 2473 Junk-Mail Filter 【并查集的删除 】
来源:互联网 发布:win8垃圾清理软件 编辑:程序博客网 时间:2024/06/05 23:44
Junk-Mail Filter
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7063 Accepted Submission(s): 2254
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.
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.
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 6M 0 1M 1 2M 1 3S 1M 1 2S 33 1M 1 20 0
Sample Output
Case #1: 3Case #2: 2
题意:给出n个点和m个操作,M a b 把a和b合并成一个集合,S a 把a从a所在的集合中删除。 问最后还剩几个集合。
并查集的删除题目 ---> 初始化的技巧,为了在删除某个根节点时不影响其它节点,要对每个节点建立一个虚根。
处理:
void init(){for(int i = 0; i < N; i++) set[i] = i + N;for(int i = N; i < MAXN; i++) set[i] = i; num = N + N;}
AC代码:
#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 4000000using namespace std;int set[MAXN];int vis[MAXN];int N, M;int num;int k = 1;void init(){for(int i = 0; i < N; i++) set[i] = i + N;for(int i = N; i < MAXN; i++) set[i] = i; num = N + N;} int find(int p){int t;int child = p;while(p != set[p])p = set[p];while(child != p){t = set[child];set[child] = p;child = t;}return p;} void merge(int x, int y){int fx = find(x);int fy = find(y);if(fx != fy)set[fx] = fy;} void getMap(){int x, y;char op[5];while(M--){scanf("%s", op);switch(op[0]){case 'M': scanf("%d%d", &x, &y); merge(x, y); break;case 'S': scanf("%d", &x); set[x] = num++; }} } void solve(){int ans = 0;memset(vis, 0, sizeof(vis));for(int i = 0; i < N; i++){if(vis[find(i)] == 0)ans++, vis[find(i)] = 1;}printf("Case #%d: %d\n", k++, ans);}int main(){while(scanf("%d%d", &N, &M), N||M){init();getMap();solve();}return 0;}
第二种思路:
用一个数组id[]节点的编号 详细看这里 http://blog.csdn.net/a915800048/article/details/41703865
AC代码:
#include <cstdio>#include <cstring>#define MAXN 3000000+10int set[MAXN]; int id[MAXN];//存储点的编号 int vis[MAXN];int n, m; int num;//编号 int k = 1;void init(){ for(int i = 0; i < n; i++) set[i] = id[i] = i;}int find(int p){ int t; int child = p; while(p != set[p]) p = set[p]; while(child != p) { t = set[child]; set[child] = p; child = t; } return p;}void merge(int x, int y){ int fx = find(x); int fy = find(y); if(fx != fy) set[fx]= fy;}void input(){ int x, y; char op[5]; num = n; while(m--) { scanf("%s", op); switch(op[0]) { case 'M': scanf("%d%d", &x, &y); merge(id[x], id[y]); break; case 'S': scanf("%d", &x); id[x] = num; set[num] = num; num++; } }}void solve(){ int ans = 0; memset(vis, 0, sizeof(vis)); for(int i = 0; i < n; i++) { if(vis[find(id[i])] == 0) { vis[find(id[i])] = 1; ans++; } } printf("Case #%d: %d\n", k++, ans);}int main(){ while(scanf("%d%d", &n, &m), n||m) { init(); input(); solve(); } return 0;}
0 0
- hdoj 2473 Junk-Mail Filter 并查集的删除
- hdoj 2473 Junk-Mail Filter 【并查集的删除 】
- HDU-2473 Junk-Mail Filter 并查集的删除
- Hdu 2473 Junk-Mail Filter (并查集的删除)
- hdu 2473 Junk-Mail Filter (并查集的删除)
- hdu2473 Junk-Mail Filter 并查集的删除
- hdu 2473 Junk-Mail Filter 并查集删除
- HDU 2473 Junk-Mail Filter 并查集 惰性删除
- hdu 2473 Junk-Mail Filter 并查集 删除点
- hdu 2473 Junk-Mail Filter 并查集删除
- HDU 2473-Junk-Mail Filter-并查集删除
- HDU 2473 ( Junk-Mail Filter ) (并查集删除操作)
- HDU 2473 Junk-Mail Filter(并查集删除节点)
- HDOJ 2473 Junk-Mail Filter(并查集 要用到代理的思想)
- hdoj Junk-Mail Filter 2473 (并查集的删点) 好题
- 并查集的删除—hdu 2473 Junk-Mail Filter
- hdu 2473 Junk-Mail Filter(删除节点操作的并查集)
- HDU 2473 Junk-Mail Filter(并查集的删除操作)
- ogre 光源
- LocalThread分析
- 培训管理模块
- C++常用术语及其英文翻译的含义和简单用途总结(三)
- codevs1050
- hdoj 2473 Junk-Mail Filter 【并查集的删除 】
- Effective C++ 学习记录
- Java远程调用RMI(Remote Method Invocation)
- TCP和UDP的“保护消息边界”(粘包、半包)
- JAVA基础----同步锁
- Ruby中的chop和chomp
- Slope one简单的协同过滤算法
- 杭电5285二分图染色和并查集
- 矩形面积并 POJ Atlantis