欧拉通路
View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
int n;
char st[1005];
bool exist[30];
int in[30], out[30];
int father[30];
int getanc(int a)
{
if (father[a] == a)
return a;
return father[a] = getanc(father[a]);
}
void merge(int a, int b)
{
father[getanc(a)] = getanc(b);
}
void input()
{
scanf("%d", &n);
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
memset(exist, 0, sizeof(exist));
for (int i = 0; i < 26; i++)
father[i] = i;
for (int i = 0 ; i < n; i++)
{
scanf("%s", st);
in[st[0] - 'a']++;
out[st[strlen(st) - 1] - 'a']++;
merge(st[0] - 'a', st[strlen(st) - 1] - 'a');
exist[st[0] - 'a'] = exist[st[strlen(st) - 1] - 'a'] = true;
}
}
bool ok()
{
int start =0;
int end = 0;
int cnt = 0;
for (int i = 0; i < 26; i++)
if (father[i] == i && exist[i])
cnt++;
if (cnt > 1)
return false;
for (int i = 0; i < 26; i++)
if (in[i] - out[i] == 1)
start++;
else if (out[i] - in[i] == 1)
end++;
else if (out[i] == in[i])
continue;
else
return false;
return start <= 1 && end <= 1;
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
input();
if (ok())
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return 0;
}