题意
单词链接, 如acm, malform, mouse可以链接
思路
欧拉回路
1.图连通
2.图中所有点的出度==入度,或只有两个奇度点并且一个点的入度比出度大1(终点),另一个点的出度比入度大1(起点)
调试的时候遇到几个比较坑的数据 需要特殊处理判断一下
input
3
2
aa
aaa
1
ab
2
aa
bb
output
Ordering is possible.
Ordering is possible.
The door cannot be opened.
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int in[30], out[30], G[30][30], vis[30];
void dfs(int a)
{
vis[a] = 1;
for (int b = 0; b < 26; b++)
if (!vis[b] && G[a][b])
dfs(b);
}
int main()
{
int T, n;
char s[1000+10];
scanf("%d",&T);
while(T--){
bool ok = true, ok2 = false;
memset(vis, 0, sizeof(vis));
memset(G, 0, sizeof(G));
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
scanf("%d",&n);
if(n==1) ok2 = true;
while(n--){
scanf("%s",s);
int len = strlen(s);
int a = s[0]-'a', b = s[len-1]-'a';
G[a][b]++;
in[b]++, out[a]++;
}
if(ok2){
puts("Ordering is possible.");
continue;
}
int cnt1 = 0, cnt2 = 0, cnt = 0;
for( int i = 0; i < 26; i++ ){
if( in[i] == out[i] ) continue;
if( in[i] + 1 == out[i] ) cnt1++;
else if( out[i] + 1 == in[i] ) cnt2++;
else{ ok = false; break;}
}
if(cnt1 && cnt2 && cnt1+cnt2 > 2) ok = false;
int num = 0;
if(ok){
for( int i = 0; i < 26; i++ )
if( out[i] ){
dfs(i);
break;
}
for( int i = 0; i < 26; i++ )
if( in[i] + out[i] )
if( !vis[i] ){
ok = false;
break;
}
}
if(ok) puts("Ordering is possible.");
else puts("The door cannot be opened.");
}
return 0;
}