-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWORDS1.cpp
74 lines (70 loc) · 1.6 KB
/
WORDS1.cpp
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
#include <bits/stdc++.h>
using namespace std;
int in[30], out[30], p[30];
vector<string> v;
int get_parent(int i)
{
while (p[i] != i)
{
i = p[p[i]];
}
return i;
}
void join(int i, int j)
{
int pi = get_parent(i);
int pj = get_parent(j);
p[pj] = pi;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t, n, i, extra, unique_p;
string s;
cin >> t;
while (t--)
{
memset(in, 0, sizeof in);
memset(out, 0, sizeof out);
v.clear();
extra = unique_p = 0;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> s;
v.push_back(s);
in[s[0] - 'a']++;
out[s[s.size() - 1] - 'a']++;
}
for (i = 0; i < 30; i++)
{
if (in[i] != out[i])
extra++;
}
for (i = 0; i < 30; i++)
{
p[i] = i;
}
for (i = 0; i < n; i++)
{
s = v[i];
if (get_parent(s[0] - 'a') != get_parent(s[s.size() - 1] - 'a'))
join(s[0] - 'a', s[s.size() - 1] - 'a');
}
for (i = 0; i < 30; i++)
{
//cout << p[i] << " ";
if (p[i] == i && (in[i] > 0 || out[i] > 0))
unique_p++;
}
//cout << "reached " << extra << " " << unique_p << "\n";
if ((extra == 0 || (extra == 2 && n > 2)) && unique_p == 1)
cout << "Ordering is possible.\n";
else
cout << "The door cannot be opened.\n";
}
return 0;
}