首页 > ACM题库 > HDU-杭电 > hdu 2473 Junk-Mail Filter-并查集-[解题报告]C++
2014
01-26

hdu 2473 Junk-Mail Filter-并查集-[解题报告]C++

Junk-Mail Filter

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

Case #1: 3
Case #2: 2

思路:每次合并时,都是给一个虚拟的根。

#include<map>
#include<Set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 100010
#define Maxm 2000010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x3f3f3f3f
#define Mod 1000000007
using namespace std;
int Set[Maxn*2],cnt,vi[Maxn*2],n;
void init()
{
    for(int i=0;i<Maxn*2;i++)
        Set[i]=i;
    memset(vi,0,sizeof(vi));
    cnt=n;
}
int Find(int x)
{
    if(Set[x]!=x)
        Set[x]=Find(Set[x]);
    return Set[x];
}
void merg(int a,int b)
{
    int x,y;
    x=Find(a);
    y=Find(b);
    if(Set[x]==a&&Set[y]==b){
        Set[x]=++cnt,Set[y]=cnt;
        return ;
    }
    if(Set[x]==Set[y])
        return ;
    if(x<=n)
        Set[x]=y;
    else
        Set[y]=x;
}
int main()
{
    int m,i,j,u,v,Case=0;
    char str[3];
    while(scanf("%d%d",&n,&m),n||m){
        init();
        for(i=1;i<=m;i++){
            scanf("%s",str);
            if(str[0]=='M'){
                scanf("%d%d",&u,&v);
                merg(u,v);
            }
            else{
                scanf("%d",&u);
                Set[u]=u;
            }
        }
        int ans=0;
        for(i=0;i<n;i++){
            u=Find(i);
            if(!vi[u]){
                ans++;
                vi[u]=1;
            }
        }
        printf("Case #%d: %d\n",++Case,ans);
    }
    return 0;
}

 

解题转自:http://www.cnblogs.com/wangfang20/archive/2013/08/29/3290166.html


  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  4. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge