Mail Filter 【并查集的删除 】

Junk-Mail Filter

Time Limit: 15000/8000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7063Accepted 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, sorelationships (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 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[]节点的编号 详细看这里

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;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

,偶尔会想,如果人生真如一场电子游戏,

Mail Filter 【并查集的删除 】

相关文章:

你感兴趣的文章:

标签云: