zoukankan      html  css  js  c++  java
  • HDU 3926 图的同构

    Hand in Hand

    题目链接

    http://acm.hdu.edu.cn/showproblem.php?pid=3926

    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
    

    题意

    给你两个无向图,判断是结构是否相同,每个点最多两个度。

    题解

    因为每个点最多只有两个度,所以只会存在链或者环,这样就容易做了,搜索就行了。特别注意1—>2 ,2->1不能看成一个环,只是一条链,和单独的1->2是一样的,

    代码

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define N 10050
    #define M 100050
    struct Edge{int x,y,s;}G1[M],G2[M];
    int last1[N],last2[N];
    template<typename T>void read(T&x)
    {
        ll k=0; char c=getchar();
        x=0;
        while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
        if (c==EOF)exit(0);
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        x=k?-x:x;
    }
    void read_char(char &c)
    {while(!isalpha(c=getchar())&&c!=EOF);}
    int dfs(int x,Edge*G,int *last,int *f)
    {
        f[x]=1;
        int ans=1;
        for(int i=last[x];i;i=G[i].s)
            {
                Edge &e=G[i];
                if (f[e.y])continue;
                ans+=dfs(e.y,G,last,f);
            }
        return ans;
    }
    void init(int &n,int &m,Edge*G,int *last,int *a,int &num)
    {
        static int d[N],f[N];
        int tot=0;
        num=0;
        read(n); read(m);
        memset(f,0,sizeof(int)*(n+1));
        memset(d,0,sizeof(int)*(n+1));
        memset(last,0,sizeof(int)*(n+1));
        for(int i=1;i<=m;i++)
        {
            int x,y;
            read(x); read(y);
            G[++tot]=Edge{x,y,last[x]};
            last[x]=tot;
            G[++tot]=Edge{y,x,last[y]};
            last[y]=tot;
            d[x]++; d[y]++;
           
        }
        for(int i=1;i<=n;i++)
            if (d[i]==1&&!f[i])a[++num]=dfs(i,G,last,f);
        for(int i=1;i<=n;i++)
            if (!f[i]){a[++num]=dfs(i,G,last,f);if (a[num]>2)a[num]+=N;}
    }
    void work()
    {
        static int cas=0,n1,m1,n2,m2,a1[N],a2[N],num1,num2;
        init(n1,m1,G1,last1,a1,num1);
        init(n2,m2,G2,last2,a2,num2);
        sort(a1+1,a1+num1+1);
        sort(a2+1,a2+num2+1);
        for(int i=1;i<=num1;i++)
            if (a1[i]!=a2[i]||num1!=num2){printf("Case #%d: NO
    ",++cas);return;}
        printf("Case #%d: YES
    ",++cas);
            
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("aa.in","r",stdin);
    #endif
        int T;
        read(T);
        while(T--)work();
    }
    
    
  • 相关阅读:
    二叉树知识拓展
    【2014年鄞州区】小幸福(e.pas/c/cpp)
    【2017年常州市】小X与队列 (queue)
    【2018年南海区甲组】扑克游戏(poker)
    【2018年南海区甲组】拆除桥墩(remove)
    【NOIP普及组模拟赛3】投影(skyline)
    常用正则表达式
    asp.net访问母版页控件方法
    C#文件读写操作
    C#读取csv格式文件
  • 原文地址:https://www.cnblogs.com/mmmqqdd/p/11183816.html
Copyright © 2011-2022 走看看