//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct node{
int c1,c2,val;
};
const int mxn=1e5+5;
vector<int> adj[mxn];
int up[mxn][20];
int depth[mxn];
vector<node> st[mxn];
int L[mxn],R[mxn];
ll bit[mxn];
ll dp[mxn];
int cnt=0;
void update(int pos,ll val){
for(;pos<mxn;pos+=(pos&-pos)){
bit[pos]+=val;
}
}
ll query(int pos){
ll ans=0;
for(;pos>0;pos-=(pos&-pos)){
ans+=bit[pos];
}
return ans;
}
void dfs(int v,int p=0){
up[v][0]=p;
depth[v]=depth[p]+1;
L[v]=++cnt;
for(auto u:adj[v]){
if(u==p) continue;
dfs(u,v);
}
R[v]=cnt;
}
int go(int x,int len){
for(int i=0;i<20;i++){
if(len&(1<<i)){
x=up[x][i];
}
}
return x;
}
int lca(int a,int b){
if(depth[a]<depth[b]) swap(a,b);
int len=depth[a]-depth[b];
a=go(a,len);
if(a==b) return a;
for(int i=19;i>=0;i--){
int ta=up[a][i];
int tb=up[b][i];
if(ta!=tb){
a=ta;
b=tb;
}
}
return up[a][0];
}
void dfs2(int v,int p=0){
ll sum=0;
for(auto u:adj[v]){
if(u==p) continue;
dfs2(u,v);
}
for(auto u:adj[v]){
if(u==p) continue;
update(L[v],dp[u]);
update(R[v]+1,-dp[u]);
update(L[u],-dp[u]);
update(R[u]+1,dp[u]);
sum+=dp[u];
}
dp[v]=sum;
for(auto u:st[v]){
dp[v]=max(dp[v],query(L[u.c1])+query(L[u.c2])-(u.c2!=0?sum:0)+u.val);
}
//cout<<v<<' '<<dp[v]<<'\n';
}
int main() {_
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++){
up[i][j]=up[up[i][j-1]][j-1];
}
}
int q;
cin>>q;
for(int i=0;i<q;i++){
int a,b,c;
cin>>a>>b>>c;
int node=lca(a,b);
//cout<<node<<' '<<a<<' '<<b<<'\n';
if(depth[a]>depth[b]) swap(a,b);
int len=depth[b]-depth[a];
if(a==node){
st[node].push_back({b,0,c});
}
else{
st[node].push_back({a,b,c});
}
}
dfs2(1);
cout<<dp[1]<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:127:13: warning: unused variable 'len' [-Wunused-variable]
127 | int len=depth[b]-depth[a];
| ^~~
election_campaign.cpp: In function 'void setIO(std::string)':
election_campaign.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5016 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
56 ms |
20052 KB |
Output is correct |
6 |
Correct |
37 ms |
27740 KB |
Output is correct |
7 |
Correct |
52 ms |
24932 KB |
Output is correct |
8 |
Correct |
43 ms |
20700 KB |
Output is correct |
9 |
Correct |
51 ms |
23336 KB |
Output is correct |
10 |
Correct |
40 ms |
20316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
3 ms |
5436 KB |
Output is correct |
4 |
Correct |
85 ms |
31132 KB |
Output is correct |
5 |
Correct |
74 ms |
31092 KB |
Output is correct |
6 |
Correct |
76 ms |
31136 KB |
Output is correct |
7 |
Correct |
76 ms |
31060 KB |
Output is correct |
8 |
Correct |
75 ms |
31060 KB |
Output is correct |
9 |
Correct |
72 ms |
30960 KB |
Output is correct |
10 |
Correct |
74 ms |
30956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
3 ms |
5436 KB |
Output is correct |
4 |
Correct |
85 ms |
31132 KB |
Output is correct |
5 |
Correct |
74 ms |
31092 KB |
Output is correct |
6 |
Correct |
76 ms |
31136 KB |
Output is correct |
7 |
Correct |
76 ms |
31060 KB |
Output is correct |
8 |
Correct |
75 ms |
31060 KB |
Output is correct |
9 |
Correct |
72 ms |
30960 KB |
Output is correct |
10 |
Correct |
74 ms |
30956 KB |
Output is correct |
11 |
Correct |
9 ms |
5980 KB |
Output is correct |
12 |
Correct |
81 ms |
31280 KB |
Output is correct |
13 |
Correct |
77 ms |
31220 KB |
Output is correct |
14 |
Correct |
73 ms |
31316 KB |
Output is correct |
15 |
Correct |
94 ms |
31356 KB |
Output is correct |
16 |
Correct |
72 ms |
31380 KB |
Output is correct |
17 |
Correct |
77 ms |
31376 KB |
Output is correct |
18 |
Correct |
84 ms |
31360 KB |
Output is correct |
19 |
Correct |
81 ms |
31400 KB |
Output is correct |
20 |
Correct |
79 ms |
31316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
22788 KB |
Output is correct |
2 |
Correct |
90 ms |
31060 KB |
Output is correct |
3 |
Correct |
104 ms |
27916 KB |
Output is correct |
4 |
Correct |
70 ms |
23324 KB |
Output is correct |
5 |
Correct |
95 ms |
27476 KB |
Output is correct |
6 |
Correct |
72 ms |
23216 KB |
Output is correct |
7 |
Correct |
121 ms |
27172 KB |
Output is correct |
8 |
Correct |
93 ms |
23200 KB |
Output is correct |
9 |
Correct |
69 ms |
31028 KB |
Output is correct |
10 |
Correct |
106 ms |
26120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5016 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
56 ms |
20052 KB |
Output is correct |
6 |
Correct |
37 ms |
27740 KB |
Output is correct |
7 |
Correct |
52 ms |
24932 KB |
Output is correct |
8 |
Correct |
43 ms |
20700 KB |
Output is correct |
9 |
Correct |
51 ms |
23336 KB |
Output is correct |
10 |
Correct |
40 ms |
20316 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5464 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5176 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
3 ms |
5212 KB |
Output is correct |
19 |
Correct |
3 ms |
5464 KB |
Output is correct |
20 |
Correct |
3 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5016 KB |
Output is correct |
3 |
Correct |
2 ms |
5208 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
56 ms |
20052 KB |
Output is correct |
6 |
Correct |
37 ms |
27740 KB |
Output is correct |
7 |
Correct |
52 ms |
24932 KB |
Output is correct |
8 |
Correct |
43 ms |
20700 KB |
Output is correct |
9 |
Correct |
51 ms |
23336 KB |
Output is correct |
10 |
Correct |
40 ms |
20316 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
3 ms |
5436 KB |
Output is correct |
14 |
Correct |
85 ms |
31132 KB |
Output is correct |
15 |
Correct |
74 ms |
31092 KB |
Output is correct |
16 |
Correct |
76 ms |
31136 KB |
Output is correct |
17 |
Correct |
76 ms |
31060 KB |
Output is correct |
18 |
Correct |
75 ms |
31060 KB |
Output is correct |
19 |
Correct |
72 ms |
30960 KB |
Output is correct |
20 |
Correct |
74 ms |
30956 KB |
Output is correct |
21 |
Correct |
9 ms |
5980 KB |
Output is correct |
22 |
Correct |
81 ms |
31280 KB |
Output is correct |
23 |
Correct |
77 ms |
31220 KB |
Output is correct |
24 |
Correct |
73 ms |
31316 KB |
Output is correct |
25 |
Correct |
94 ms |
31356 KB |
Output is correct |
26 |
Correct |
72 ms |
31380 KB |
Output is correct |
27 |
Correct |
77 ms |
31376 KB |
Output is correct |
28 |
Correct |
84 ms |
31360 KB |
Output is correct |
29 |
Correct |
81 ms |
31400 KB |
Output is correct |
30 |
Correct |
79 ms |
31316 KB |
Output is correct |
31 |
Correct |
89 ms |
22788 KB |
Output is correct |
32 |
Correct |
90 ms |
31060 KB |
Output is correct |
33 |
Correct |
104 ms |
27916 KB |
Output is correct |
34 |
Correct |
70 ms |
23324 KB |
Output is correct |
35 |
Correct |
95 ms |
27476 KB |
Output is correct |
36 |
Correct |
72 ms |
23216 KB |
Output is correct |
37 |
Correct |
121 ms |
27172 KB |
Output is correct |
38 |
Correct |
93 ms |
23200 KB |
Output is correct |
39 |
Correct |
69 ms |
31028 KB |
Output is correct |
40 |
Correct |
106 ms |
26120 KB |
Output is correct |
41 |
Correct |
3 ms |
5208 KB |
Output is correct |
42 |
Correct |
3 ms |
5464 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5176 KB |
Output is correct |
46 |
Correct |
3 ms |
5212 KB |
Output is correct |
47 |
Correct |
3 ms |
5212 KB |
Output is correct |
48 |
Correct |
3 ms |
5212 KB |
Output is correct |
49 |
Correct |
3 ms |
5464 KB |
Output is correct |
50 |
Correct |
3 ms |
5208 KB |
Output is correct |
51 |
Correct |
86 ms |
23432 KB |
Output is correct |
52 |
Correct |
77 ms |
31308 KB |
Output is correct |
53 |
Correct |
120 ms |
26664 KB |
Output is correct |
54 |
Correct |
64 ms |
23588 KB |
Output is correct |
55 |
Correct |
84 ms |
23052 KB |
Output is correct |
56 |
Correct |
77 ms |
31312 KB |
Output is correct |
57 |
Correct |
93 ms |
27160 KB |
Output is correct |
58 |
Correct |
70 ms |
23464 KB |
Output is correct |
59 |
Correct |
87 ms |
23436 KB |
Output is correct |
60 |
Correct |
77 ms |
31372 KB |
Output is correct |
61 |
Correct |
98 ms |
27344 KB |
Output is correct |
62 |
Correct |
78 ms |
23252 KB |
Output is correct |
63 |
Correct |
111 ms |
23056 KB |
Output is correct |
64 |
Correct |
77 ms |
31312 KB |
Output is correct |
65 |
Correct |
110 ms |
27148 KB |
Output is correct |
66 |
Correct |
69 ms |
23444 KB |
Output is correct |
67 |
Correct |
81 ms |
23024 KB |
Output is correct |
68 |
Correct |
80 ms |
31328 KB |
Output is correct |
69 |
Correct |
98 ms |
26088 KB |
Output is correct |
70 |
Correct |
83 ms |
23732 KB |
Output is correct |