-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path10199 - Tourist Guide.cpp
95 lines (90 loc) · 1.83 KB
/
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
/**
UVa 10199 - Tourist Guide
by Rico Tiongson
Submitted: October 26, 2013
Accepted 0.029s C++
O(|V| + E) time
*/
#include<iostream>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#define MX 101
using namespace std;
int n, m, tc;
int low[MX], id[MX], ID;
bool ap[MX];
vector<int> adj[MX];
map<string, int> M;
string S[MX];
void dfs( int u, int v ){
id[v] = low[v] = ID++;
int child = 0;
for( int i=0; i<adj[v].size(); ++i ){
int w = adj[v][i];
if( id[w] == -1 ){
dfs(v, w);
child++;
low[v] = min( low[v], low[w] );
if( u != v && id[v] <= low[w] )
ap[v] = true;
}
else if( w != u )
low[v] = min( low[v], id[w] );
}
if( u == v && child > 1 )
ap[v] = true;
}
void Tarjan(){
memset( low, -1, sizeof low );
memset( id, -1, sizeof id );
ID = 0;
memset( ap, 0, sizeof ap );
for( int i=0; i<n; ++i ){
if( id[i] == -1 ){
// cout << i << endl;
dfs(i, i);
}
}
}
int main(){
string s, t;
bool first = true;
while( cin >> n, n ){
if( !first ) cout << endl;
else first = false;
M.clear();
for( int i=0; i<n; ++i ){
cin >> S[i];
M[S[i]] = i;
adj[i].clear();
}
cin >> m;
while(m--){
cin >> s >> t;
int a = M[s];
int b = M[t];
adj[a].push_back(b);
adj[b].push_back(a);
}
// for( int i=0; i<n; ++i ){
// sort( adj[i].begin(), adj[i].end() );
// adj[i].erase( unique( adj[i].begin(), adj[i].end() ), adj[i].end() );
// }
Tarjan();
cout << "City map #" << ++tc << ": ";
set<string> ans;
for( int i=0; i<n; ++i ){
// cout << ap[i] << " ";
if( ap[i] ){
ans.insert( S[i] );
}
}
cout << ans.size() << " camera(s) found" << endl;
for( set<string>::iterator it = ans.begin(); it != ans.end(); ++it ){
cout << *it << endl;
}
}
}