-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210310_BOJ_1516.cpp
More file actions
73 lines (54 loc) · 968 Bytes
/
Copy path210310_BOJ_1516.cpp
File metadata and controls
73 lines (54 loc) · 968 Bytes
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
69
70
71
72
73
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N;
vector<int> E[501];
int time[501];
int res[501]{0};
int inCount[501]{0};
int init() {
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> time[i];
int from;
while (1) {
cin >> from;
if (from == -1)
break;
E[from].push_back(i);
inCount[i]++;
}
}
int Min = 1;
for (int i = 2; i <= N; ++i) {
if (inCount[Min] > inCount[i])
Min = i;
}
return Min;
}
void TopologicalSort(int start) {
res[start] = 0;
queue<int> q;
q.push(start);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = 0; i < E[x].size(); ++i) {
int y = E[x][i];
inCount[y]--;
if (inCount[y] == 0)
q.push(y);
if (res[y] < time[x] + res[x])
res[y] = time[x] + res[x];
}
}
for (int i = 1; i <= N; ++i)
cout << res[i] + time[i] << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
TopologicalSort(init());
return 0;
}