-
Notifications
You must be signed in to change notification settings - Fork 1
/
11060 - Beverages.cpp
66 lines (53 loc) · 1 KB
/
11060 - Beverages.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
#include<bits/stdc++.h>
using namespace std;
int cn = 0;
void solve(int n){
map<string,int> code;
vector<string> name(n+1);
vector<int> indegree(n+1);
vector<int> graph[n];
for(int i = 0 ; i < n; i++){
cin>>name[i];
code[name[i]] = i;
}
int m; cin >> m;
for(int i = 0 ; i < m ; i++){
string s, t; cin >> s >> t;
int a= code[s] , b = code[t];
graph[a].emplace_back(b);
indegree[b] ++;
}
vector<int> order;
bool flag = true;
while(flag){
flag = false;
for(int i = 0 ; i < n; i++){
if(indegree[i]==0){
flag = true;
indegree[i]=-1;
order.emplace_back(i);
for(int x : graph[i]){
indegree[x]--;
}
break ;
}
}
}
cout <<"Case #"<<++cn<<": Dilbert should drink beverages in this order: ";
int sz = (int)order.size();
for(int i = 0 ; i < sz ; i++){
cout << name[order[i]];
if(i==sz-1)
cout<<".\n";
else
cout<<" ";
}
cout <<"\n";
}
int main(){
ios_base::sync_with_stdio(false);
int n;
while(cin >> n)
solve(n);
return 0;
}