#include <bits/stdc++.h>
#include <fstream>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define mod 1000000007
#define INF 1000000000
#define INF2 2000000000
///#define cin fin
///#define cout fout
using namespace std;
double const EPS = 1e-14;
const int P = 1007;
typedef long long ll;
///ofstream fout("herding.out");
///ifstream fin("herding.in");
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // find_by_order, order_of_key
const int N = 1e5 + 5;
const int M = 2e5 + 5;
struct Plan {
ll a, b, c;
};
pair<ll,ll> seg[M*4];
vector<int> v[N];
vector<Plan> plan[N];
ll dp[N], sum[N];
int st[N], ed[N];
int dep[N], lca[N][30];
int tim = 1;
void Update(int x, int l, int r, int pos, pair<ll,ll> val) {
if(l == r) {
seg[x].first = val.first;
seg[x].second = val.second;
}
else {
int mid = (l+r)/2;
if(pos <= mid) Update(x*2,l,mid,pos,val);
else Update(x*2+1,mid+1,r,pos,val);
seg[x].first = seg[x*2].first+seg[x*2+1].first;
seg[x].second = seg[x*2].second+seg[x*2+1].second;
}
}
pair<ll,ll> Sum(int x, int l, int r, int L, int R) {
if(r < L || l > R) {
return {0,0};
}
else if(l >= L && r <= R) {
return seg[x];
}
else {
int mid = (l+r)/2;
pair<ll,ll> val1 = Sum(x*2,l,mid,L,R), val2 = Sum(x*2+1,mid+1,r,L,R);
pair<ll,ll> val3;
val3.first = val1.first+val2.first;
val3.second = val1.second+val2.second;
return val3;
}
}
void dfs(int s, int p) {
for(int i = 1; (1<<i) <= dep[s]; i++) {
lca[s][i] = lca[lca[s][i-1]][i-1];
}
st[s] = tim; tim++;
for(auto i : v[s]) {
if(i != p) {
dep[i] += dep[s]+1;
lca[i][0] = s;
dfs(i,s);
}
}
ed[s] = tim; tim++;
}
void sol(int s, int p) {
for(auto i : v[s]) {
if(i != p) {
sol(i,s);
sum[s] += dp[i];
}
}
dp[s] = max(dp[s],sum[s]);
for(auto i : plan[s]) {
int a = i.a, b = i.b;
if(s == a || s == b) {
if(st[a] > st[b]) swap(a,b);
pair<ll,ll> tota = Sum(1,1,M,1,st[a]), totb = Sum(1,1,M,1,st[b]);
ll tot1 = totb.second-tota.second+sum[s], tot2 = totb.first-tota.first;
dp[s] = max(dp[s],tot1-tot2+i.c);
}
else {
pair<ll,ll> tota = Sum(1,1,M,1,st[a]), totb = Sum(1,1,M,1,st[b]), tots = Sum(1,1,M,1,st[s]);
ll tot1 = totb.second+tota.second - (2ll*tots.second) + sum[s], tot2 = totb.first+tota.first - (2ll*tots.first);
dp[s] = max(dp[s],tot1-tot2+i.c);
}
}
pair<ll,ll> val; val.first = dp[s], val.second = sum[s];
Update(1,1,M,st[s],val);
val.first *= -1ll; val.second *= -1ll;
Update(1,1,M,ed[s],val);
}
int fin(int x, int y) {
if(dep[x] < dep[y]) swap(x,y);
int pw2 = 0;
while(dep[x]-dep[y] > 0) {
if(((dep[x]-dep[y])&(1<<pw2)) > 0) {
x = lca[x][pw2];
}
pw2++;
}
while(x != y) {
pw2 = 29;
while(lca[x][pw2] == lca[y][pw2] && pw2 > 0) pw2--;
x = lca[x][pw2];
y = lca[y][pw2];
}
return x;
}
int main()
{
ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
int n; cin >> n;
for(int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1,0);
int m; cin >> m;
for(int i = 0; i < m; i++) {
Plan cur; cin >> cur.a >> cur.b >> cur.c;
int d = fin(cur.a,cur.b);
plan[d].push_back(cur);
}
sol(1,0);
cout << dp[1] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
73 ms |
33620 KB |
Output is correct |
6 |
Correct |
55 ms |
42832 KB |
Output is correct |
7 |
Correct |
86 ms |
39760 KB |
Output is correct |
8 |
Correct |
57 ms |
34128 KB |
Output is correct |
9 |
Correct |
91 ms |
37716 KB |
Output is correct |
10 |
Correct |
55 ms |
34152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
124 ms |
47800 KB |
Output is correct |
5 |
Correct |
122 ms |
47696 KB |
Output is correct |
6 |
Correct |
117 ms |
47824 KB |
Output is correct |
7 |
Correct |
137 ms |
47876 KB |
Output is correct |
8 |
Correct |
121 ms |
47700 KB |
Output is correct |
9 |
Correct |
124 ms |
47732 KB |
Output is correct |
10 |
Correct |
125 ms |
47848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
124 ms |
47800 KB |
Output is correct |
5 |
Correct |
122 ms |
47696 KB |
Output is correct |
6 |
Correct |
117 ms |
47824 KB |
Output is correct |
7 |
Correct |
137 ms |
47876 KB |
Output is correct |
8 |
Correct |
121 ms |
47700 KB |
Output is correct |
9 |
Correct |
124 ms |
47732 KB |
Output is correct |
10 |
Correct |
125 ms |
47848 KB |
Output is correct |
11 |
Correct |
13 ms |
14168 KB |
Output is correct |
12 |
Correct |
136 ms |
47956 KB |
Output is correct |
13 |
Correct |
128 ms |
48052 KB |
Output is correct |
14 |
Correct |
112 ms |
48016 KB |
Output is correct |
15 |
Correct |
127 ms |
47956 KB |
Output is correct |
16 |
Correct |
116 ms |
48176 KB |
Output is correct |
17 |
Correct |
132 ms |
47992 KB |
Output is correct |
18 |
Correct |
125 ms |
47956 KB |
Output is correct |
19 |
Correct |
121 ms |
48180 KB |
Output is correct |
20 |
Correct |
128 ms |
48060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
37868 KB |
Output is correct |
2 |
Correct |
112 ms |
48140 KB |
Output is correct |
3 |
Correct |
188 ms |
44172 KB |
Output is correct |
4 |
Correct |
127 ms |
38448 KB |
Output is correct |
5 |
Correct |
166 ms |
43296 KB |
Output is correct |
6 |
Correct |
138 ms |
38628 KB |
Output is correct |
7 |
Correct |
195 ms |
43020 KB |
Output is correct |
8 |
Correct |
138 ms |
38228 KB |
Output is correct |
9 |
Correct |
109 ms |
47700 KB |
Output is correct |
10 |
Correct |
216 ms |
41732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
73 ms |
33620 KB |
Output is correct |
6 |
Correct |
55 ms |
42832 KB |
Output is correct |
7 |
Correct |
86 ms |
39760 KB |
Output is correct |
8 |
Correct |
57 ms |
34128 KB |
Output is correct |
9 |
Correct |
91 ms |
37716 KB |
Output is correct |
10 |
Correct |
55 ms |
34152 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
13000 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
73 ms |
33620 KB |
Output is correct |
6 |
Correct |
55 ms |
42832 KB |
Output is correct |
7 |
Correct |
86 ms |
39760 KB |
Output is correct |
8 |
Correct |
57 ms |
34128 KB |
Output is correct |
9 |
Correct |
91 ms |
37716 KB |
Output is correct |
10 |
Correct |
55 ms |
34152 KB |
Output is correct |
11 |
Correct |
3 ms |
12632 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
124 ms |
47800 KB |
Output is correct |
15 |
Correct |
122 ms |
47696 KB |
Output is correct |
16 |
Correct |
117 ms |
47824 KB |
Output is correct |
17 |
Correct |
137 ms |
47876 KB |
Output is correct |
18 |
Correct |
121 ms |
47700 KB |
Output is correct |
19 |
Correct |
124 ms |
47732 KB |
Output is correct |
20 |
Correct |
125 ms |
47848 KB |
Output is correct |
21 |
Correct |
13 ms |
14168 KB |
Output is correct |
22 |
Correct |
136 ms |
47956 KB |
Output is correct |
23 |
Correct |
128 ms |
48052 KB |
Output is correct |
24 |
Correct |
112 ms |
48016 KB |
Output is correct |
25 |
Correct |
127 ms |
47956 KB |
Output is correct |
26 |
Correct |
116 ms |
48176 KB |
Output is correct |
27 |
Correct |
132 ms |
47992 KB |
Output is correct |
28 |
Correct |
125 ms |
47956 KB |
Output is correct |
29 |
Correct |
121 ms |
48180 KB |
Output is correct |
30 |
Correct |
128 ms |
48060 KB |
Output is correct |
31 |
Correct |
155 ms |
37868 KB |
Output is correct |
32 |
Correct |
112 ms |
48140 KB |
Output is correct |
33 |
Correct |
188 ms |
44172 KB |
Output is correct |
34 |
Correct |
127 ms |
38448 KB |
Output is correct |
35 |
Correct |
166 ms |
43296 KB |
Output is correct |
36 |
Correct |
138 ms |
38628 KB |
Output is correct |
37 |
Correct |
195 ms |
43020 KB |
Output is correct |
38 |
Correct |
138 ms |
38228 KB |
Output is correct |
39 |
Correct |
109 ms |
47700 KB |
Output is correct |
40 |
Correct |
216 ms |
41732 KB |
Output is correct |
41 |
Correct |
3 ms |
12892 KB |
Output is correct |
42 |
Correct |
3 ms |
12892 KB |
Output is correct |
43 |
Correct |
3 ms |
12892 KB |
Output is correct |
44 |
Correct |
3 ms |
12892 KB |
Output is correct |
45 |
Correct |
4 ms |
13000 KB |
Output is correct |
46 |
Correct |
4 ms |
12892 KB |
Output is correct |
47 |
Correct |
3 ms |
12892 KB |
Output is correct |
48 |
Correct |
3 ms |
12892 KB |
Output is correct |
49 |
Correct |
3 ms |
12892 KB |
Output is correct |
50 |
Correct |
3 ms |
12888 KB |
Output is correct |
51 |
Correct |
140 ms |
38288 KB |
Output is correct |
52 |
Correct |
130 ms |
48076 KB |
Output is correct |
53 |
Correct |
246 ms |
42484 KB |
Output is correct |
54 |
Correct |
114 ms |
38716 KB |
Output is correct |
55 |
Correct |
159 ms |
38280 KB |
Output is correct |
56 |
Correct |
127 ms |
47952 KB |
Output is correct |
57 |
Correct |
168 ms |
43036 KB |
Output is correct |
58 |
Correct |
127 ms |
38656 KB |
Output is correct |
59 |
Correct |
145 ms |
38348 KB |
Output is correct |
60 |
Correct |
128 ms |
48044 KB |
Output is correct |
61 |
Correct |
163 ms |
43148 KB |
Output is correct |
62 |
Correct |
124 ms |
38136 KB |
Output is correct |
63 |
Correct |
170 ms |
37888 KB |
Output is correct |
64 |
Correct |
139 ms |
48032 KB |
Output is correct |
65 |
Correct |
201 ms |
42892 KB |
Output is correct |
66 |
Correct |
115 ms |
38400 KB |
Output is correct |
67 |
Correct |
153 ms |
38072 KB |
Output is correct |
68 |
Correct |
134 ms |
48036 KB |
Output is correct |
69 |
Correct |
194 ms |
41640 KB |
Output is correct |
70 |
Correct |
131 ms |
38640 KB |
Output is correct |