#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
vector<vector<int>> E(N);
for (int i = 0; i < N - 1; i++){
int X, Y;
cin >> X >> Y;
X--;
Y--;
E[X].push_back(Y);
E[Y].push_back(X);
}
int M;
cin >> M;
vector<int> A(M), B(M), C(M);
for (int i = 0; i < M; i++){
cin >> A[i] >> B[i] >> C[i];
A[i]--;
B[i]--;
}
vector<int> p(N, -1);
vector<vector<int>> c(N);
vector<int> d(N, 0);
queue<int> Q;
Q.push(0);
vector<int> bfs;
while (!Q.empty()){
int v = Q.front();
Q.pop();
bfs.push_back(v);
for (int w : E[v]){
if (w != p[v]){
p[w] = v;
c[v].push_back(w);
d[w] = d[v] + 1;
Q.push(w);
}
}
}
vector<vector<int>> path(M);
vector<vector<int>> id(N);
for (int i = 0; i < M; i++){
int u = A[i], v = B[i];
while (d[u] > d[v]){
path[i].push_back(u);
u = p[u];
}
while (d[v] > d[u]){
path[i].push_back(v);
v = p[v];
}
while (u != v){
path[i].push_back(u);
path[i].push_back(v);
u = p[u];
v = p[v];
}
path[i].push_back(u);
id[u].push_back(i);
}
reverse(bfs.begin(), bfs.end());
vector<int> dp(N, 0);
for (int v : bfs){
for (int w : id[v]){
vector<bool> in_path(N, false);
for (int x : path[w]){
in_path[x] = true;
}
int sum = 0;
for (int x : path[w]){
for (int y : c[x]){
if (!in_path[y]){
sum += dp[y];
}
}
}
dp[v] = max(dp[v], sum + C[w]);
}
int sum = 0;
for (int w : c[v]){
sum += dp[w];
}
dp[v] = max(dp[v], sum);
}
cout << dp[0] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
116 ms |
13668 KB |
Output is correct |
6 |
Correct |
74 ms |
15688 KB |
Output is correct |
7 |
Correct |
120 ms |
15068 KB |
Output is correct |
8 |
Correct |
110 ms |
13160 KB |
Output is correct |
9 |
Correct |
113 ms |
16256 KB |
Output is correct |
10 |
Correct |
77 ms |
13352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Runtime error |
562 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Runtime error |
562 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
367 ms |
31012 KB |
Output is correct |
2 |
Runtime error |
544 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
116 ms |
13668 KB |
Output is correct |
6 |
Correct |
74 ms |
15688 KB |
Output is correct |
7 |
Correct |
120 ms |
15068 KB |
Output is correct |
8 |
Correct |
110 ms |
13160 KB |
Output is correct |
9 |
Correct |
113 ms |
16256 KB |
Output is correct |
10 |
Correct |
77 ms |
13352 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
2584 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
7 ms |
1748 KB |
Output is correct |
19 |
Correct |
3 ms |
480 KB |
Output is correct |
20 |
Correct |
7 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
116 ms |
13668 KB |
Output is correct |
6 |
Correct |
74 ms |
15688 KB |
Output is correct |
7 |
Correct |
120 ms |
15068 KB |
Output is correct |
8 |
Correct |
110 ms |
13160 KB |
Output is correct |
9 |
Correct |
113 ms |
16256 KB |
Output is correct |
10 |
Correct |
77 ms |
13352 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Runtime error |
562 ms |
262144 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |