#include <bits/stdc++.h>
#define N 100005
using namespace std;
#define ll long long
#define ff first
#define ss second
ll bit[N];
ll get(int v){
ll s = 0;
while(v){
s += bit[v];
v -= v & -v;
}
return s;
}
void update(int x, ll v){
while(x < N){
bit[x] += v;
x += x & -x;
}
}
vector<int> adj[N];
int in[N], out[N], p[N][20];
int d[N];
int n;
int cnt;
void dfs2(int v, int par){
in[v] = ++cnt;
d[v] = d[par] + 1;
p[v][0] = par;
for(int i = 0; p[v][i] != 0; i++){
p[v][i + 1] = p[p[v][i]][i];
}
for(int& x : adj[v]){
if(x == par) continue;
dfs2(x, v);
}
out[v] = cnt;
}
int find(int v, int u){
if(d[v] < d[u]) swap(v, u);
for(int i = 0; d[v] - d[u]; i++)
if((d[v] - d[u]) & (1 << i))
v = p[v][i];
if(v == u) return v;
for(int i = 18; i >= 0; i--){
if(p[v][i] != p[u][i]){
v = p[v][i];
u = p[u][i];
}
}
return p[v][0];
}
vector<pair<pair<int, int>, int> > obj[N];
ll is1[N], is0[N];
void dfs(int v, int p){
ll res = 0;
for(int& x : adj[v]){
if(x == p) continue;
dfs(x, v);
res += is1[x];
}
is1[v] = is0[v] = res;
for(auto& x : obj[v])
is1[v] = max(is1[v], res + x.ss + get(in[x.ff.ff]) + get(in[x.ff.ss]));
update(in[v], res - is1[v]);
update(out[v] + 1, is1[v] - res);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int v, u, i = 1; i < n; i++){
cin>>v>>u;
adj[v].push_back(u);
adj[u].push_back(v);
}
dfs2(1, 0);
cin>>n;
while(n--){
int a, b, c;
cin>>a>>b>>c;
obj[find(a, b)].push_back({{a, b}, c});
}
dfs(1, 0);
cout<<is1[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
92 ms |
20856 KB |
Output is correct |
6 |
Correct |
69 ms |
28408 KB |
Output is correct |
7 |
Correct |
103 ms |
25816 KB |
Output is correct |
8 |
Correct |
70 ms |
21112 KB |
Output is correct |
9 |
Correct |
97 ms |
24184 KB |
Output is correct |
10 |
Correct |
74 ms |
21112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5368 KB |
Output is correct |
4 |
Correct |
149 ms |
31804 KB |
Output is correct |
5 |
Correct |
141 ms |
31736 KB |
Output is correct |
6 |
Correct |
128 ms |
31876 KB |
Output is correct |
7 |
Correct |
143 ms |
31736 KB |
Output is correct |
8 |
Correct |
143 ms |
31740 KB |
Output is correct |
9 |
Correct |
131 ms |
31832 KB |
Output is correct |
10 |
Correct |
144 ms |
31736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5368 KB |
Output is correct |
4 |
Correct |
149 ms |
31804 KB |
Output is correct |
5 |
Correct |
141 ms |
31736 KB |
Output is correct |
6 |
Correct |
128 ms |
31876 KB |
Output is correct |
7 |
Correct |
143 ms |
31736 KB |
Output is correct |
8 |
Correct |
143 ms |
31740 KB |
Output is correct |
9 |
Correct |
131 ms |
31832 KB |
Output is correct |
10 |
Correct |
144 ms |
31736 KB |
Output is correct |
11 |
Correct |
18 ms |
6136 KB |
Output is correct |
12 |
Correct |
150 ms |
32008 KB |
Output is correct |
13 |
Correct |
155 ms |
32084 KB |
Output is correct |
14 |
Correct |
129 ms |
32120 KB |
Output is correct |
15 |
Correct |
150 ms |
32120 KB |
Output is correct |
16 |
Correct |
135 ms |
32188 KB |
Output is correct |
17 |
Correct |
144 ms |
32044 KB |
Output is correct |
18 |
Correct |
149 ms |
32016 KB |
Output is correct |
19 |
Correct |
139 ms |
32212 KB |
Output is correct |
20 |
Correct |
160 ms |
32168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
23728 KB |
Output is correct |
2 |
Correct |
133 ms |
31844 KB |
Output is correct |
3 |
Correct |
202 ms |
28664 KB |
Output is correct |
4 |
Correct |
122 ms |
24024 KB |
Output is correct |
5 |
Correct |
191 ms |
28280 KB |
Output is correct |
6 |
Correct |
128 ms |
24196 KB |
Output is correct |
7 |
Correct |
215 ms |
27956 KB |
Output is correct |
8 |
Correct |
149 ms |
23840 KB |
Output is correct |
9 |
Correct |
131 ms |
31992 KB |
Output is correct |
10 |
Correct |
211 ms |
27052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
92 ms |
20856 KB |
Output is correct |
6 |
Correct |
69 ms |
28408 KB |
Output is correct |
7 |
Correct |
103 ms |
25816 KB |
Output is correct |
8 |
Correct |
70 ms |
21112 KB |
Output is correct |
9 |
Correct |
97 ms |
24184 KB |
Output is correct |
10 |
Correct |
74 ms |
21112 KB |
Output is correct |
11 |
Correct |
7 ms |
5240 KB |
Output is correct |
12 |
Correct |
7 ms |
5368 KB |
Output is correct |
13 |
Correct |
7 ms |
5240 KB |
Output is correct |
14 |
Correct |
7 ms |
5240 KB |
Output is correct |
15 |
Correct |
7 ms |
5240 KB |
Output is correct |
16 |
Correct |
7 ms |
5240 KB |
Output is correct |
17 |
Correct |
7 ms |
5244 KB |
Output is correct |
18 |
Correct |
8 ms |
5368 KB |
Output is correct |
19 |
Correct |
7 ms |
5368 KB |
Output is correct |
20 |
Correct |
7 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
92 ms |
20856 KB |
Output is correct |
6 |
Correct |
69 ms |
28408 KB |
Output is correct |
7 |
Correct |
103 ms |
25816 KB |
Output is correct |
8 |
Correct |
70 ms |
21112 KB |
Output is correct |
9 |
Correct |
97 ms |
24184 KB |
Output is correct |
10 |
Correct |
74 ms |
21112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5368 KB |
Output is correct |
14 |
Correct |
149 ms |
31804 KB |
Output is correct |
15 |
Correct |
141 ms |
31736 KB |
Output is correct |
16 |
Correct |
128 ms |
31876 KB |
Output is correct |
17 |
Correct |
143 ms |
31736 KB |
Output is correct |
18 |
Correct |
143 ms |
31740 KB |
Output is correct |
19 |
Correct |
131 ms |
31832 KB |
Output is correct |
20 |
Correct |
144 ms |
31736 KB |
Output is correct |
21 |
Correct |
18 ms |
6136 KB |
Output is correct |
22 |
Correct |
150 ms |
32008 KB |
Output is correct |
23 |
Correct |
155 ms |
32084 KB |
Output is correct |
24 |
Correct |
129 ms |
32120 KB |
Output is correct |
25 |
Correct |
150 ms |
32120 KB |
Output is correct |
26 |
Correct |
135 ms |
32188 KB |
Output is correct |
27 |
Correct |
144 ms |
32044 KB |
Output is correct |
28 |
Correct |
149 ms |
32016 KB |
Output is correct |
29 |
Correct |
139 ms |
32212 KB |
Output is correct |
30 |
Correct |
160 ms |
32168 KB |
Output is correct |
31 |
Correct |
167 ms |
23728 KB |
Output is correct |
32 |
Correct |
133 ms |
31844 KB |
Output is correct |
33 |
Correct |
202 ms |
28664 KB |
Output is correct |
34 |
Correct |
122 ms |
24024 KB |
Output is correct |
35 |
Correct |
191 ms |
28280 KB |
Output is correct |
36 |
Correct |
128 ms |
24196 KB |
Output is correct |
37 |
Correct |
215 ms |
27956 KB |
Output is correct |
38 |
Correct |
149 ms |
23840 KB |
Output is correct |
39 |
Correct |
131 ms |
31992 KB |
Output is correct |
40 |
Correct |
211 ms |
27052 KB |
Output is correct |
41 |
Correct |
7 ms |
5240 KB |
Output is correct |
42 |
Correct |
7 ms |
5368 KB |
Output is correct |
43 |
Correct |
7 ms |
5240 KB |
Output is correct |
44 |
Correct |
7 ms |
5240 KB |
Output is correct |
45 |
Correct |
7 ms |
5240 KB |
Output is correct |
46 |
Correct |
7 ms |
5240 KB |
Output is correct |
47 |
Correct |
7 ms |
5244 KB |
Output is correct |
48 |
Correct |
8 ms |
5368 KB |
Output is correct |
49 |
Correct |
7 ms |
5368 KB |
Output is correct |
50 |
Correct |
7 ms |
5368 KB |
Output is correct |
51 |
Correct |
163 ms |
24156 KB |
Output is correct |
52 |
Correct |
153 ms |
32024 KB |
Output is correct |
53 |
Correct |
220 ms |
27412 KB |
Output is correct |
54 |
Correct |
131 ms |
24244 KB |
Output is correct |
55 |
Correct |
161 ms |
23980 KB |
Output is correct |
56 |
Correct |
148 ms |
32120 KB |
Output is correct |
57 |
Correct |
214 ms |
28180 KB |
Output is correct |
58 |
Correct |
140 ms |
24240 KB |
Output is correct |
59 |
Correct |
177 ms |
24152 KB |
Output is correct |
60 |
Correct |
152 ms |
32012 KB |
Output is correct |
61 |
Correct |
187 ms |
28152 KB |
Output is correct |
62 |
Correct |
132 ms |
24064 KB |
Output is correct |
63 |
Correct |
161 ms |
23768 KB |
Output is correct |
64 |
Correct |
155 ms |
32136 KB |
Output is correct |
65 |
Correct |
240 ms |
28080 KB |
Output is correct |
66 |
Correct |
131 ms |
24372 KB |
Output is correct |
67 |
Correct |
161 ms |
23784 KB |
Output is correct |
68 |
Correct |
148 ms |
32104 KB |
Output is correct |
69 |
Correct |
198 ms |
27128 KB |
Output is correct |
70 |
Correct |
137 ms |
24412 KB |
Output is correct |