#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int MXN = 2e5 + 20 , LOG = 20;
vector<int> adj[MXN];
int h[MXN] , sz[MXN] , sttime[MXN] , p[LOG][MXN] , zaman;
int from[MXN] , to[MXN] , c[MXN];
ll dp[MXN] , pd[MXN];
vector<int> d[MXN];
int n,m;
struct fenwick {
int size;
vector<int> bit;
fenwick() {
size = 0;
}
void init(int n) {
size = n;
bit.assign(size , 0);
}
void add(int i , int k) {
for(; i < size ; i |= (i+1)) {
bit[i] += k;
}
}
void add(int l , int r , int k) {
add(l , k); add(r , -k);
}
int operator[](int i) {
int res = 0;
for(; i >= 0 ; i = (i&(i+1))-1) {
res += bit[i];
}
return res;
}
} f;
void prep(int v , int dad) {
p[0][v] = (dad == -1 ? v : dad);
h[v] = (dad == -1 ? 0 : h[dad]+1);
sz[v] = 1;
sttime[v] = zaman++;
for(auto u : adj[v]) {
if(u == dad) continue;
prep(u , v);
sz[v] += sz[u];
}
}
int kth_anc(int v , int k) {
while(k > 0) {
int x = __builtin_ctz(k);
v = p[x][v];
k -= (1 << x);
}
return v;
}
int LCA(int v , int u) {
if(h[v] > h[u]) swap(v , u);
u = kth_anc(u , h[u]-h[v]);
if(v == u) return v;
for(int i = LOG-1 ; i >= 0 ; i--) {
if(p[i][v] != p[i][u]) {
v = p[i][v]; u = p[i][u];
}
}
return p[0][v];
}
void DFS(int v , int dad) {
for(auto u : adj[v]) {
if(u == dad) continue;
DFS(u , v); pd[v] += dp[u];
}
dp[v] = pd[v];
for(auto p : d[v]) {
dp[v] = max(dp[v] , c[p] + pd[v] + f[sttime[from[p]]]-f[sttime[v]] + f[sttime[to[p]]]-f[sttime[v]]);
}
f.add(sttime[v] , sttime[v]+sz[v] , pd[v]-dp[v]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for(int i = 1 ; i < n ; i++) {
int v,u; cin >> v >> u;
adj[v].push_back(u); adj[u].push_back(v);
}
prep(1 , -1);
for(int i = 1 ; i < LOG ; i++) {
for(int j = 1 ; j <= n ; j++) {
p[i][j] = p[i-1][p[i-1][j]];
}
}
cin >> m;
for(int i = 0 ; i < m ; i++) {
cin >> from[i] >> to[i] >> c[i];
d[LCA(from[i] , to[i])].push_back(i);
}
f.init(n);
DFS(1 , -1);
cout << dp[1] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9804 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
7 ms |
9804 KB |
Output is correct |
4 |
Correct |
5 ms |
10060 KB |
Output is correct |
5 |
Correct |
74 ms |
35228 KB |
Output is correct |
6 |
Correct |
46 ms |
42224 KB |
Output is correct |
7 |
Correct |
73 ms |
40024 KB |
Output is correct |
8 |
Correct |
56 ms |
34556 KB |
Output is correct |
9 |
Correct |
80 ms |
38376 KB |
Output is correct |
10 |
Correct |
79 ms |
34556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9804 KB |
Output is correct |
2 |
Correct |
5 ms |
9920 KB |
Output is correct |
3 |
Correct |
6 ms |
10112 KB |
Output is correct |
4 |
Correct |
107 ms |
46296 KB |
Output is correct |
5 |
Correct |
114 ms |
46312 KB |
Output is correct |
6 |
Correct |
128 ms |
46404 KB |
Output is correct |
7 |
Correct |
125 ms |
46324 KB |
Output is correct |
8 |
Correct |
104 ms |
46284 KB |
Output is correct |
9 |
Correct |
97 ms |
46300 KB |
Output is correct |
10 |
Correct |
157 ms |
46344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9804 KB |
Output is correct |
2 |
Correct |
5 ms |
9920 KB |
Output is correct |
3 |
Correct |
6 ms |
10112 KB |
Output is correct |
4 |
Correct |
107 ms |
46296 KB |
Output is correct |
5 |
Correct |
114 ms |
46312 KB |
Output is correct |
6 |
Correct |
128 ms |
46404 KB |
Output is correct |
7 |
Correct |
125 ms |
46324 KB |
Output is correct |
8 |
Correct |
104 ms |
46284 KB |
Output is correct |
9 |
Correct |
97 ms |
46300 KB |
Output is correct |
10 |
Correct |
157 ms |
46344 KB |
Output is correct |
11 |
Correct |
12 ms |
11296 KB |
Output is correct |
12 |
Correct |
115 ms |
46312 KB |
Output is correct |
13 |
Correct |
119 ms |
46288 KB |
Output is correct |
14 |
Correct |
144 ms |
46496 KB |
Output is correct |
15 |
Correct |
114 ms |
46276 KB |
Output is correct |
16 |
Correct |
100 ms |
46492 KB |
Output is correct |
17 |
Correct |
109 ms |
46256 KB |
Output is correct |
18 |
Correct |
160 ms |
46276 KB |
Output is correct |
19 |
Correct |
109 ms |
46436 KB |
Output is correct |
20 |
Correct |
107 ms |
46268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
37436 KB |
Output is correct |
2 |
Correct |
105 ms |
46396 KB |
Output is correct |
3 |
Correct |
269 ms |
43568 KB |
Output is correct |
4 |
Correct |
166 ms |
38108 KB |
Output is correct |
5 |
Correct |
191 ms |
43204 KB |
Output is correct |
6 |
Correct |
176 ms |
38220 KB |
Output is correct |
7 |
Correct |
260 ms |
42864 KB |
Output is correct |
8 |
Correct |
195 ms |
38896 KB |
Output is correct |
9 |
Correct |
98 ms |
46332 KB |
Output is correct |
10 |
Correct |
256 ms |
41872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9804 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
7 ms |
9804 KB |
Output is correct |
4 |
Correct |
5 ms |
10060 KB |
Output is correct |
5 |
Correct |
74 ms |
35228 KB |
Output is correct |
6 |
Correct |
46 ms |
42224 KB |
Output is correct |
7 |
Correct |
73 ms |
40024 KB |
Output is correct |
8 |
Correct |
56 ms |
34556 KB |
Output is correct |
9 |
Correct |
80 ms |
38376 KB |
Output is correct |
10 |
Correct |
79 ms |
34556 KB |
Output is correct |
11 |
Correct |
7 ms |
10132 KB |
Output is correct |
12 |
Correct |
7 ms |
10188 KB |
Output is correct |
13 |
Correct |
6 ms |
10244 KB |
Output is correct |
14 |
Correct |
6 ms |
10188 KB |
Output is correct |
15 |
Correct |
7 ms |
10184 KB |
Output is correct |
16 |
Correct |
7 ms |
10120 KB |
Output is correct |
17 |
Correct |
6 ms |
10188 KB |
Output is correct |
18 |
Correct |
7 ms |
10116 KB |
Output is correct |
19 |
Correct |
7 ms |
10188 KB |
Output is correct |
20 |
Correct |
7 ms |
10256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9804 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
7 ms |
9804 KB |
Output is correct |
4 |
Correct |
5 ms |
10060 KB |
Output is correct |
5 |
Correct |
74 ms |
35228 KB |
Output is correct |
6 |
Correct |
46 ms |
42224 KB |
Output is correct |
7 |
Correct |
73 ms |
40024 KB |
Output is correct |
8 |
Correct |
56 ms |
34556 KB |
Output is correct |
9 |
Correct |
80 ms |
38376 KB |
Output is correct |
10 |
Correct |
79 ms |
34556 KB |
Output is correct |
11 |
Correct |
6 ms |
9804 KB |
Output is correct |
12 |
Correct |
5 ms |
9920 KB |
Output is correct |
13 |
Correct |
6 ms |
10112 KB |
Output is correct |
14 |
Correct |
107 ms |
46296 KB |
Output is correct |
15 |
Correct |
114 ms |
46312 KB |
Output is correct |
16 |
Correct |
128 ms |
46404 KB |
Output is correct |
17 |
Correct |
125 ms |
46324 KB |
Output is correct |
18 |
Correct |
104 ms |
46284 KB |
Output is correct |
19 |
Correct |
97 ms |
46300 KB |
Output is correct |
20 |
Correct |
157 ms |
46344 KB |
Output is correct |
21 |
Correct |
12 ms |
11296 KB |
Output is correct |
22 |
Correct |
115 ms |
46312 KB |
Output is correct |
23 |
Correct |
119 ms |
46288 KB |
Output is correct |
24 |
Correct |
144 ms |
46496 KB |
Output is correct |
25 |
Correct |
114 ms |
46276 KB |
Output is correct |
26 |
Correct |
100 ms |
46492 KB |
Output is correct |
27 |
Correct |
109 ms |
46256 KB |
Output is correct |
28 |
Correct |
160 ms |
46276 KB |
Output is correct |
29 |
Correct |
109 ms |
46436 KB |
Output is correct |
30 |
Correct |
107 ms |
46268 KB |
Output is correct |
31 |
Correct |
276 ms |
37436 KB |
Output is correct |
32 |
Correct |
105 ms |
46396 KB |
Output is correct |
33 |
Correct |
269 ms |
43568 KB |
Output is correct |
34 |
Correct |
166 ms |
38108 KB |
Output is correct |
35 |
Correct |
191 ms |
43204 KB |
Output is correct |
36 |
Correct |
176 ms |
38220 KB |
Output is correct |
37 |
Correct |
260 ms |
42864 KB |
Output is correct |
38 |
Correct |
195 ms |
38896 KB |
Output is correct |
39 |
Correct |
98 ms |
46332 KB |
Output is correct |
40 |
Correct |
256 ms |
41872 KB |
Output is correct |
41 |
Correct |
7 ms |
10132 KB |
Output is correct |
42 |
Correct |
7 ms |
10188 KB |
Output is correct |
43 |
Correct |
6 ms |
10244 KB |
Output is correct |
44 |
Correct |
6 ms |
10188 KB |
Output is correct |
45 |
Correct |
7 ms |
10184 KB |
Output is correct |
46 |
Correct |
7 ms |
10120 KB |
Output is correct |
47 |
Correct |
6 ms |
10188 KB |
Output is correct |
48 |
Correct |
7 ms |
10116 KB |
Output is correct |
49 |
Correct |
7 ms |
10188 KB |
Output is correct |
50 |
Correct |
7 ms |
10256 KB |
Output is correct |
51 |
Correct |
217 ms |
38920 KB |
Output is correct |
52 |
Correct |
150 ms |
46340 KB |
Output is correct |
53 |
Correct |
260 ms |
42028 KB |
Output is correct |
54 |
Correct |
181 ms |
37952 KB |
Output is correct |
55 |
Correct |
265 ms |
38716 KB |
Output is correct |
56 |
Correct |
114 ms |
46356 KB |
Output is correct |
57 |
Correct |
197 ms |
42740 KB |
Output is correct |
58 |
Correct |
147 ms |
38120 KB |
Output is correct |
59 |
Correct |
209 ms |
38948 KB |
Output is correct |
60 |
Correct |
129 ms |
46276 KB |
Output is correct |
61 |
Correct |
180 ms |
42884 KB |
Output is correct |
62 |
Correct |
160 ms |
37948 KB |
Output is correct |
63 |
Correct |
265 ms |
38608 KB |
Output is correct |
64 |
Correct |
124 ms |
46252 KB |
Output is correct |
65 |
Correct |
260 ms |
42600 KB |
Output is correct |
66 |
Correct |
188 ms |
38208 KB |
Output is correct |
67 |
Correct |
262 ms |
38872 KB |
Output is correct |
68 |
Correct |
118 ms |
46304 KB |
Output is correct |
69 |
Correct |
202 ms |
41616 KB |
Output is correct |
70 |
Correct |
158 ms |
38316 KB |
Output is correct |