-
Notifications
You must be signed in to change notification settings - Fork 0
/
builddeps.cpp
62 lines (52 loc) · 1.52 KB
/
builddeps.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
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Graph {
private:
unordered_map<string, vector<string>> adjacent_list;
unordered_set<string> visited;
stack<string> s;
void dfs(string dependency) {
visited.insert(dependency);
for (auto it = adjacent_list[dependency].begin(); it != adjacent_list[dependency].end(); ++it) {
if (visited.find(*it) == visited.end()) dfs(*it);
}
s.push(dependency);
}
public:
void add_edge(string file, string dependency) {
adjacent_list[dependency].push_back(file);
}
void toposort(string changed) {
dfs(changed);
while (!s.empty()) {
cout << s.top() << "\n";
s.pop();
}
visited.clear();
}
};
int main() {
// Admin Purposes
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
Graph g;
// Reading in the inputs
int n; cin >> n; cin.ignore();
for (int i = 0; i < n; ++i) {
string str, file, dependency; getline(cin, str);
// Filtering out and retrieving the first file
stringstream ss(str);
ss >> file;
file.pop_back();
while (ss >> dependency) g.add_edge(file, dependency);
}
string file; cin >> file;
g.toposort(file);
return 0;
}