-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa-10507.cpp
More file actions
101 lines (67 loc) · 2.56 KB
/
UVa-10507.cpp
File metadata and controls
101 lines (67 loc) · 2.56 KB
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
#include <iostream>
#include <cstdio>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h> // here we have all the STL we need, including istringstream and ostringstream
/*In this problem, it's mentioned in the book that Union find disjoin set can
make solving this problem easier, but I prefer using a hashmap of sets, I could have used a normal array though,
there is no need for the hashmap, the array can be of size 26 and indexed with character - 'A' */
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
bool woke[26];
unordered_map<char, unordered_set<char>> adj_list(26);
int main() {
int n;
int m;
string woke_str;
string s;
while(scanf("%d\n", &n) != EOF) {
memset(woke, false, sizeof(woke));
adj_list.clear();
scanf("%d\n", &m);
cin >> woke_str;
vector<pair<char, char>> connections(m);
getchar();
for (int i = 0; i < m; i++) {
cin >> s;
connections[i] = make_pair(s[0], s[1]);
}
for (int i = 0; i < woke_str.length(); i++) {
woke[woke_str[i]-'A'] = true;
}
bool cont = false; // means we can wake some area in the brain, will be set to false if we can't,
// and that way the loop exits.
int ans = 0;
char A1, A2;
int slp = n - 3;
do {
// we need to add the list of woke areas connected to each non-woke area.
cont = false;
for (int j = 0; j < connections.size(); j++) {
A1 = connections[j].first;
A2 = connections[j].second;
if (not woke[A1 - 'A'] and woke[A2 - 'A']) {
adj_list[A1].insert(A2);
}
if (woke[A1 - 'A'] and not woke[A2 - 'A']) {
adj_list[A2].insert(A1);
}
}
for (int i = 0; i < 26; i++) {
if (not woke[i] && adj_list[i+'A'].size() >= 3) {
woke[i] = true;
slp--;
cont = true;
}
}
// if one or more areas woke up at this moment, we add 1 only because it takes 1 year to wake up
if (cont)
ans += 1;
} while (slp > 0 && cont);
if (slp > 0) cout << "THIS BRAIN NEVER WAKES UP\n";
else cout << "WAKE UP IN, " << ans << ", YEARS\n";
getchar();
}
}