-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexicographical topSort.cpp
69 lines (57 loc) · 1.38 KB
/
Lexicographical topSort.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
// uva 11060
#include<bits/stdc++.h>
using namespace std;
const int maxi = 111;
vector<int> adj[maxi];
int in[maxi], n, tick, taken[maxi];
map<string, int> mp;
map<int, string> mp_res;
inline void reset() {
mp.clear();
mp_res.clear();
tick = 1;
for(int i=0; i<maxi; i++) {
adj[i].clear();
in[i] = 0;
taken[i] = 0;
}
}
int main() {
int caseno = 1;
while(scanf("%d", &n)==1) {
reset();
for(int i=1; i<=n; i++) {
string u;
cin>>u;
if(!mp[u]) {
mp[u] = tick;
mp_res[tick] = u;
tick++;
}
}
int m;
scanf("%d", &m);
for(int i=1; i<=m; i++) {
string u, v;
cin>>u>>v;
int u_ = mp[u], v_ = mp[v];
adj[u_].push_back(v_);
in[v_]++;
}
printf("Case #%d: Dilbert should drink beverages in this order:", caseno++);
for(int k=1; k<=n; k++) {
for(int i=1; i<=n; i++) {
if(!in[i] && !taken[i]) {
cout<<" "<<mp_res[i];
for(int j=0; j<adj[i].size(); j++) {
in[adj[i][j]]--;
}
taken[i] = 1;
break;
}
}
}
printf(".\n\n");
}
return 0;
}