#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int LOGN = 20;
struct {
ll t[N];
void upd(int ind, ll val) {
for (int i = ind + 1; i < N; i += i & -i) {
t[i] += val;
}
}
void upd(int l, int r, ll val) {
upd(l, val);
upd(r, -val);
}
int get(int ind) {
ll sum = 0;
for (int i = ind + 1; i > 0; i -= i & -i) {
sum += t[i];
}
return sum;
}
} fsum, fdp;
int n;
vector<int> g[N];
int m;
int a[N], b[N], c[N];
int tin[N], tout[N];
int par[N];
int up[LOGN][N];
void dfs(int v, int p) {
static int t = 0;
tin[v] = t++;
par[v] = p;
if (p != v) {
g[v].erase(find(g[v].begin(), g[v].end(), p));
}
for (int u : g[v]) {
dfs(u, v);
}
tout[v] = t;
}
bool anc(int v, int u) {
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
int getlca(int v, int u) {
if (anc(v, u)) {
return v;
}
if (anc(u, v)) {
return u;
}
for (int i = LOGN - 1; i >= 0; i--) {
if (!anc(up[i][v], u)) {
v = up[i][v];
}
}
return par[v];
}
vector<int> by_lca[N];
ll dp[N];
ll sum[N];
void dfsdp(int v) {
for (int u : g[v]) {
dfsdp(u);
sum[v] += dp[u];
}
dp[v] = sum[v];
for (int i : by_lca[v]) {
ll cur = fsum.get(tin[a[i]]) + fsum.get(tin[b[i]]) + sum[v] - fdp.get(tin[a[i]]) - fdp.get(tin[b[i]]) + c[i];
dp[v] = max(dp[v], cur);
}
fsum.upd(tin[v], tout[v], sum[v]);
fdp.upd(tin[v], tout[v], dp[v]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
g[x].push_back(y);
g[y].push_back(x);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
a[i]--;
b[i]--;
}
dfs(0, 0);
for (int i = 0; i < n; i++) {
up[0][i] = par[i];
}
for (int i = 1; i < LOGN; i++) {
for (int j = 0; j < n; j++) {
up[i][j] = up[i - 1][up[i - 1][j]];
}
}
for (int i = 0; i < m; i++) {
by_lca[getlca(a[i], b[i])].push_back(i);
}
dfsdp(0);
cout << dp[0] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5196 KB |
Output is correct |
2 |
Correct |
3 ms |
5156 KB |
Output is correct |
3 |
Correct |
3 ms |
5188 KB |
Output is correct |
4 |
Correct |
5 ms |
5324 KB |
Output is correct |
5 |
Correct |
73 ms |
21536 KB |
Output is correct |
6 |
Correct |
47 ms |
29128 KB |
Output is correct |
7 |
Correct |
83 ms |
26544 KB |
Output is correct |
8 |
Correct |
52 ms |
21164 KB |
Output is correct |
9 |
Correct |
60 ms |
24924 KB |
Output is correct |
10 |
Correct |
55 ms |
21096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5156 KB |
Output is correct |
2 |
Correct |
3 ms |
5196 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
86 ms |
32744 KB |
Output is correct |
5 |
Correct |
92 ms |
32772 KB |
Output is correct |
6 |
Correct |
81 ms |
32840 KB |
Output is correct |
7 |
Correct |
84 ms |
32776 KB |
Output is correct |
8 |
Correct |
78 ms |
32864 KB |
Output is correct |
9 |
Correct |
75 ms |
32836 KB |
Output is correct |
10 |
Correct |
85 ms |
32820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5156 KB |
Output is correct |
2 |
Correct |
3 ms |
5196 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
86 ms |
32744 KB |
Output is correct |
5 |
Correct |
92 ms |
32772 KB |
Output is correct |
6 |
Correct |
81 ms |
32840 KB |
Output is correct |
7 |
Correct |
84 ms |
32776 KB |
Output is correct |
8 |
Correct |
78 ms |
32864 KB |
Output is correct |
9 |
Correct |
75 ms |
32836 KB |
Output is correct |
10 |
Correct |
85 ms |
32820 KB |
Output is correct |
11 |
Correct |
12 ms |
6056 KB |
Output is correct |
12 |
Incorrect |
83 ms |
33060 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
24476 KB |
Output is correct |
2 |
Correct |
77 ms |
32752 KB |
Output is correct |
3 |
Correct |
152 ms |
29708 KB |
Output is correct |
4 |
Correct |
152 ms |
24104 KB |
Output is correct |
5 |
Correct |
130 ms |
29316 KB |
Output is correct |
6 |
Correct |
123 ms |
24280 KB |
Output is correct |
7 |
Correct |
147 ms |
28960 KB |
Output is correct |
8 |
Correct |
130 ms |
24996 KB |
Output is correct |
9 |
Correct |
80 ms |
32800 KB |
Output is correct |
10 |
Correct |
146 ms |
28020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5196 KB |
Output is correct |
2 |
Correct |
3 ms |
5156 KB |
Output is correct |
3 |
Correct |
3 ms |
5188 KB |
Output is correct |
4 |
Correct |
5 ms |
5324 KB |
Output is correct |
5 |
Correct |
73 ms |
21536 KB |
Output is correct |
6 |
Correct |
47 ms |
29128 KB |
Output is correct |
7 |
Correct |
83 ms |
26544 KB |
Output is correct |
8 |
Correct |
52 ms |
21164 KB |
Output is correct |
9 |
Correct |
60 ms |
24924 KB |
Output is correct |
10 |
Correct |
55 ms |
21096 KB |
Output is correct |
11 |
Correct |
4 ms |
5300 KB |
Output is correct |
12 |
Correct |
3 ms |
5328 KB |
Output is correct |
13 |
Correct |
4 ms |
5420 KB |
Output is correct |
14 |
Correct |
4 ms |
5324 KB |
Output is correct |
15 |
Correct |
4 ms |
5324 KB |
Output is correct |
16 |
Correct |
4 ms |
5340 KB |
Output is correct |
17 |
Correct |
4 ms |
5324 KB |
Output is correct |
18 |
Correct |
4 ms |
5316 KB |
Output is correct |
19 |
Correct |
3 ms |
5304 KB |
Output is correct |
20 |
Correct |
4 ms |
5428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5196 KB |
Output is correct |
2 |
Correct |
3 ms |
5156 KB |
Output is correct |
3 |
Correct |
3 ms |
5188 KB |
Output is correct |
4 |
Correct |
5 ms |
5324 KB |
Output is correct |
5 |
Correct |
73 ms |
21536 KB |
Output is correct |
6 |
Correct |
47 ms |
29128 KB |
Output is correct |
7 |
Correct |
83 ms |
26544 KB |
Output is correct |
8 |
Correct |
52 ms |
21164 KB |
Output is correct |
9 |
Correct |
60 ms |
24924 KB |
Output is correct |
10 |
Correct |
55 ms |
21096 KB |
Output is correct |
11 |
Correct |
3 ms |
5156 KB |
Output is correct |
12 |
Correct |
3 ms |
5196 KB |
Output is correct |
13 |
Correct |
3 ms |
5324 KB |
Output is correct |
14 |
Correct |
86 ms |
32744 KB |
Output is correct |
15 |
Correct |
92 ms |
32772 KB |
Output is correct |
16 |
Correct |
81 ms |
32840 KB |
Output is correct |
17 |
Correct |
84 ms |
32776 KB |
Output is correct |
18 |
Correct |
78 ms |
32864 KB |
Output is correct |
19 |
Correct |
75 ms |
32836 KB |
Output is correct |
20 |
Correct |
85 ms |
32820 KB |
Output is correct |
21 |
Correct |
12 ms |
6056 KB |
Output is correct |
22 |
Incorrect |
83 ms |
33060 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |