有向图是否具有欧拉通路或回路的判定:
欧拉通路:图连通;除2个端点外其余节点入度=出度;1个端点入度比出度大1;一个端点入度比出度小1 或 所有节点入度等于出度
欧拉回路:图连通;所有节点入度等于出度
#include<stdio.h>
#include<string.h>
#define MAX 27
int in[MAX],out[MAX];
int visit[MAX],father[MAX];
int find(int index)
{
if(index==father[index]) return index;
else return find(father[index]);
}
int main(void)
{
int t,n;
int i,j;
int s,e;
char str[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(visit,,sizeof(visit));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(i=;i<MAX;i++) father[i]=i; for(i=;i<n;i++){
scanf("%s",str);
int len=strlen(str);
s=str[]-'a',e=str[len-]-'a';
father[s]=father[e]=find(s);
visit[s]=visit[e]=;
out[s]++;in[e]++;
}
//判断改图是否连通
int r=;
for(i=;i<MAX;i++){
if(visit[i]&&i==father[i]) r++;
}
if(r>){ //aba abc
printf("The door cannot be opened.\n"); continue;
} int x,y,z,h;
x=y=z=h=;
for(i=;i<MAX;i++){
if(visit[i]){
if(out[i]-in[i]====) x++;
else if(in[i]-out[i]==)y++;
else if(in[i]==out[i]) z++;
else h++;
}
}
if(h==&&((x==&&y==)||(x==||y==))) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n"); }
return ;
}