定义:
欧拉回路:每条边恰好只走一次,并能回到出发点的路径
欧拉路径:经过每一条边一次,但是不要求回到起始点

①首先看欧拉回路存在性的判定:

一、无向图
每个顶点的度数都是偶数,则存在欧拉回路。

二、有向图(所有边都是单向的)
每个节顶点的入度都等于出度,则存在欧拉回路。

②.欧拉路径存在性的判定

一。无向图
一个无向图存在欧拉路径,当且仅当   该图所有顶点的度数为偶数   或者  除了两个度数为奇数外其余的全是偶数。

二。有向图
一个有向图存在欧拉路径,当且仅当  该图所有顶点的入度等于出度 或者 一个点出度比入度多一(起点) 一个点入度比出度多一(终点) 其他都为入度等于出度。

在已经知道存在的情况下,下面的程序用于输出路径;

如果是输出欧拉道路,那么参数必须是道路的起点; 另外打印的顺序是逆序的,因此真正使用时候,用push存入(u,v);

void euler(int u){

for(Int v = 0; v < n; v++){

if(G[u][v]&&!vis[u][v]){

vis[u][v] = vis[v][u]=1;
          euler(v);
          printf("%d %d\n",u,v);

}

}

}

上面的代码只适用于无向图,改成有向图只需要把vis[u][v] =vis[v][u] = 1 改成 vis[u][v]即可;

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve
it to open that doors. Because there is no other way to open the doors, the puzzle is very important
for us.
There is a large number of magnetic plates on every door. Every plate has one word written on
it. The plates must be arranged into a sequence in such a way that every word begins with the same
letter as the previous word ends. For example, the word `
acm
' can be followed by the word `
m
otorola
'.
Your task is to write a computer program that will read the list of words and determine whether it
is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to
open the door.
Input
The input consists of
T
test cases. The number of them (
T
) is given on the rst line of the input le.
Each test case begins with a line containing a single integer number
N
that indicates the number of
plates (1
N
100000). Then exactly
N
lines follow, each containing a single word. Each word
contains at least two and at most 1000 lowercase characters, that means only letters `
a
' through `
z
' will
appear in the word. The same word may appear several times in the list.
Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that
the rst letter of each word is equal to the last letter of the previous word. All the plates from the
list must be used, each exactly once. The words mentioned several times must be used that number of
times.
If there exists such an ordering of plates, your program should print the sentence `
Ordering is
possible.
'. Otherwise, output the sentence `
The door cannot be opened.
'
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
 
 
  1. /**
  2. 题目:Play on Words UVA - 10129
  3. 链接:https://vjudge.net/problem/UVA-10129
  4. 题意:给定n个单词,排成一行,使单词的首部字母和上一个单词(如果存在)的尾部字母相同。问是否存在这样的一行序列满足要求。
  5.  
  6. 思路:
  7. 首先保证不考虑方向的情况下图是连通的。
  8.  
  9. 判断有向图是否存在欧拉路径。
  10. 1,所有的点入度等于出度。
  11. 2,一个点入度比出度多一,一个点出度比入度多一,其他点入度等于出度。
  12.  
  13. 把每一个word的首字母和尾字母连一条有向的边。
  14. */
  15.  
  16. #include <iostream>
  17. #include <cstdio>
  18. #include <vector>
  19. #include <cstring>
  20. #include <cmath>
  21. #include <algorithm>
  22. using namespace std;
  23. typedef long long LL;
  24. const int mod=1e9+;
  25. const int maxn=1e2+;
  26. const double eps = 1e-;
  27. int T, n;
  28. char s[];
  29. int in[], ot[];
  30. int st[];
  31. int vis[];
  32. int Find(int x)
  33. {
  34. if(x==st[x]) return x;
  35. return st[x] = Find(st[x]);
  36. }
  37. void Merge(int x,int y)
  38. {
  39. int fx = Find(x);
  40. int fy = Find(y);
  41. if(fx>fy){
  42. st[fx] = fy;
  43. }else st[fy] = fx;
  44. }
  45. int main()
  46. {
  47. cin>>T;
  48. while(T--)
  49. {
  50. scanf("%d",&n);
  51. memset(in, , sizeof in);
  52. memset(ot, , sizeof ot);
  53. memset(vis, , sizeof vis);
  54. for(int i = ; i < ; i++) st[i] = i;
  55. for(int i = ; i < n; i++){
  56. scanf("%s",s);
  57. int len = strlen(s);
  58. vis[s[]-'a'] = vis[s[len-]-'a'] = ;
  59. Merge(s[]-'a',s[len-]-'a');
  60. ot[s[]-'a']++;
  61. in[s[len-]-'a']++;
  62. }
  63.  
  64. int r;
  65. for(int i = ; i < ; i++){
  66. if(vis[i]){
  67. r = Find(i);
  68. }
  69. }
  70. int sign = ;
  71. for(int i = ; i < ; i++){
  72. if(vis[i]){
  73. if(Find(i)!=r){
  74. sign = ; break;
  75. }
  76. }
  77. }
  78. if(sign){///不考虑方向,没有连通。
  79. printf("The door cannot be opened.\n"); continue;
  80. }
  81.  
  82. int flag1, flag2, flag3;
  83. flag1 = flag2 = flag3 = ;///分别表示入度等于出度的点数,入度比出度多一的点数,出度比入度多一的点数。
  84. for(int i = ; i < ; i++){
  85. if(in[i]==ot[i]){
  86. flag1++;
  87. }
  88. if(in[i]-ot[i]==){
  89. flag2++;
  90. }
  91. if(ot[i]-in[i]==){
  92. flag3++;
  93. }
  94. }
  95. if(flag1==||((flag1==)&&(flag2==)&&(flag3==))){
  96. printf("Ordering is possible.\n");
  97. }else
  98. printf("The door cannot be opened.\n");
  99. }
  100. return ;
  101. }

Play on Words UVA - 10129 欧拉路径的更多相关文章

  1. UVa 10129 (并查集 + 欧拉路径) Play on Words

    题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...

  2. UVa 10129 Play on Words(有向图欧拉路径)

    Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to s ...

  3. UVa 10129 Play on Words(并查集+欧拉路径)

    题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...

  4. Uva 10129 单词

    题目链接:https://uva.onlinejudge.org/external/101/10129.pdf 把单词的首字母和最后一个字母看做节点,一个单词就是一个有向边.有向图的欧拉定理,就是除了 ...

  5. UVa 10129单词(欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. uva 10129 play on words——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABNUAAANeCAYAAAA1BjiHAAAgAElEQVR4nOydabWsuhaFywIasIAHJK

  7. Uva 10129 - Play on Words 单词接龙 欧拉道路应用

    跟Uva 10054很像,不过这题的单词是不能反向的,所以是有向图,判断欧拉道路. 关于欧拉道路(from Titanium大神): 判断有向图是否有欧拉路 1.判断有向图的基图(即有向图转化为无向图 ...

  8. 单词 (Play on Words UVA - 10129 )

    题目描述: 原题:https://vjudge.net/problem/UVA-10129 题目思路: 1.明显是判断欧拉路径 2.欧拉路径的两个条件 a.图连通 b.至多为两个奇点,且一个为起点一个 ...

  9. UVa 10129 Play On Words【欧拉道路 并查集 】

    题意:给出n个单词,问这n个单词能否首尾接龙,即能否构成欧拉道路 按照紫书上的思路:用并查集来做,取每一个单词的第一个字母,和最后一个字母进行并查集的操作 但这道题目是欧拉道路(下面摘自http:// ...

随机推荐

  1. maven-pom-properties

    出处: http://blog.csdn.net/taiyangdao/article/details/52358083

  2. 利用Cain+wireshark进行协议分析

    Cain抓包指南 1.简介: 在开发测试工作中经常有捕抓设备间通信报文的需求,但有时候被抓包的设备并不直接和进行抓包的主机或设备进行通信,因此会达不到想要的效果.解决该问题的常见方法有: (1).为被 ...

  3. Telnet协议详解

    转:http://www.cnblogs.com/dazhaxie/archive/2012/06/27/2566054.html 1. 概述 Telnet协议是TCP/IP协议族中的一员,是Inte ...

  4. getopt使用

    参考: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html http://en.wikipedia.org ...

  5. GIS空间参考及坐标转换

    空间参考(Spatial Reference)是 GIS 数据的骨骼框架,能够将我们的数据定位到相应的位置,为地图中的每一点提供准确的坐标. 在同一个地图上显示的地图数据的空间参考必须是一致的,如果两 ...

  6. Matlab与C++混合编程 编写独立外部应用程序时出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误

    出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误,这种错误一般是同名函数出现在两个不同的头文件中了. 笔者的这个错误是由于 #include "mat.h" ...

  7. python pip更换下载源(转)

    对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题.所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装成 ...

  8. CSS实现四种loading动画效果

    四种loading加载效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  9. [转载]Java 反射机制(包括组成、结构、示例说明等内容)

    FROM:http://www.cnblogs.com/skywang12345/p/3345205.html 第1部分 Java 反射机制介绍 Java 反射机制.通俗来讲呢,就是在运行状态中,我们 ...

  10. javascript 递归函数调用(recursive funciton call)

    所谓的递归函数调用,就是自己调用自己的函数. var timerHandler = null; function a(){ console.log(123); timerHandler = setTi ...