-
Notifications
You must be signed in to change notification settings - Fork 15
/
11060.cpp
47 lines (40 loc) · 1.02 KB
/
11060.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
/*
graphs > traversal > topological sorting
difficulty: easy
date: 31/Jan/2020
by: @brpapa
*/
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int main() {
int V, T = 0;
while (cin >> V) {
string str[110];
map<string, int> id;
vector<int> adjList[110];
vector<int> inDegree(V, 0);
for (int v = 0; v < V; v++)
cin >> str[v], id[str[v]] = v;
int E; cin >> E;
while (E--) {
string s1, s2; cin >> s1 >> s2;
adjList[id[s1]].push_back(id[s2]);
inDegree[id[s2]]++;
}
priority_queue<int> pq;
for (int v = 0; v < V; v++) if (inDegree[v] == 0) pq.push(-v);
printf("Case #%d: Dilbert should drink beverages in this order:", ++T);
while (!pq.empty()) {
int v = 0-pq.top(); pq.pop();
cout << " " << str[v];
for (int u : adjList[v])
if (--inDegree[u] == 0)
pq.push(-u);
}
cout << "." << endl << endl;
}
return 0;
}