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
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 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
/************************************************** filename :j.cpp author :maksyuki created time :2018/6/1 21:33:24 last modified :2018/6/1 21:55:59 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 66; int n; int g[maxn][maxn]; int in[maxn], out[maxn]; int par[maxn]; void init() { memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); memset(g, 0, sizeof(g)); for(int i = 0; i < maxn; i++) par[i] = i; } int UnFind(int v) { if(par[v] == v) return v; return par[v] = UnFind(par[v]); } int main() { #ifdef LOCAL CFF; //CFO; #endif int t; cin >> t; while(t--) { init(); cin >> n; string s; for(int i = 1; i <= n; i++) { cin >> s; int slen = s.size(); int va = s[slen-1] - 'a'; int vb = s[0] - 'a'; in[va]++; out[vb]++; g[va][vb] = 1; int para = UnFind(va); int parb = UnFind(vb); if(para != parb) par[para] = parb; } bool is_first = true; bool is_valid = true; int ttpar = -1; for(int i = 0; i < 26; i++) if(in[i] || out[i]) { int tpar = UnFind(i); if(is_first) { is_first = false; ttpar = tpar; } else { if(ttpar != tpar) { is_valid = false; break; } } } if(!is_valid) cout << "The door cannot be opened." << endl; else { int vva = 0, vvb = 0; for(int i = 0; i < 26; i++) if(in[i] || out[i]) { if(in[i] != out[i]) { if(abs(in[i] - out[i]) >= 2) { is_valid = false; break; } else { if(in[i] > out[i]) vva++; else vvb++; } } } if(!is_valid) cout << "The door cannot be opened." << endl; else { if((!vva && !vvb) || (vva == 1 && vvb == 1)) cout << "Ordering is possible." << endl; else cout << "The door cannot be opened." << endl; } } } return 0; } |
- « 上一篇:uva10305