-
Notifications
You must be signed in to change notification settings - Fork 0
/
10507.cpp
94 lines (89 loc) · 1.23 KB
/
10507.cpp
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
88
89
90
91
92
93
94
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int cnt[26],ans;
bool st[30][30];
queue<int> arq;
void init(char *start)
{
ans = 0;
while(!arq.empty())
{
arq.pop();
}
memset(st,0,sizeof(bool) * 30 *30);
memset(cnt,0,sizeof(int) * 26);
int len = strlen(start)-1;
for(int i=0;i<len;i++)
{
int c1 = start[i] - 'A';
arq.push(c1);
cnt[c1] = 3;
//printf("%d\n",c1);
}
}
int bfs()
{
int ny = arq.size();
while(!arq.empty())
{
int p = arq.front();
arq.pop();
for(int i=0;i<26;i++)
{
if(cnt[i]<3 && st[p][i])
{
cnt[i]++;
if(cnt[i]==3)
{
arq.push(i);
}
}
}
ny--;
if(!ny)
{
ans++;
ny = arq.size();
}
}
int ret = 0;
for(int i=0;i<26;i++)
{
if(cnt[i]==3)
{
ret += 1;
}
}
return ret;
}
int main()
{
int n,m;
char start[30],con[5];
while(scanf("%d",&n)!=EOF)
{
// ans = 0;
scanf("%d",&m);
fgets(start,30,stdin);
fgets(start,30,stdin);
//printf("%s",start);
init(start);
for(int i=0;i<m;i++)
{
fgets(con,5,stdin);
int p1 = con[0]-'A',p2 = con[1]-'A';
st[p1][p2] = st[p2][p1] = true;
}
if(bfs()==n)
{
printf("WAKE UP IN, %d, YEARS\n",ans-1);
}
else
{
printf("THIS BRAIN NEVER WAKES UP\n");
}
}
return 0;
}