#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
bool remender(ll a , ll b){return a%b;}
//freopen("problemname.in", "r", stdin);
//freopen("problemname.out", "w", stdout);
struct Fen {
vector<int> tree;
void init(int n){
tree.assign(n + 1 , 0LL);
}
void set(int i , int val , int n){
i++;
while(i <= n){
tree[i] += val;
i += (i & (-i));
}
}
int sum(int i){
i++;
ll ans = 0;
while(i > 0){
ans += tree[i];
i -= (i & (-i));
}
return ans;
}
void build(vector<int>& a){
int n = a.size();
for(int i = 0; i < n; i++){
set(i , a[i] , n);
}
}
};
struct item {
int a , b , lca , c;
};
item make(int a , int b , int lca, int c){
item it;
it.a = a;
it.b = b;
it.lca = lca;
it.c = c;
return it;
}
const int N = 100002,L=20;
int timer , tin[N],tout[N],up[N][L],ft[2*N];
vector<int> adj[N],pos[N];
vector<item> v;
int dp[N],sum[N],n;
Fen ft_dp,ft_sum;
void bl(int node , int par){
up[node][0] = par;
ft[timer] = node;
tin[node] = timer++;
for(int i = 1; i < L; i++){
up[node][i] = up[up[node][i-1]][i-1];
}
for(int i : adj[node]){
if(i!=par){
bl(i , node);
}
}
ft[timer] = node;
tout[node] = timer++;
}
bool is_lca(int x , int y){
return tin[x] <= tin[y] && tout[x] >= tout[y];
}
int find(int x , int y){
if(is_lca(x,y))return x;
if(is_lca(y,x))return y;
for(int i = L - 1; i >= 0; i--){
if(!is_lca(up[x][i] , y))x = up[x][i];
}
return up[x][0];
}
void dfs(int node , int par){
sum[node] = 0;
for(int i : adj[node]){
if(i!=par){
dfs(i,node);
sum[node] += dp[i];
}
}
dp[node] = sum[node];
for(int i : pos[node]){
int res = 0;
if(v[i].a != node){
res += ft_sum.sum(tin[v[i].a]) - ft_sum.sum(tin[node]);
res -= ft_dp.sum(tin[v[i].a]) - ft_dp.sum(tin[node]);
}
if(v[i].b != node){
res += ft_sum.sum(tin[v[i].b]) - ft_sum.sum(tin[node]);
res -= ft_dp.sum(tin[v[i].b]) - ft_dp.sum(tin[node]);
}
res += sum[node];
res += v[i].c;
dp[node] = max(dp[node] , res);
}
ft_sum.set(tin[node],sum[node],2*n);
ft_sum.set(tout[node],-sum[node],2*n);
ft_dp.set(tin[node],dp[node],2*n);
ft_dp.set(tout[node],-dp[node],2*n);
}
void solve(){
cin >> n;
for(int i = 0; i < n - 1; i++){
int a , b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
timer = 0;
bl(1,1);
int m;
cin >> m;
for(int i = 0; i < m; i++){
int a , b , c;
cin >> a >> b >> c;
int lca = find(a,b);
v.pb(make(a,b,lca,c));
pos[lca].pb(i);
}
ft_dp.init(2*n);
ft_sum.init(2*n);
dfs(1,1);
cout << dp[1] << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5032 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
95 ms |
21156 KB |
Output is correct |
6 |
Correct |
51 ms |
30308 KB |
Output is correct |
7 |
Correct |
97 ms |
27184 KB |
Output is correct |
8 |
Correct |
79 ms |
21580 KB |
Output is correct |
9 |
Correct |
102 ms |
25284 KB |
Output is correct |
10 |
Correct |
72 ms |
21448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
102 ms |
34448 KB |
Output is correct |
5 |
Correct |
116 ms |
34320 KB |
Output is correct |
6 |
Correct |
92 ms |
34320 KB |
Output is correct |
7 |
Correct |
91 ms |
34324 KB |
Output is correct |
8 |
Correct |
89 ms |
34360 KB |
Output is correct |
9 |
Correct |
101 ms |
34364 KB |
Output is correct |
10 |
Correct |
95 ms |
34328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5332 KB |
Output is correct |
4 |
Correct |
102 ms |
34448 KB |
Output is correct |
5 |
Correct |
116 ms |
34320 KB |
Output is correct |
6 |
Correct |
92 ms |
34320 KB |
Output is correct |
7 |
Correct |
91 ms |
34324 KB |
Output is correct |
8 |
Correct |
89 ms |
34360 KB |
Output is correct |
9 |
Correct |
101 ms |
34364 KB |
Output is correct |
10 |
Correct |
95 ms |
34328 KB |
Output is correct |
11 |
Correct |
10 ms |
6096 KB |
Output is correct |
12 |
Correct |
99 ms |
34640 KB |
Output is correct |
13 |
Correct |
109 ms |
34588 KB |
Output is correct |
14 |
Correct |
103 ms |
34684 KB |
Output is correct |
15 |
Correct |
100 ms |
34652 KB |
Output is correct |
16 |
Correct |
103 ms |
34756 KB |
Output is correct |
17 |
Correct |
107 ms |
34636 KB |
Output is correct |
18 |
Correct |
129 ms |
34636 KB |
Output is correct |
19 |
Correct |
113 ms |
34696 KB |
Output is correct |
20 |
Correct |
106 ms |
34632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
24620 KB |
Output is correct |
2 |
Correct |
107 ms |
34452 KB |
Output is correct |
3 |
Correct |
208 ms |
30852 KB |
Output is correct |
4 |
Correct |
121 ms |
24892 KB |
Output is correct |
5 |
Correct |
143 ms |
30268 KB |
Output is correct |
6 |
Correct |
147 ms |
25024 KB |
Output is correct |
7 |
Correct |
179 ms |
29880 KB |
Output is correct |
8 |
Correct |
148 ms |
25028 KB |
Output is correct |
9 |
Correct |
110 ms |
34384 KB |
Output is correct |
10 |
Correct |
178 ms |
28836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5032 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
95 ms |
21156 KB |
Output is correct |
6 |
Correct |
51 ms |
30308 KB |
Output is correct |
7 |
Correct |
97 ms |
27184 KB |
Output is correct |
8 |
Correct |
79 ms |
21580 KB |
Output is correct |
9 |
Correct |
102 ms |
25284 KB |
Output is correct |
10 |
Correct |
72 ms |
21448 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5332 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5172 KB |
Output is correct |
15 |
Correct |
4 ms |
5188 KB |
Output is correct |
16 |
Correct |
4 ms |
5180 KB |
Output is correct |
17 |
Correct |
4 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5312 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
5 ms |
5332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5032 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
95 ms |
21156 KB |
Output is correct |
6 |
Correct |
51 ms |
30308 KB |
Output is correct |
7 |
Correct |
97 ms |
27184 KB |
Output is correct |
8 |
Correct |
79 ms |
21580 KB |
Output is correct |
9 |
Correct |
102 ms |
25284 KB |
Output is correct |
10 |
Correct |
72 ms |
21448 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
5332 KB |
Output is correct |
14 |
Correct |
102 ms |
34448 KB |
Output is correct |
15 |
Correct |
116 ms |
34320 KB |
Output is correct |
16 |
Correct |
92 ms |
34320 KB |
Output is correct |
17 |
Correct |
91 ms |
34324 KB |
Output is correct |
18 |
Correct |
89 ms |
34360 KB |
Output is correct |
19 |
Correct |
101 ms |
34364 KB |
Output is correct |
20 |
Correct |
95 ms |
34328 KB |
Output is correct |
21 |
Correct |
10 ms |
6096 KB |
Output is correct |
22 |
Correct |
99 ms |
34640 KB |
Output is correct |
23 |
Correct |
109 ms |
34588 KB |
Output is correct |
24 |
Correct |
103 ms |
34684 KB |
Output is correct |
25 |
Correct |
100 ms |
34652 KB |
Output is correct |
26 |
Correct |
103 ms |
34756 KB |
Output is correct |
27 |
Correct |
107 ms |
34636 KB |
Output is correct |
28 |
Correct |
129 ms |
34636 KB |
Output is correct |
29 |
Correct |
113 ms |
34696 KB |
Output is correct |
30 |
Correct |
106 ms |
34632 KB |
Output is correct |
31 |
Correct |
187 ms |
24620 KB |
Output is correct |
32 |
Correct |
107 ms |
34452 KB |
Output is correct |
33 |
Correct |
208 ms |
30852 KB |
Output is correct |
34 |
Correct |
121 ms |
24892 KB |
Output is correct |
35 |
Correct |
143 ms |
30268 KB |
Output is correct |
36 |
Correct |
147 ms |
25024 KB |
Output is correct |
37 |
Correct |
179 ms |
29880 KB |
Output is correct |
38 |
Correct |
148 ms |
25028 KB |
Output is correct |
39 |
Correct |
110 ms |
34384 KB |
Output is correct |
40 |
Correct |
178 ms |
28836 KB |
Output is correct |
41 |
Correct |
4 ms |
5204 KB |
Output is correct |
42 |
Correct |
3 ms |
5332 KB |
Output is correct |
43 |
Correct |
4 ms |
5204 KB |
Output is correct |
44 |
Correct |
4 ms |
5172 KB |
Output is correct |
45 |
Correct |
4 ms |
5188 KB |
Output is correct |
46 |
Correct |
4 ms |
5180 KB |
Output is correct |
47 |
Correct |
4 ms |
5212 KB |
Output is correct |
48 |
Correct |
5 ms |
5312 KB |
Output is correct |
49 |
Correct |
4 ms |
5204 KB |
Output is correct |
50 |
Correct |
5 ms |
5332 KB |
Output is correct |
51 |
Correct |
178 ms |
25212 KB |
Output is correct |
52 |
Correct |
115 ms |
34576 KB |
Output is correct |
53 |
Correct |
200 ms |
29204 KB |
Output is correct |
54 |
Correct |
117 ms |
25216 KB |
Output is correct |
55 |
Correct |
160 ms |
24892 KB |
Output is correct |
56 |
Correct |
103 ms |
34656 KB |
Output is correct |
57 |
Correct |
171 ms |
29968 KB |
Output is correct |
58 |
Correct |
129 ms |
25188 KB |
Output is correct |
59 |
Correct |
167 ms |
25220 KB |
Output is correct |
60 |
Correct |
107 ms |
34636 KB |
Output is correct |
61 |
Correct |
155 ms |
30140 KB |
Output is correct |
62 |
Correct |
122 ms |
25456 KB |
Output is correct |
63 |
Correct |
155 ms |
24956 KB |
Output is correct |
64 |
Correct |
105 ms |
34584 KB |
Output is correct |
65 |
Correct |
174 ms |
30016 KB |
Output is correct |
66 |
Correct |
143 ms |
25184 KB |
Output is correct |
67 |
Correct |
150 ms |
25024 KB |
Output is correct |
68 |
Correct |
93 ms |
34640 KB |
Output is correct |
69 |
Correct |
189 ms |
28600 KB |
Output is correct |
70 |
Correct |
125 ms |
25256 KB |
Output is correct |