#include <iostream>
#include <vector>
using namespace std;
const int NMAX = 1e5;
const int LGMAX = 17;
struct ElectoralRoad {
int x, y, c;
};
int N, M;
vector <int> g[NMAX + 2];
vector <ElectoralRoad> lca[NMAX + 2];
int h[NMAX + 2], dp[NMAX + 2], dpSons[NMAX + 2];
int anc[NMAX + 2][LGMAX + 2];
void dfs(int node, int parent = 0) {
anc[node][0] = parent;
for(int son : g[node]) {
if(son != parent) {
h[son] = h[node] + 1;
dfs(son, node);
}
}
}
void BinaryLift() {
for(int step = 1; step <= LGMAX; step++) {
for(int i = 1; i <= N; i++) {
anc[i][step] = anc[anc[i][step - 1]][step - 1];
}
}
}
int getLca(int x, int y) {
if(h[x] < h[y]) {
swap(x, y);
}
for(int step = LGMAX; step >= 0; step--) {
if(h[anc[x][step]] >= h[y]) {
x = anc[x][step];
}
}
if(x == y) {
return x;
}
for(int step = LGMAX; step >= 0; step--) {
if(anc[x][step] != anc[y][step]) {
x = anc[x][step];
y = anc[y][step];
}
}
return anc[x][0];
}
void solve(int node, int parent = 0) {
int sumDpSon = 0;
for(int son : g[node]) {
if(son != parent) {
solve(son, node);
sumDpSon += dp[son];
}
}
for(int i = LGMAX; i >= 1; i--) {
dpSons[node] = sumDpSon;
}
/*
for(ElectoralRoad road : lca[node]) {
int newDp = road.c;
pair <int, int> crossedKids;
if(node != road.x) {
}
if(node != road.y) {
}
dp[node] = max(dp[node], newDp);
}
*/
dp[node] = max(dp[node], dpSons[node]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for(int i = 1; i < N; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
h[1] = 1;
dfs(1);
BinaryLift();
cin >> M;
for(int i = 1; i <= M; i++) {
int x, y, c;
cin >> x >> y >> c;
lca[getLca(x, y)].push_back({x, y, c});
}
solve(1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
180 ms |
20900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |