zoukankan      html  css  js  c++  java
  • HDU3926Hand in Hand(搜索 或 并查集)

    Problem Description
    In order to get rid of Conan, Kaitou KID disguises himself as a teacher in the kindergarten. He knows kids love games and works out a new game called "hand in hand".

    Initially kids run on the playground randomly. When Kid says "stop", kids catch others' hands immediately. One hand can catch any other hand randomly. It's weird to have more than two hands get together so one hand grabs at most one other hand. After kids stop moving they form a graph.

    Everybody takes a look at the graph and repeat the above steps again to form another graph. Now Kid has a question for his kids: "Are the two graph isomorphism?"
     

    Input
    The first line contains a single positive integer T( T <= 100 ), indicating the number of datasets.
    There are two graphs in each case, for each graph:
    first line contains N( 1 <= N <= 10^4 ) and M indicating the number of kids and connections.
    the next M lines each have two integers u and v indicating kid u and v are "hand in hand".
    You can assume each kid only has two hands.
     

    Output
    For each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise.
     

    Sample Input
    2 3 2 1 2 2 3 3 2 3 2 2 1 3 3 1 2 2 3 3 1 3 1 1 2
     

    Sample Output
    Case #1: YES Case #2: NO
     

    Source
     搜索:
    #include<stdio.h>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    const int N = 10100;
    struct node
    {
        int sum,cyc;
    };
    
    node sta1[N],sta2[N];
    int vist[N];
    vector<int>tmap[N];
    
    bool cmp(const node &g1, const node &g2)
    {
        if(g1.sum < g2.sum)
            return true;
        else if(g1.sum == g2.sum && g1.cyc < g2.cyc)
            return true;
        else
            return false;
    }
    
    void addedg(int a,int b)
    {
        int flag=0;
        for(int i=0;i<tmap[a].size();i++)
            if(b==tmap[a][i])
        {
            flag=1; break;
        }
        if(flag==0)
            tmap[a].push_back(b),tmap[b].push_back(a);
    }
    void init(int n)
    {
        for(int i=0;i<=n;i++)
            {
                vist[i]=0; tmap[i].clear();
            }
    }
    void DFS(int x,int fath,node &ss)
    {
        ss.sum++; vist[x]=1;
        for(int i=0;i<tmap[x].size();i++)
        {
            if(fath==tmap[x][i])continue;
            if(vist[tmap[x][i]])
            {
                ss.cyc=1; continue;
            }
            DFS(tmap[x][i],x,ss);
        }
    }
    
    int main()
    {
        int t,a,b,n[2],m[2],c=0,flag;
        scanf("%d",&t);
        while(t--)
        {
            c++; flag=1;
            scanf("%d%d",&n[0],&m[0]);
    
            init(n[0]);
            for(int i=1;i<=m[0];i++)
            {
                scanf("%d%d",&a,&b);
                addedg(a,b);
            }
    
            int k1=0;
            for(int i=1;i<=n[0];i++)
            if(vist[i]==0)
            {
                sta1[k1].cyc=sta1[k1].sum=0;
                DFS(i,-1,sta1[k1]);
                k1++;
            }
    
            scanf("%d%d",&n[1],&m[1]);
            if(n[1]!=n[0])
                flag=0;
            init(n[1]);
            for(int i=1;i<=m[1];i++)
            {
                scanf("%d%d",&a,&b);
                if(flag)
                addedg(a,b);
            }
            if(flag)
            {
                int k2=0;
                for(int i=1;i<=n[1];i++)
                if(vist[i]==0)
                {
                    sta2[k2].cyc=sta2[k2].sum=0;
                   DFS(i,-1,sta2[k2]); k2++;
                }
    
                if(k1!=k2)
                flag=0;
    
                sort(sta1,sta1+k1,cmp);
                sort(sta2,sta2+k2,cmp);
                for(int i=0;i<k1;i++)
                if(sta1[i].sum!=sta2[i].sum||sta1[i].cyc!=sta2[i].cyc)
                {
                    flag=0; break;
                }
            }
            if(flag)
                printf("Case #%d: YES
    ",c);
            else
                printf("Case #%d: NO
    ",c);
        }
    }
    


    并查集:
    #include<stdio.h>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    const int N = 10100;
    struct node
    {
        int sum,cyc;
    };
    
    node sta1[N],sta2[N];
    int fath[N],cyc[N],sum[N];
    
    bool cmp(const node &g1, const node &g2)
    {
        if(g1.sum < g2.sum)
            return true;
        else if(g1.sum == g2.sum && g1.cyc < g2.cyc)
            return true;
        else
            return false;
    }
    
    int findfath(int x)
    {
        if(x==fath[x])
            return fath[x];
        fath[x]=findfath(fath[x]);
        return fath[x];
    }
    void setfath(int x,int y)
    {
        x=findfath(x);
        y=findfath(y);
        if(x==y)
            cyc[x]=1;
        else
            fath[x]=y,sum[y]+=sum[x];
    }
    void init(int n)
    {
        for(int i=0;i<=n;i++)
            {
                cyc[i]=0;
                sum[i]=1; fath[i]=i;
            }
    }
    
    int main()
    {
        int t,a,b,n[2],m[2],c=0,flag;
        scanf("%d",&t);
        while(t--)
        {
            c++; flag=1;
            scanf("%d%d",&n[0],&m[0]);
    
            init(n[0]);
            for(int i=1;i<=m[0];i++)
            {
                scanf("%d%d",&a,&b);
                setfath(a,b);
            }
    
            int k1=0;
            for(int i=1;i<=n[0];i++)
            if(fath[i]==i)
            {
                k1++;
               sta1[k1].sum=sum[i];
               sta1[k1].cyc=cyc[i];
            }
    
            scanf("%d%d",&n[1],&m[1]);
            if(n[1]!=n[0])
                flag=0;
            init(n[1]);
            for(int i=1;i<=m[1];i++)
            {
                scanf("%d%d",&a,&b);
                if(flag)
                setfath(a,b);
            }
            if(flag)
            {
                int k2=0;
                for(int i=1;i<=n[1];i++)
                if(fath[i]==i)
                {
                    k2++;
                   sta2[k2].sum=sum[i];
                   sta2[k2].cyc=cyc[i];
                }
    
                if(k1!=k2)
                flag=0;
    
                sort(sta1+1,sta1+k1+1,cmp);
                sort(sta2+1,sta2+k2+1,cmp);
                for(int i=1;i<=k1;i++)
                if(sta1[i].sum!=sta2[i].sum||sta1[i].cyc!=sta2[i].cyc)
                {
                    flag=0; break;
                }
            }
    
            printf("Case #%d: %s
    ",c,flag>0?"YES":"NO");
        }
    }
    


  • 相关阅读:
    深入入门正则表达式(java) 匹配原理 2 回溯
    java实现sftp实例
    自定义注解
    java构造器
    Does the parameter type of the setter match the return type of the getter?
    JAVA经典算法40题(18)
    关于session的详细解释
    .net上传功能fileupload代码
    ContentUris类使用介绍
    java回顾之类初级
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6811143.html
Copyright © 2011-2022 走看看