Post

UVa10129 - Play on Words

UVa10129 - Play on Words

题设

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 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 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

1
2
3
4
5
6
7
8
9
10
11
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

Sample Output

1
2
3
The door cannot be opened.
Ordering is possible.
The door cannot be opened.

题解

本题考察的是欧拉回路和欧拉道路的判断:

  1. 图为连通图

  2. 图中每个点的入度和出度均相等或有且仅有两个点的入度和出度不行等且其中一个的入度比出度少1,另一个出度比入度少1

对于第一个条件,我选择用并查集来判断,第二个条件则用数组记录并判断即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <cstring>
#include <iostream>
using namespace std;
const int N = 26;

int n;
int in[N], out[N], p[N];

void init() {
  for (int i = 0; i < N; i++) p[i] = i;
  memset(in, 0, sizeof in);
  memset(out, 0, sizeof out);
  memset(G, false, sizeof G);
}

int find(int x) {
  if (p[x] != x) p[x] = find(p[x]);
  return p[x];
}

bool euler() {
  int cnt1 = 0, cnt2 = 0;
  for (int i = 0; i < N; i++) {
    if (in[i] == out[i]) continue;
    if (in[i] == out[i] - 1)
      cnt1++;
    else if (out[i] == in[i] - 1)
      cnt2++;
    else
      return false;
  }
  if (!((cnt1 == 0 && cnt2 == 0) || (cnt1 == 1 && cnt2 == 1))) return false;

  int a;
  for (int i = 0; i < N; i++) {
    if (in[i] || out[i]) {
      a = find(i);
      break;
    }
  }

  for (int i = 0; i < N; i++)
    if (in[i] || out[i])
      if (find(i) != a) return false;

  return true;
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    init();
    scanf("%d", &n);
    char s[1000];
    for (int i = 0; i < n; i++) {
      scanf("%s", s);
      int a = s[0] - 'a', b = s[strlen(s) - 1] - 'a';
      out[a]++, in[b]++;
      a = find(a), b = find(b);
      p[a] = b;
    }

    if (euler())
      puts("Ordering is possible.");
    else
      puts("The door cannot be opened.");
  }

  return 0;
}
This post is licensed under CC BY 4.0 by the author.