HDU 3926 图的同构
題目鏈接
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
23 21 22 33 23 22 13 31 22 33 13 11 2Sample Output
Case #1: YESCase #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\n",++cas);return;}printf("Case #%d: YES\n",++cas);} int main() { #ifndef ONLINE_JUDGEfreopen("aa.in","r",stdin); #endifint T;read(T);while(T--)work(); }轉載于:https://www.cnblogs.com/mmmqqdd/p/11183816.html
總結
以上是生活随笔為你收集整理的HDU 3926 图的同构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java BlockingQueue 用
- 下一篇: dubbo中使用动态代理