-
Notifications
You must be signed in to change notification settings - Fork 0
/
10199.cpp
92 lines (78 loc) · 2.2 KB
/
10199.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
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <string>
#include <vector>
int num_city, step;
std::array<bool, 100> seen, camera;
std::array<int, 100> count, low;
std::array<std::vector<int>, 100> connected;
void explore(int x, int parent)
{
int num_child = 0;
seen[x] = true;
count[x] = low[x] = ++step;
for (auto const &i : connected[x]) {
if (!seen[i]) {
explore(i, x);
low[x] = std::min(low[x], low[i]);
if (parent != -1 && low[i] >= count[x]) {
camera[x] = true;
}
++num_child;
}
else {
low[x] = std::min(low[x], count[i]);
}
}
if (parent == -1 && num_child > 1) {
camera[x] = true;
}
}
int main(void)
{
int idx, num_route, num_cam;
std::string src, dst;
std::array<std::string, 100> city;
std::map<std::string, int> city2id;
idx = 0;
while (1) {
std::cin >> num_city;
if (!num_city) break;
city2id.clear();
for (int i = 0; i < num_city; ++i) {
std::cin >> city[i];
}
std::sort(city.begin(), city.begin() + num_city);
for (int i = 0; i < num_city; ++i) {
city2id[city[i]] = i;
connected[i].clear();
}
std::cin >> num_route;
for (int i = 0; i < num_route; ++i) {
std::cin >> src >> dst;
connected[city2id[src]].push_back(city2id[dst]);
connected[city2id[dst]].push_back(city2id[src]);
}
seen.fill(false);
camera.fill(false);
count.fill(0);
low.fill(0);
step = 0;
for (int i = 0; i < num_city; ++i) {
if (!seen[i]) explore(i, -1);
}
num_cam = 0;
for (int i = 0; i < num_city; ++i) {
if (camera[i]) ++num_cam;
}
if (idx) std::cout << std::endl;
std::cout << "City map #" << ++idx << ": " << num_cam
<< " camera(s) found" << std::endl;
for (int i = 0; i < num_city; ++i) {
if (camera[i]) std::cout << city[i] << std::endl;
}
}
return 0;
}