The Necklace UVA - 10054 (无向图的欧拉回路)

The Necklace

UVA - 10054

题意:每个珠子有两个颜色,给n个珠子,问能不能连成一个项链,使得项链相邻的珠子颜色相同。

把颜色看做点,珠子内部连一条边,无向图求欧拉回路。

这里我用的并查集。

输出路径就dfs就行了

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int g[55][55];
 4 int f[55];
 5 int deg[55];
 6 int n;
 7
 8 int gf(int x)
 9 {
10   return x==f[x]?x:f[x]=gf(f[x]);
11 }
12
13 void dfs(int u)
14 {
15     for(int i=1;i<=50;i++) if(g[u][i])
16     {
17         g[u][i]--;
18         g[i][u]--;
19         dfs(i);
20         printf("%d %d\n",i,u);
21     }
22 }
23 int main()
24 {
25     int t;
26     int kase=0;
27     scanf("%d",&t);
28     while(t--)
29     {
30         for(int i=0;i<=55;i++) f[i]=i;
31         memset(deg,0,sizeof(deg));
32         memset(g,0,sizeof(g));
33         scanf("%d",&n);
34         int u,v;
35         for(int i=0;i<n;i++)
36         {
37             scanf("%d%d",&u,&v);
38             g[u][v]++;
39             g[v][u]++;
40             deg[u]++;
41             deg[v]++;
42             int pu=gf(u);
43             int pv=gf(v);
44             if(pu!=pv) f[pu]=pv;
45         }
46         if(kase) puts("");
47         printf("Case #%d\n",++kase);
48         int rt=0;
49         int flag=1;
50         for(int i=1;i<=50;i++) if(deg[i])
51         {
52             if(deg[i]&1) {
53                 flag=0;
54                 break;
55             }
56             if(rt==0) rt=gf(i);
57             else{
58                 int v=gf(i);
59                 if(v!=rt) {
60                     flag=0;
61                     break;
62                 }
63             }
64         }
65         if(flag)
66         {
67             dfs(rt);
68         }
69         else puts("some beads may be lost");
70     }
71 }

时间: 2024-08-14 11:59:57

The Necklace UVA - 10054 (无向图的欧拉回路)的相关文章

The Necklace UVA 10054 (无向图的欧拉回路,求证Flury算法)

说说:题目的意思本质上就是给你N条无向边,若存在欧拉回路,则将其生成.无向图的欧拉回路的判断非常容易,只要判断是否每个节点都是偶数度即可.但是,对欧拉回路的生成,也就是Fleury算法,貌似有点问题.我自己在这个地方也纠结了好久.下面就来讲讲Fleury算法. 开始我觉得,就是个非常简单的深度优先搜索的问题,直接从任意一个节点,然后不断DFS即可.所以就有了如下的代码: for(i=1;i<MAX;i++) if(map[m][i]>0){ map[m][i]--; map[i][m]--;

UVA - 10054 - The Necklace (欧拉回路!!)

UVA - 10054 The Necklace Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Problem D: The Necklace  My little sister had a beautiful necklace made of colorful beads. Two successive beads in the neckl

uva 10054 The Necklace 欧拉回路

// uva 10054 The Necklace 欧拉回路 // 以颜色为节点,两种颜色互相连着有向边,然后跑一边欧拉回路就ok了 // 这题套了余老师模板书上的欧拉回路,然后就过了 // // 通过这题我了解到了,欧拉回路的基本思想 // 哎,继续练吧... #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat>

UVa10054 The Necklace,无向图求欧拉回路

无向图求欧拉回路: 1.图连通 2.所有顶点的度数位偶数 随便从一个点开始递归遍历即可求出路径 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxcolor = 50; int n, G[maxcolor+1][maxcolor+1], deg[maxcolor+1]; struct Edge{ int from, to; Edge(int f

Uva(10129)+Uva(10054)

这两道题都是和欧拉图的判定有关,一个是有向图,一个是无向图的欧拉图的判定还有一个是有向图= = 先看10129...我们把单词的首字母看做是入度,最后一个字符看做是初度,那么这道题就变成图中是否存在欧拉回路.判断有向图的条件是该图是连通图和最多最有两个点的出度不等于入度,其实那两个点就是起点和终点,而且必须是其中一个点的入度比出度恰好大1(作为终点),另一个点的出度比它的入度恰好大1(作为起点) 连通图的话我们用并查集来判断,如果该图是连通图的话,那么他们共用祖先= = #include<std

UVA 10054(DFS_G题)解题报告

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=995 ------------------------------------------------------------------------------------------------------------------------------------------

poj1041 John&#39;s trip,无向图求欧拉回路路径

点击打开链接 无向图求欧拉回路: 1.图连通 2.所有顶点的度数位偶数 #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <algorithm> using namespace std; const int mt = 2000; const int ms = 50; bool vis[mt+5]; int g[ms][mt+5]; int

poj2513Colored Sticks(无向图的欧拉回路)

1 /* 2 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 3 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 4 5 并查集判通 + 奇度节点个数等于2或者0 6 */ 7 #include<cstring> 8 #include<cstdio> 9 #include<algorithm> 10 #define N 2500005*2 11 using namespace std; 12 13 int f[N]; 1

poj1041 John&#39;s trip (无向图求欧拉回路方案)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5950   Accepted: 1946   Special Judge Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his frien