#define X first
#define Y second
#define pb push_back
#include<bits/stdc++.h>
using namespace std;
typedef pair < int, int > ii;
typedef pair < ii , int > iii;
using ll = long long;
using ld = long double;
const ll MAX=100003,inf=1e18;
vector < int > pr[MAX];
int par[18][MAX];
int F[18][MAX];
int h[MAX];
int dp[MAX];
int sum[MAX];
void DFS(int v,int trc){
for(int u:pr[v]) if(u!=trc){
par[0][u]=v;
h[u]=h[v]+1;
DFS(u,v);
}
}
int jump(int v,int d){
for(int i= 17;i>=0;i--)if(d&(1<<i))
v=par[i][v];
return v;
}
int cnt(int i,int v){
if(F[i][v]!=-1) return F[i][v];
else return F[i][v]=cnt(i-1,v)+cnt(i-1,par[i-1][v]);
}
int getup(int v,int d){
int ans=0;
for(int i=17;i>=0;i--)if(d&(1<<i)){
ans+=cnt(i,v);
v=par[i][v];
}
return ans;
}
int lca(int u,int v){
v=jump(v,h[v]-h[u]);
if(u==v) return u;
for(int i=17;i>=0;i--)if(par[i][u]!=par[i][v]) {
u=par[i][u];v=par[i][v];
}
return par[0][u];
}
struct path{
int x,y,c;
path(){}
path(int u1,int u2,int u3){
x=u1;y=u2;c=u3;
}
};
vector < path > ad[MAX];
void GO(int v,int trc){
for(int u:pr[v]) if (u!=trc){
GO(u,v);
sum[v]+=dp[u];
}
for(int u:pr[v]) if (u!=trc){
F[0][u]=sum[v]-dp[u];
}
dp[v]=sum[v];
for(path e:ad[v]){
if(e.x!=v){
dp[v]=max(dp[v],e.c+getup(e.x,h[e.x]-h[v])+getup(e.y,h[e.y]-h[v])+sum[e.x]+sum[e.y]-sum[v]);
}
else dp[v]=max(dp[v],e.c+getup(e.y,h[e.y]-h[v])+sum[e.y]);
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("election.inp","r",stdin);freopen("election.out","w",stdout);
int n,Q,x,y,c;
cin>>n;
memset(F,-1,sizeof F);
for(int i=1;i<n;i++){
cin>>x>>y;
pr[x].pb(y);
pr[y].pb(x);
}
DFS(1,0);
for(int i=1;i<=17;i++)
for(int u=1;u<=n;u++)
par[i][u]=par[i-1][par[i-1][u]];
cin>>Q;
while(Q--){
cin>>x>>y>>c;
if(h[x]>h[y]) swap(x,y);
//cout<<x<<' '<<y<<' '<<lca(x,y)<<'\n';
ad[lca(x,y)].pb(path(x,y,c));
}
GO(1,0);
cout<<dp[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12288 KB |
Output is correct |
5 |
Correct |
91 ms |
24696 KB |
Output is correct |
6 |
Correct |
54 ms |
32376 KB |
Output is correct |
7 |
Correct |
84 ms |
29688 KB |
Output is correct |
8 |
Correct |
62 ms |
24696 KB |
Output is correct |
9 |
Correct |
85 ms |
28172 KB |
Output is correct |
10 |
Correct |
66 ms |
24824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12416 KB |
Output is correct |
4 |
Correct |
135 ms |
35704 KB |
Output is correct |
5 |
Correct |
137 ms |
35772 KB |
Output is correct |
6 |
Correct |
117 ms |
35704 KB |
Output is correct |
7 |
Correct |
135 ms |
35832 KB |
Output is correct |
8 |
Correct |
137 ms |
35832 KB |
Output is correct |
9 |
Correct |
124 ms |
35704 KB |
Output is correct |
10 |
Correct |
138 ms |
35736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12416 KB |
Output is correct |
4 |
Correct |
135 ms |
35704 KB |
Output is correct |
5 |
Correct |
137 ms |
35772 KB |
Output is correct |
6 |
Correct |
117 ms |
35704 KB |
Output is correct |
7 |
Correct |
135 ms |
35832 KB |
Output is correct |
8 |
Correct |
137 ms |
35832 KB |
Output is correct |
9 |
Correct |
124 ms |
35704 KB |
Output is correct |
10 |
Correct |
138 ms |
35736 KB |
Output is correct |
11 |
Correct |
18 ms |
13184 KB |
Output is correct |
12 |
Correct |
142 ms |
36040 KB |
Output is correct |
13 |
Correct |
135 ms |
35960 KB |
Output is correct |
14 |
Correct |
132 ms |
35960 KB |
Output is correct |
15 |
Correct |
148 ms |
35960 KB |
Output is correct |
16 |
Correct |
130 ms |
36004 KB |
Output is correct |
17 |
Correct |
146 ms |
35960 KB |
Output is correct |
18 |
Correct |
135 ms |
35960 KB |
Output is correct |
19 |
Correct |
123 ms |
36088 KB |
Output is correct |
20 |
Correct |
134 ms |
35964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
27568 KB |
Output is correct |
2 |
Correct |
122 ms |
35832 KB |
Output is correct |
3 |
Correct |
338 ms |
32632 KB |
Output is correct |
4 |
Correct |
115 ms |
27568 KB |
Output is correct |
5 |
Correct |
273 ms |
32120 KB |
Output is correct |
6 |
Correct |
116 ms |
27632 KB |
Output is correct |
7 |
Correct |
333 ms |
31924 KB |
Output is correct |
8 |
Correct |
178 ms |
28024 KB |
Output is correct |
9 |
Correct |
119 ms |
35728 KB |
Output is correct |
10 |
Correct |
339 ms |
30896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12288 KB |
Output is correct |
5 |
Correct |
91 ms |
24696 KB |
Output is correct |
6 |
Correct |
54 ms |
32376 KB |
Output is correct |
7 |
Correct |
84 ms |
29688 KB |
Output is correct |
8 |
Correct |
62 ms |
24696 KB |
Output is correct |
9 |
Correct |
85 ms |
28172 KB |
Output is correct |
10 |
Correct |
66 ms |
24824 KB |
Output is correct |
11 |
Correct |
11 ms |
12288 KB |
Output is correct |
12 |
Correct |
9 ms |
12416 KB |
Output is correct |
13 |
Correct |
9 ms |
12416 KB |
Output is correct |
14 |
Correct |
9 ms |
12288 KB |
Output is correct |
15 |
Correct |
9 ms |
12288 KB |
Output is correct |
16 |
Correct |
9 ms |
12288 KB |
Output is correct |
17 |
Correct |
10 ms |
12448 KB |
Output is correct |
18 |
Correct |
9 ms |
12416 KB |
Output is correct |
19 |
Correct |
9 ms |
12288 KB |
Output is correct |
20 |
Correct |
9 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12288 KB |
Output is correct |
5 |
Correct |
91 ms |
24696 KB |
Output is correct |
6 |
Correct |
54 ms |
32376 KB |
Output is correct |
7 |
Correct |
84 ms |
29688 KB |
Output is correct |
8 |
Correct |
62 ms |
24696 KB |
Output is correct |
9 |
Correct |
85 ms |
28172 KB |
Output is correct |
10 |
Correct |
66 ms |
24824 KB |
Output is correct |
11 |
Correct |
9 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12416 KB |
Output is correct |
14 |
Correct |
135 ms |
35704 KB |
Output is correct |
15 |
Correct |
137 ms |
35772 KB |
Output is correct |
16 |
Correct |
117 ms |
35704 KB |
Output is correct |
17 |
Correct |
135 ms |
35832 KB |
Output is correct |
18 |
Correct |
137 ms |
35832 KB |
Output is correct |
19 |
Correct |
124 ms |
35704 KB |
Output is correct |
20 |
Correct |
138 ms |
35736 KB |
Output is correct |
21 |
Correct |
18 ms |
13184 KB |
Output is correct |
22 |
Correct |
142 ms |
36040 KB |
Output is correct |
23 |
Correct |
135 ms |
35960 KB |
Output is correct |
24 |
Correct |
132 ms |
35960 KB |
Output is correct |
25 |
Correct |
148 ms |
35960 KB |
Output is correct |
26 |
Correct |
130 ms |
36004 KB |
Output is correct |
27 |
Correct |
146 ms |
35960 KB |
Output is correct |
28 |
Correct |
135 ms |
35960 KB |
Output is correct |
29 |
Correct |
123 ms |
36088 KB |
Output is correct |
30 |
Correct |
134 ms |
35964 KB |
Output is correct |
31 |
Correct |
186 ms |
27568 KB |
Output is correct |
32 |
Correct |
122 ms |
35832 KB |
Output is correct |
33 |
Correct |
338 ms |
32632 KB |
Output is correct |
34 |
Correct |
115 ms |
27568 KB |
Output is correct |
35 |
Correct |
273 ms |
32120 KB |
Output is correct |
36 |
Correct |
116 ms |
27632 KB |
Output is correct |
37 |
Correct |
333 ms |
31924 KB |
Output is correct |
38 |
Correct |
178 ms |
28024 KB |
Output is correct |
39 |
Correct |
119 ms |
35728 KB |
Output is correct |
40 |
Correct |
339 ms |
30896 KB |
Output is correct |
41 |
Correct |
11 ms |
12288 KB |
Output is correct |
42 |
Correct |
9 ms |
12416 KB |
Output is correct |
43 |
Correct |
9 ms |
12416 KB |
Output is correct |
44 |
Correct |
9 ms |
12288 KB |
Output is correct |
45 |
Correct |
9 ms |
12288 KB |
Output is correct |
46 |
Correct |
9 ms |
12288 KB |
Output is correct |
47 |
Correct |
10 ms |
12448 KB |
Output is correct |
48 |
Correct |
9 ms |
12416 KB |
Output is correct |
49 |
Correct |
9 ms |
12288 KB |
Output is correct |
50 |
Correct |
9 ms |
12416 KB |
Output is correct |
51 |
Correct |
181 ms |
28280 KB |
Output is correct |
52 |
Correct |
143 ms |
35960 KB |
Output is correct |
53 |
Correct |
354 ms |
31336 KB |
Output is correct |
54 |
Correct |
113 ms |
27828 KB |
Output is correct |
55 |
Correct |
190 ms |
27984 KB |
Output is correct |
56 |
Correct |
140 ms |
36020 KB |
Output is correct |
57 |
Correct |
269 ms |
32120 KB |
Output is correct |
58 |
Correct |
122 ms |
27856 KB |
Output is correct |
59 |
Correct |
175 ms |
28024 KB |
Output is correct |
60 |
Correct |
136 ms |
35996 KB |
Output is correct |
61 |
Correct |
273 ms |
32176 KB |
Output is correct |
62 |
Correct |
117 ms |
27600 KB |
Output is correct |
63 |
Correct |
187 ms |
27768 KB |
Output is correct |
64 |
Correct |
142 ms |
36088 KB |
Output is correct |
65 |
Correct |
343 ms |
31920 KB |
Output is correct |
66 |
Correct |
115 ms |
27960 KB |
Output is correct |
67 |
Correct |
190 ms |
27884 KB |
Output is correct |
68 |
Correct |
138 ms |
35960 KB |
Output is correct |
69 |
Correct |
261 ms |
30840 KB |
Output is correct |
70 |
Correct |
123 ms |
27996 KB |
Output is correct |