-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathbuilddeps.cc
44 lines (44 loc) · 812 Bytes
/
builddeps.cc
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
// https://open.kattis.com/problems/builddeps
#include<bits/stdc++.h>
using namespace std;
using msi=map<string,int>;
using vi=vector<int>;
using vs=vector<string>;
using vvi=vector<vi>;
int main(){
int n;
cin>>n;
vvi g(n);
msi m;
vs mi(n);
string s;
getline(cin,s);
int w=0;
for(int i=0;i<n;i++){
getline(cin,s);
stringstream in(s);
in>>s;
s=s.substr(0, s.size()-1);
int u=m.count(s)?m[s]:m[s]=w++;
mi[u]=s;
while(in>>s){
int v=m.count(s)?m[s]:m[s]=w++;
mi[v]=s;
g[v].push_back(u);
}
}
vi t(n);
vi o;
function<void(int)>dfs=[&](int u){
t[u]++;
for(int v:g[u]){
if(t[v])continue;
dfs(v);
}
o.push_back(u);
};
getline(cin, s);
dfs(m[s]);
reverse(o.begin(),o.end());
for(int u:o)cout<<mi[u]<<endl;
}