日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 3926 图的同构

發(fā)布時(shí)間:2025/7/14 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 3926 图的同构 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

23 21 22 33 23 22 13 31 22 33 13 11 2

Sample Output

Case #1: YESCase #2: NO

題意

給你兩個(gè)無向圖,判斷是結(jié)構(gòu)是否相同,每個(gè)點(diǎn)最多兩個(gè)度。

 題解

因?yàn)槊總€(gè)點(diǎn)最多只有兩個(gè)度,所以只會(huì)存在鏈或者環(huán),這樣就容易做了,搜索就行了。特別注意1—>2 ,2->1不能看成一個(gè)環(huán),只是一條鏈,和單獨(dú)的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(); }

轉(zhuǎn)載于:https://www.cnblogs.com/mmmqqdd/p/11183816.html

總結(jié)

以上是生活随笔為你收集整理的HDU 3926 图的同构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。