#include <stdio.h> #include <string.h> #include <math.h> char s[1005]; int in[27], out[27]; int g[27][27]; int vis[27]; //int same[27]; int Euler()//返回1表示在图全连通的条件下,是欧拉道路 { int n = 0; for (int i = 1;i <= 26;i++) { //if (same[i] && !in[i] && !out[i])return 0; int sum = in[i] + out[i]; if (sum == 1 || sum == -1)n++; else if (sum != 0)return 0; } if (n != 0 && n != 2)return 0; return 1; } void dfs(int u) { for (int i = 1;i <= 26;i++) { if (!vis[i] && g[i][u]) { vis[i] = 1; dfs(i); } } } int DFS()//返回1表示图全连通 { int count = 0; for (int i = 1;i <= 26;i++) { if (in[i] || out[i]) if (!vis[i]) { vis[i] = 1; if (++count > 1)return 0; dfs(i); } } return 1; } int main(void) { int t, n; scanf("%d", &t); while (t--) { scanf("%d", &n); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); // memset(same, 0, sizeof(same)); memset(vis, 0, sizeof(vis)); memset(g, 0, sizeof(g)); while (n--) { scanf("%s", s); int _last = strlen(s) - 1; int u = s[0] - 'a' + 1, v = s[_last] - 'a' + 1; //uv相同不需特别处理,否则当全部是比如mem这种就会出错,难过,自以为谨小慎微反而错了--bug了好久好久 g[u][v] = g[v][u] = 1; // if (u == v)same[u] = 1; //else { in[u]++; out[v]--; //} } int a = !DFS(), b = !Euler(); if (a || b) printf("The door cannot be opened. "); else printf("Ordering is possible. "); } return 0; }