Ninth HDU 3926 hand in multiple schools (a special picture homogeneous problem)

Source: Internet
Author: User

The condition that the degree of vertices is less than or equal to 2 is given, that is, the graph can only be a graph composed of rings and links. The vertices are included in the chain, so that you only need to repeat each connected component, record whether they are loops, links, and the number of points, and then sort and compare them.

#include <cstdio>#include <algorithm>using namespace std;const int inf =0x3fffffff;const int maxn=10005;struct graph{     int chain[maxn],ring[maxn];     int chain_num,ring_num;}g1,g2;struct Edge {    int v,next;}edge[2*maxn];int head[maxn],cnt;bool vis[maxn];void addedge (int u,int v){     edge[cnt].v=v;     edge[cnt].next=head[u];     head[u]=cnt++;     edge[cnt].v=u;     edge[cnt].next=head[v];     head[v]=cnt++;}bool is_same(){     sort (g1.chain,g1.chain+g1.chain_num);     sort (g2.chain,g2.chain+g2.chain_num);     for (int i=0 ; i<g1.chain_num ; ++i)     if(g1.chain[i]!=g2.chain[i])return false;          sort (g1.ring,g1.ring+g1.ring_num);     sort (g2.ring,g2.ring+g2.ring_num);     for (int i=0 ; i<g1.ring_num ; ++i)     if(g1.ring[i]!=g2.ring[i])return false;     return true;}void init (graph &g){     memset (g.chain , 125 , sizeof(g.chain));     memset (g.ring , 125 , sizeof(g.ring));     g.chain_num=g.ring_num=0;     memset (head , -1 , sizeof(head));     memset (vis , 0 , sizeof(vis));     cnt=0;}bool is_ring;void dfs (int u,int &num,int fath){     vis[u]=true; num++;     for (int p=head[u]; ~p ; p=edge[p].next)     {         int v=edge[p].v;         if(v==fath)continue;         if(vis[v])is_ring=true;         if (!vis[v])         {              dfs(v,num,u);         }     }}int m,n,u,v,i,j;void solve(graph &g){     init(g);     scanf("%d%d",&n,&m);     for (i=0 ; i<m ; ++i)     {         scanf("%d%d",&u,&v);         u--,v--;         addedge(u,v);     }     int count;     for (i=0 ; i<n ; ++i)     {         if (!vis[i])         {               count=1;               is_ring=false;               dfs(i,count,inf);               if(is_ring)g.ring[g.ring_num++]=count;               else g.chain[g.chain_num++]=count;         }     }}int main (){    int cas;    //freopen ("G.txt","r",stdin);    //freopen ("out.txt","w",stdout);    scanf("%d",&cas);    for (int I=1 ; I<=cas ; ++I)    {        solve(g1);        solve(g2);        printf("Case #%d: %s\n",I,is_same()?"YES":"NO");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.