zoukankan      html  css  js  c++  java
  • *HDU2473 并查集

    Junk-Mail Filter

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


    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个邮件,编号0~n-1,M a b 表示将a,b邮件归为一类,S a 表示将a从所属类中分离出来,问最后有几个分类。
    代码:
     1 //并查集删点,可以将每一个点映射到对应的一个虚拟点上,在这个虚拟点上操作,如果删除某一点就把这个点指向另一个点如:1,2,3 
     2 //在一个并查集中时他们的根节点是1,但是当删去1时被分成了3个集合实际上是2个,我们把他们分别映射到4,5,6 即1-4,2-5,3-6 这样
     3 //根节点就是4,删去1时就把1映射到7这样以4为根节点的这个集合中就只有两个点了.最后统计时找有几个根节点就行。
     4 #include<iostream>
     5 #include<cstdio>
     6 #include<cstring>
     7 using namespace std;
     8 int n,m;
     9 int fat[3000006];    //数组开大一些
    10 int num[3000006];
    11 int find(int x)
    12 {
    13     int rt=x;
    14     while(fat[rt]!=rt)
    15     rt=fat[rt];
    16     int i=x,j;
    17     while(i!=rt)
    18     {
    19         j=fat[i];
    20         fat[i]=rt;
    21         i=j;
    22     }
    23     return rt;
    24 }
    25 void connect(int x,int y)
    26 {
    27     int a=find(x),b=find(y);
    28     if(a!=b)
    29     {
    30         fat[b]=a;
    31     }
    32 }
    33 int main()
    34 {
    35     char ch[2];
    36     int a,b,k=1;
    37     while(scanf("%d%d",&n,&m)&&(n+m))
    38     {
    39         for(int i=0;i<=n;i++)       //映射到虚拟点
    40         fat[i]=i+n;
    41         for(int i=n+1;i<=2*n+m;i++)
    42         fat[i]=i;
    43         int tem=2*n+1;
    44         for(int i=1;i<=m;i++)
    45         {
    46             scanf("%s",ch);
    47             if(ch[0]=='M')
    48             {
    49                 scanf("%d%d",&a,&b);
    50                 connect(a+1,b+1);
    51             }
    52             else
    53             {
    54                 scanf("%d",&a);
    55                 fat[a+1]=tem++;     //指向另一个点
    56             }
    57         }
    58         int ans=0;
    59         memset(num,0,sizeof(num));
    60         for(int i=1;i<=n;i++)       //统计
    61         {
    62             int nu=find(i);
    63             if(num[nu]==0)
    64             {
    65                 num[nu]=1;
    66                 ans++;
    67             }
    68         }
    69         printf("Case #%d: %d
    ",k++,ans);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    网站访问量和服务器带宽的选择
    PHP实现四种基本排序算法
    常用的PHP排序算法以及应用场景
    常见的mysql数据库sql语句的编写和运行结果
    MyBatis拦截器:给参数对象属性赋值
    《自律让你自由》摘要
    Java JDK1.5、1.6、1.7新特性整理(转)
    人人都能做产品经理吗?
    Windows下查询进程、端口
    一语收录(2016-09-18)
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6021931.html
Copyright © 2011-2022 走看看