-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUVa-10199-Tourist-Guide.cpp
126 lines (99 loc) · 2.35 KB
/
UVa-10199-Tourist-Guide.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
#define endl '\n'
#define UNVISITED 0
#define EXPLORED 1
#define VISITED 2
using namespace std;
typedef int64_t ll;
void Fast() {
cin.sync_with_stdio(0);
cin.tie(0);cout.tie(0);
}
void File() {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
const int N = 1e5 + 9, M = 1e6 + 9, oo = 0x3f3f3f3f;
ll INF = 0x3f3f3f3f3f3f3f3f;
int Head[N], Next[M], To[M], Par[N], in_time[N], Low[N], ne, n, m, dfs_timer;
int root, rootChildren;
bool Art[N];
vector <pair <int, int> > bridges;
map <string, int> inv;
string line, why, u, v, ar[N];
stringstream ss;
void addEdge(int from, int to) {
Next[++ne] = Head[from];
Head[from] = ne;
To[ne] = to;
}
void DFS(int node)
{
in_time[node] = Low[node] = ++dfs_timer;
for(int i = Head[node]; i; i = Next[i])
{
if(in_time[To[i]] == 0)
{
if(node == root) rootChildren++;
Par[To[i]] = node;
DFS(To[i]);
Low[node] = min(Low[node], Low[To[i]]);
if(Low[To[i]] >= in_time[node])
Art[node] = true;
}
else if(To[i] != Par[node])
Low[node] = min(Low[node], in_time[To[i]]);
}
}
void _clear() {
memset(Head, 0, sizeof(Head[0]) * (n + 2));
memset(Par, -1, sizeof(Par[0]) * (n + 2));
memset(Art, false, sizeof(Art[0]) * (n + 2));
memset(in_time, 0, sizeof(in_time[0]) * (n + 2));
bridges.clear();
inv.clear();
dfs_timer = 0;
ne = 0;
}
void Solve()
{
_clear();
for(int i = 1; i <= n; ++i)
{
cin >> ar[i];
inv[ar[i]] = i;
}
cin >> m;
while(m--)
{
cin >> u >> v;
addEdge(inv[u], inv[v]);
addEdge(inv[v], inv[u]);
}
for(int i = 1; i <= n; ++i) if(in_time[i] == 0)
{
root = i;
rootChildren = 0;
DFS(i);
Art[i] = rootChildren > 1;
}
vector <string> Arti;
for(int i = 1; i <= n; ++i) if(Art[i])
Arti.push_back(ar[i]);
sort(Arti.begin(), Arti.end());
cout << Arti.size() << " camera(s) found" << endl;
for(string s : Arti)
cout << s << endl;
}
int main()
{
Fast();
int tc = 1;
for(int i = 1; cin >> n && n; ++i)
{
if(i > 1) cout << endl;
cout << "City map #" << i << ": ";
Solve();
}
}