#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);
vector<int> dpsum(N, 0);
for (int v : bfs){
for (int w : c[v]){
dpsum[v] += dp[w];
}
int tmp = dpsum[v];
for (int w : id[v]){
vector<bool> in_path(N, false);
int sum = 0;
for (int x : path[w]){
sum += dpsum[x] - dp[x];
}
tmp = max(tmp, sum + C[w]);
}
dp[v] = tmp;
}
cout << dp[0] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
140 ms |
14148 KB |
Output is correct |
6 |
Correct |
71 ms |
16172 KB |
Output is correct |
7 |
Correct |
91 ms |
15404 KB |
Output is correct |
8 |
Correct |
74 ms |
13568 KB |
Output is correct |
9 |
Correct |
95 ms |
16696 KB |
Output is correct |
10 |
Correct |
82 ms |
13580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Runtime error |
510 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Runtime error |
510 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
31340 KB |
Output is correct |
2 |
Runtime error |
524 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
140 ms |
14148 KB |
Output is correct |
6 |
Correct |
71 ms |
16172 KB |
Output is correct |
7 |
Correct |
91 ms |
15404 KB |
Output is correct |
8 |
Correct |
74 ms |
13568 KB |
Output is correct |
9 |
Correct |
95 ms |
16696 KB |
Output is correct |
10 |
Correct |
82 ms |
13580 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
6 ms |
2388 KB |
Output is correct |
13 |
Correct |
3 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
5 ms |
1748 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
6 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
140 ms |
14148 KB |
Output is correct |
6 |
Correct |
71 ms |
16172 KB |
Output is correct |
7 |
Correct |
91 ms |
15404 KB |
Output is correct |
8 |
Correct |
74 ms |
13568 KB |
Output is correct |
9 |
Correct |
95 ms |
16696 KB |
Output is correct |
10 |
Correct |
82 ms |
13580 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Runtime error |
510 ms |
262144 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |