zoukankan      html  css  js  c++  java
  • HDU 2473 Junk-Mail Filter (并查集节点删除)

    Junk-Mail Filter

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


    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个点,可以对点进行两种操作,M u v是将点u,v所在的两个集合合并,S u是将点u从其所在的区间内删除,问最后有多少个集合
    分析:如果是将两个点所在的集合合并那可以做到,但问题是怎么将这个节点从集合内删除?
    可能会想到把根节点指向自己,但是怎么去处理和其他点的关系呢?所以这么做行不通
    传统并查集在支持合并的基础上并没有进行将某一个节点从集合内删除的操作,所以要另外想办法,怎么做呢?我们可以设置虚拟节点。
    意思就是说如果有一点被删除,可以设置一个新的节点代替它
    例如:有4个点{1,2,3,4} 有两个集合A={1,2,3} B={4},如果把A集合的3从集合内除去,那么我们就可以新设置一个节点5来代替节点3,于是就得到A={1,2,3} B={4}
    C={5} 以后对于"点3"的操作都对点5进行
    用vir[i]数组表示编号为i的节点的实际编号为vir[i],那么Union(vir[u],vir[v])就是把点u所指的点vir[u]和点v所指的点vir[v]进行合并操作,del(u)就是“删除” 集合中的点u,相对来说相当于生成了一个新的节点。
    最后集合的个数就是根节点的个数。
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<vector>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=1e6+1e5+5;
    int p[MAXN];
    int vis[MAXN];
    int vir[MAXN]; //存储虚拟点的编号
    int n,m,cnt;
    int init()
    {
        for(int i=0;i<=MAXN;i++) p[i]=i;
        for(int i=0;i<n;i++) vir[i]=i;
        memset(vis,0,sizeof(vis));
        cnt=n;
    }
    int findfa(int x)
    {
        return p[x]==x?x:p[x]=findfa(p[x]);
    }
    void Union(int u,int v)
    {
        int x=findfa(u);
        int y=findfa(v);
        if(x!=y)
            p[x]=y;
    }
    void del(int u)
    {
        vir[u]=cnt++;
    }
    int main()
    {
        freopen("in.txt","r",stdin);
        int Case=0;
        while(scanf("%d %d",&n,&m) && (n||m))
        {
            init();
            while(m--)
            {
                char opt[10];
                scanf("%s",opt);
                if(opt[0]=='M')
                {
                    int u,v;
                    scanf("%d %d",&u,&v);
                    Union(vir[u],vir[v]);
                }
                else
                {
                    int u;
                    scanf("%d",&u);
                    del(u);
                }
            }
    
            for(int i=0;i<n;i++)
                vis[findfa(vir[i])]=1;
    
            int ans=0;
            for(int i=0;i<cnt;i++)
                if(vis[i])
                    ans++;
            printf("Case #%d: %d
    ",++Case,ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    K8S 本地 配置 Local PV 实践
    Filebeat 收集K8S 日志,生产环境实践
    K8S 上部署 Redis-cluster 三主三从 集群
    Kubernetes 用户流量接入方案
    给Nginx配置日志格式和调整日期格式
    唇亡齿寒,运维与安全
    Nginx记录用户请求Header到access log
    Kubernetes中利用Kubectl set 让Deployment更新镜像
    故障管理:故障定级和定责
    使用 Elastic 技术栈构建 Kubernetes全栈监控
  • 原文地址:https://www.cnblogs.com/clliff/p/4694255.html
Copyright © 2011-2022 走看看