水题不解释。。。。
# include <stdio.h> # include <string.h> int dee[26], set[26], node[26], n; char s[1002]; int find(int x) { int s, temp; for (s=x; set[s]>=0; s=set[s]) ; while (s!=x) { temp = set[x]; set[x] = s; x = temp; } return s; } void union_set(int x, int y) { int a, b, temp; a = find(x); b = find(y); temp = set[a] + set[b]; if (set[a] > set[b]) { set[a] = b; set[b] = temp; } else { set[b] = a; set[a] = temp; } } int main (void) { int t, i, j, u, v, len, flag, cnt1, cnt2; scanf("%d",&t); while (t--) { flag = 0; memset(set,-1,sizeof(set)); memset(dee,0,sizeof(dee)); memset(node,0,sizeof(node)); scanf("%d",&n); while (n--) { scanf("%s",s); len = strlen(s); u = s[0] - ‘a‘; v = s[len-1] - ‘a‘; dee[u]++; dee[v]--; node[u] = node[v] = 1; if (find(u) != find(v)) union_set(u,v); } for (i=0, j=0; i<26; i++) if (node[i] == 1) node[j++] = i; for (i=0; i<j-1; i++) if (find(node[i])^find(node[i+1])) { flag = 1; break; } if (flag) { puts("The door cannot be opened."); continue; } for (i=0, cnt1=0, cnt2=0; i<j; i++) { if (dee[node[i]] == 1) cnt1++; else if (dee[node[i]] == -1) cnt2++; else if (dee[node[i]] != 0) { flag = 1; break; } } if (flag ||(cnt1!=1 && cnt1)|| (cnt2!=1 && cnt2)) puts("The door cannot be opened."); else puts("Ordering is possible."); } return 0; }
时间: 2024-10-17 06:19:54