HDU - 1116 Play on Words

题意:欧拉路还是欧拉回路的判断

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAXN = 30;

int in[MAXN],fa[MAXN],out[MAXN];

int find(int x){
    if (fa[x] != x)
        fa[x] = find(fa[x]);
    return fa[x];
}

void unite(int x,int y){
    int t1 = find(x);
    int t2 = find(y);
    fa[t1] = t2;
}

int main(){
    char str[1010];
    int t,x,y,n;
    scanf("%d",&t);
    while (t--){
        for (int i = 0; i <= 26; i++){
            fa[i] = i;
            in[i] = 0;
            out[i] = 0;
        }
        scanf("%d",&n);
        for (int i = 1; i <= n; i++){
            scanf("%s",str);
            x = str[0] - 'a';
            y = str[strlen(str)-1] - 'a';
            unite(y,x);
            out[y]++;
            in[x]++;
        }
        int num1=0,num2=0,num3=0;
        int m = 0;
        for (int i = 0; i <= 26; i++){
            if (in[i]==0 && out[i]==0)
                continue;
            m++;
            if (abs(in[i]-out[i]) == 1)
                num1++;
            if (in[i] == out[i])
                num3++;
            if (find(i) == i)
                num2++;
        }
         if(num2==1&&(num3==m||(num3==m-2&&num1==2)))  
             printf("Ordering is possible.\n");
         else printf("The door cannot be opened.\n");
    }
    return 0;
}



你可能感兴趣的:(HDU - 1116 Play on Words)