Play on Words(有向图欧拉路)

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8571 Accepted: 2997

Description

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 ``motorola''. 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 first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines 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 first 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.

题意:给出n个单词,问所有的单词能否首尾相连(能相连的单词的首和尾必须是相同的);

思路:一道判断有向图欧拉路的题目;
   可以将每个单词的首和尾看作节点,判断图的连通性可以用并查集,每输入一个单词将其首和尾加入集合中,最后任取一个节点判断其他所有节点和该节点是否有共同的祖先,若是,则是连通的,否则不连通;
     在连通性的前提下,若所有节点的入读等于出度 或者 一个节点的入度比出度大1 一个节点的入度比出度小1,说明有欧拉路,否则没有欧拉路;
因为手误,贡献一次WA;
  1 #include<stdio.h>
  2 #include<string.h>
  3 
  4 int set[30],indegree[1010],outdegree[1010],vis[26];
  5 int count;
  6 
  7 void init()
  8 {
  9     for(int i = 0; i < 26; i++)
 10         set[i] = i;
 11 }
 12 
 13 int find(int x)
 14 {
 15     if(set[x] != x)
 16         set[x] = find(set[x]);
 17     return set[x];
 18 }
 19 
 20 int check()
 21 {
 22     int x,i;
 23     int flag = 0;
 24     for(i = 0; i < 26; i++)
 25     {
 26         if(vis[i])
 27         {
 28             if(flag == 0)
 29             {
 30                 x = find(i);
 31                 flag = 1;
 32             }
 33             else
 34             {
 35                 if(find(i) != x)
 36                     break;
 37             }
 38         }
 39     }
 40     if(i < 26)
 41         return 0;//图是不连通的,直接返回;
 42 
 43     int c1 = 0, c2 = 0, c3 = 0;
 44     for(int i = 0; i < 26; i++)
 45     {
 46         if(vis[i])
 47         {
 48             if(indegree[i] == outdegree[i])
 49                 c1++;
 50             else if(indegree[i]-outdegree[i] == 1)
 51                 c2++;
 52             else if(outdegree[i]-indegree[i] == 1)
 53                 c3++;
 54         }
 55     }
 56     if((c2 == 1 && c3 == 1 && c1 == count-2) ||(c1 == count))
 57         return 1;
 58     else return 0;
 59 }
 60 
 61 int main()
 62 {
 63     int test,n;
 64     char s[1010];
 65     scanf("%d",&test);
 66     while(test--)
 67     {
 68         memset(indegree,0,sizeof(indegree));
 69         memset(outdegree,0,sizeof(outdegree));
 70         memset(vis,0,sizeof(vis));
 71         init();
 72         count = 0;
 73 
 74         scanf("%d",&n);
 75         for(int i = 1; i <= n; i++)
 76         {
 77             scanf("%s",s);
 78             int len = strlen(s);
 79             int u = s[0]-'a';
 80             if(!vis[u])
 81             {
 82                 vis[u] = 1;
 83                 count++;
 84             }
 85             int v = s[len-1]-'a';
 86             if(!vis[v])
 87             {
 88                 vis[v] = 1;
 89                 count++;
 90             }
 91 
 92             indegree[u]++;
 93             outdegree[v]++;
 94             int tu = find(u);
 95             int tv = find(v);
 96             if(tu != tv)
 97                 set[tu] = tv;
 98         }
 99 
100         if(check())
101             printf("Ordering is possible.\n");
102         else printf("The door cannot be opened.\n");
103     }
104     return 0;
105 }
View Code
 
 

欧拉路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次,

称这条回路为欧拉回路。具有欧拉回路的图成为欧拉图。

 

判断欧拉路是否存在的方法

有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。

无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。

 

判断欧拉回路是否存在的方法

有向图:图连通,所有的顶点出度=入度。

无向图:图连通,所有顶点都是偶数度。

其中判断图的连通性用并查集。

转载于:https://www.cnblogs.com/LK1994/p/3291757.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
构造欧拉回路的整数规划模型如下: 假设有一个无向 $G=(V,E)$,其中 $V$ 表示节点集合,$E$ 表示边集合。设 $x_{ij}$ 表示从节点 $i$ 到节点 $j$ 的边的数量,$y_i$ 表示节点 $i$ 的度数。则整数规划模型可以表示为: $$ \begin{aligned} &\text{maximize} && 0\\ &\text{subject to} && \sum_{j\in V} x_{ij} - \sum_{j\in V} x_{ji} = 0, \quad \forall i\in V\\ &&& y_i = \sum_{j\in V} x_{ij}, \quad \forall i\in V\\ &&& \sum_{i,j\in V} x_{ij} = |E|\\ &&& x_{ij} \in \{0,1\}, \quad \forall i,j\in V\\ &&& y_i \in \{0,2\}, \quad \forall i\in V\\ \end{aligned} $$ 其中第一个约束条件表示节点 $i$ 的入度和出度相等,第二个约束条件表示节点 $i$ 的度数为其相邻边的数量之和,第三个约束条件表示所有边都必须被遍历,第四个和第五个约束条件是整数规划的限制条件。 Python 可以使用 PuLP 模块来实现整数规划求解: ```python from pulp import * def euler_circuit(edges): # 获取所有的节点 nodes = set() for a, b in edges: nodes.add(a) nodes.add(b) n = len(nodes) # 创建整数规划问题 prob = LpProblem('Euler Circuit', LpMaximize) # 创建变量 x = {} y = {} for i in nodes: for j in nodes: if i != j: x[i, j] = LpVariable(f'x_{i}_{j}', 0, 1, LpInteger) y[i] = LpVariable(f'y_{i}', 0, 2, LpInteger) # 创建目标函数 prob += 0 # 添加约束 for i in nodes: prob += lpSum(x[i, j] for j in nodes if i != j) - lpSum(x[j, i] for j in nodes if i != j) == 0 prob += y[i] == lpSum(x[i, j] for j in nodes if i != j) prob += lpSum(x[i, j] for i in nodes for j in nodes if i != j) == len(edges) # 求解 prob.solve() # 获取结果 circuit = [] for i in nodes: for j in nodes: if i != j and value(x[i, j]) == 1: circuit.append((i, j)) return circuit ``` 其中 `edges` 是一个由边组成的列表,每条边是一个二元组 `(a, b)` 表示从节点 `a` 到节点 `b` 有一条边。函数返回一个欧拉回路,也是一个由边组成的列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值