判断一些字符串能首尾相连连在一起
并查集求欧拉回路和通路
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.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
char s[MAXN];
int in[],out[],vis[],f[],p[];
int find(int x)
{
if(f[x]==-)return x;
return f[x]=find(f[x]);
}
void bing(int x,int y)
{
int t1=find(x);
int t2=find(y);
if(t1!=t2)
{
f[t1]=t2;
}
}
void init()
{
cl(vis);
cl(in);
cl(out);
memset(f,-,sizeof(f));
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
while(tt--)
{
init();
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",s);
int len=strlen(s);
int x=s[]-'a';
int y=s[len-]-'a';
out[x]++;
in[y]++;
bing(x,y);
vis[x]=vis[y]=;
}
int cnt=; //统计个数
for(i=;i<;i++)
{
if(vis[i]&&find(i)==i)
{
cnt++;
}
}
if(cnt>)
{
printf("The door cannot be opened.\n");
continue;
}
int tot=; //统计出入度不相同的点
for(i=;i<;i++)
{
if(vis[i]&&in[i]!=out[i])
{
p[tot++]=i;
}
}
if(tot==)
{
printf("Ordering is possible.\n");
continue;
}
if(tot==&&((out[p[]]-in[p[]]==&&in[p[]]-out[p[]]==)||(out[p[]]-in[p[]]==&&in[p[]]-out[p[]]==))) //欧拉通路
{
printf("Ordering is possible.\n");
continue;
}
printf("The door cannot be opened.\n");
}
}