-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10507.cpp
102 lines (99 loc) · 1.89 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
95
96
97
98
99
100
101
102
#include <iostream>
#include <map>
#include <bitset>
#include <vector>
#include <set>
#include <cstdio>
#include <queue>
#define foi( i , n , k ) for( int i = n ; i < k ; ++i)
using namespace std;
int funcion( map< int , vector<int> > &mp,set<int> &st,int n)
{
bool cam=true;
int c=0;
map< int , vector<int> >::iterator it;
queue<int> q;
while( cam && st.size() < n)
{
c++;
// cout<<c<<endl;
cam=false;
for( it=mp.begin() ; it!=mp.end() ; ++it)
{
if( st.find(it->first)==st.end() )
{
int t=0;
foi( i , 0 , it->second.size() )
{
if( st.find(it->second[i])!=st.end() )
t++;
}
if( t>2 )
{
cam=true;
q.push(it->first);
}
}
}
while(!q.empty())
{
st.insert(q.front());
q.pop();
}
}
if( st.size()>= n)
{
return c;
}
return -1;
}
int main()
{
int n;
while( scanf("%d",&n)!=EOF)
{
int c;
char a,b;
scanf("%d",&c);
set<int> st;
// cout<<n<<" "<<c<<endl;
foi( i , 0 , 3 )
{
scanf(" %c",&a);
st.insert(a-'A');
// cout<<a<<endl;
}
map< int , vector<int> > mp;
foi( i , 0 , c )
{
scanf(" %c%c",&a,&b);
// cout<<a<<" "<<b<<" "<<a-'A'<<" "<<b-'A'<<endl;
int aa,bb;
aa=a-'A';
bb=b-'A';
if( mp.find(aa)==mp.end() )
{
mp.insert(make_pair(aa,vector<int>()));
}
if( mp.find(bb)==mp.end() )
{
mp.insert(make_pair(bb,vector<int>()));
}
mp[aa].push_back(bb);
mp[bb].push_back(aa);
}
int tot=-1;
// cout<<mp.size() << " " << st.size() << " " << n << endl;
if( mp.size() == n || st.size() == n )
tot=funcion(mp,st,n);
if( tot < 0 )
{
printf("THIS BRAIN NEVER WAKES UP\n");
}
else
{
printf("WAKE UP IN, %d, YEARS\n",tot);
}
}
return 0;
}