-
Notifications
You must be signed in to change notification settings - Fork 0
/
10129.c
87 lines (87 loc) · 1.3 KB
/
10129.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<stdio.h>
#include<string.h>
char s[1020];
int G[30][30];
int reg[30][2];
int max;
int vis[30];
void dfs_fuck(int i)
{
int j;
max++;
vis[i]=1;
for (j=0;j<26;j++)
if (G[i][j] && !vis[j])
dfs_fuck(j);
}
main()
{:
int i,j,k,l,m,n;
int ans;
int flag1,ok,flag2,start,total;
/* freopen("in.txt","r",stdin);*/
scanf("%d",&n);
getchar();
for (ans=0;ans<n;ans++)
{
flag1=0;
flag2=0;
ok=1;
max=0;
total=0;
memset(G,0,sizeof(G));
memset(reg,0,sizeof(reg));
memset(vis,0,sizeof(vis));
scanf("%d",&m);
{
getchar();
for (j=0;j<m;j++)
{
scanf("%s",s);
l=strlen(s);
G[s[0]-'a'][s[l-1]-'a']++;
reg[s[0]-'a'][0]++;
reg[s[l-1]-'a'][1]++;
}
}
start=s[0]-'a';
for (i=0;i<26;i++)
{
if (reg[i][0] || reg[i][1])
total++;
if (reg[i][0]!=reg[i][1])
{
if (flag1>1 || flag2>1)
{
ok=0;
break;
}
if (reg[i][0]-reg[i][1]==1)
{
flag1++;
start=i;
}
else
if (reg[i][0]-reg[i][1]==-1)
flag2++;
else
{
ok=0;
break;
}
}
}
if (ok)
{
dfs_fuck(start);
/* printf("%d\n",start);*/
if (max==total)
printf("Ordering is possible.\n",max);
else
printf("The door cannot be opened.\n",max,total);
}
else
printf("The door cannot be opened.\n");
}
return 0;
}