#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif
#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int MAXN = 1e5;
int N;
vi adj[MAXN];
int depth[MAXN], jmp[MAXN][17], tin[MAXN], tout[MAXN], tt;
vector<ar<int, 3>> paths[MAXN];
i64 dp[MAXN];
struct {
i64 t[MAXN];
void add(int i, i64 x) {
while (i < MAXN) {
t[i] += x;
i |= i+1;
}
}
i64 qry(int i) {
i64 x = 0;
while (i >= 0) {
x += t[i];
i &= i+1, i--;
}
return x;
}
void add(int l, int r, i64 x) {
add(l, x), add(r+1, -x);
}
} T;
void dfs(int i) {
tin[i] = tt++;
for (int l = 1; l < 17; l++) jmp[i][l] = jmp[jmp[i][l-1]][l-1];
for (int j : adj[i]) if (jmp[i][0]^j) {
depth[j] = depth[i]+1;
jmp[j][0] = i;
dfs(j);
}
tout[i] = tt-1;
}
int jump(int i, int d) {
for (int l = 0; l < 17; l++) if (d>>l&1) i = jmp[i][l];
return i;
}
int lca(int i, int j) {
if (depth[i] < depth[j]) swap(i, j);
i = jump(i, depth[i]-depth[j]);
if (i == j) return i;
for (int l = 16; l >= 0; l--) if (jmp[i][l]^jmp[j][l]) i = jmp[i][l], j = jmp[j][l];
return jmp[i][0];
}
void rec(int i) {
for (int j : adj[i]) if (jmp[i][0]^j) rec(j);
i64 sum = 0;
for (int j : adj[i]) if (jmp[i][0]^j) {
T.add(tin[i]+1, tout[i], dp[j]);
T.add(tin[j], tout[j], -dp[j]);
sum += dp[j];
}
dp[i] = sum;
for (auto [x, y, w] : paths[i]) dp[i] = max(dp[i], w+T.qry(tin[x])+T.qry(tin[y])-sum);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> N;
for (int i = 0; i < N-1; i++) {
int x, y; cin >> x >> y, x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
jmp[0][0] = 0;
dfs(0);
int M; cin >> M;
while (M--) {
int x, y, w; cin >> x >> y >> w, x--, y--;
int z = lca(x, y);
paths[z].push_back({x, y, w});
}
rec(0);
cout << dp[0] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7432 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7432 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
21648 KB |
Output is correct |
2 |
Incorrect |
79 ms |
29956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |