码迷,mamicode.com
首页 > 其他好文 > 详细

Junk-Mail Filter(并差集删点)

时间:2015-10-22 21:08:22      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:

Junk-Mail Filter

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7755    Accepted Submission(s): 2449


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的父节点,这样就把其节点删除了。。。和南阳合纵连横那题一样;注意退出时只退出的是当前那个人,并不是这个子树;例如测试数据:
4 4
M 0 1
M 0 2
M 0 3
S 1
Case #1: 2

代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<stack>
 7 #include<vector>
 8 using namespace std;
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=1e6+10;
11 int pre[MAXN],prt[MAXN],num[MAXN];
12 int find(int x){
13     int r=x;
14     while(r!=pre[r])r=pre[r];
15     pre[x]=r;
16     return r;
17 }
18 void merge(int x,int y){
19     int f1,f2;
20     f1=find(x);f2=find(y);
21     if(f1!=f2){
22         pre[f2]=f1;
23     }
24 }
25 int n;
26 void initial(){
27     for(int i=0;i<=n;i++)pre[i]=i,prt[i]=i;
28     memset(num,0,sizeof(num));
29 }
30 int main(){
31     int m;
32     char s[5];
33     int a,b,flot=0;
34     while(~scanf("%d%d",&n,&m),n|m){
35         initial();
36         int k=n;
37         while(m--){
38         scanf("%s",s);
39         if(s[0]==M){
40             scanf("%d%d",&a,&b);
41             merge(prt[a],prt[b]);
42             }
43         else{
44                 scanf("%d",&a);
45                 prt[a]=k;
46                 pre[k]=k;
47                 k++;
48             }
49         }
50         int cnt=0;
51         for(int i=0;i<n;i++){
52             int t=find(prt[i]);
53             if(!num[t]){
54                 cnt++;
55                 num[t]=1;
56             }
57         }
58         printf("Case #%d: %d\n",++flot,cnt);
59     }
60     return 0;
61 }

合纵连横

时间限制:1000 ms  |           内存限制:65535 KB
难度:3
描述

乱世天下,诸侯割据。每个诸侯王都有一片自己的领土。但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法吞并那些实力弱的,让自己的领土 面积不断扩大。而实力弱的诸侯王为了不让自己的领土被吞并,他会联合一些其他同样弱小的诸侯国,组成联盟(联盟不止一个),来共同抵抗那些强大的诸侯国。 强大的诸侯国为了瓦解这些联盟,派出了最优秀的间谍来离间他们,使一些诸侯国退出联盟。最开始,每个诸侯国是一个联盟。

有两种操作

1、U x y 表示x和y在同一个联盟。(0≤x,y<n)

2、D x   表示x退出联盟。

输入
多组测试数据
第一行两个数,n和m(1 ≤ n≤ 10^5, 1 ≤ m ≤10^5),分别表示诸侯国的个数和操作次数。
接下来有m行操作
输出
输出联盟的个数
样例输入
5 7
U 0 1
U 1 2
U 0 3
D 0
U 1 4
D 2
U 0 2
10 1
U 0 9
 
样例输出
Case #1: 2
Case #2: 9

Junk-Mail Filter(并差集删点)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4902582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!