#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
const int mx=100010;
vector<int>adj[mx];
int spa[mx][17];
int dep[mx];
inline int kpa(int x,int k)
{
for(;k>0;k^=k&-k)
x=spa[x][__builtin_ctz(k)];
return x;
}
inline int lca(int x,int y)
{
if(dep[x]>dep[y])
x=kpa(x,dep[x]-dep[y]);
else
y=kpa(y,dep[y]-dep[x]);
if(x==y)
return x;
for(int i=17;i-->0;)
if(spa[x][i]!=spa[y][i])
x=spa[x][i],y=spa[y][i];
return spa[x][0];
}
void sdfs(int x,int p)
{
dep[x]=dep[p]+1;
spa[x][0]=p;
for(int i=0;i<16;i++)
spa[x][i+1]=spa[spa[x][i]][i];
for(int&t:adj[x])
if(t!=p)
sdfs(t,x);
return;
}
ll spd[mx][17];
inline ll pq(int x,int y)
{
ll ret=0;
for(int k=dep[x]-dep[y];k>0;k^=k&-k)
ret+=spd[x][__builtin_ctz(k)],x=spa[x][__builtin_ctz(k)];
return ret;
}
ll dp[mx],dp2[mx];
vector<pi>upd[mx];
vector<pair<pi,int> >quer[mx];
void dfs(int x,int p)
{
dp[x]=0;
for(int&t:adj[x])
if(t!=p)
dfs(t,x),dp[x]+=dp2[t];
for(pi&t:upd[x])
spd[t.fi][t.se+1]=spd[t.fi][t.se]+spd[spa[t.fi][t.se]][t.se];
dp2[x]=dp[x];
for(auto&t:quer[x])
dp2[x]=max(dp2[x],dp[x]-pq(t.fi.fi,x)-pq(t.fi.se,x)+t.se);
spd[x][0]=dp2[x]-dp[x];
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin>>n;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
adj[u].eb(v);
adj[v].eb(u);
}
sdfs(1,0);
cin>>m;
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
quer[lca(u,v)].eb(pi(u,v),w);
}
for(int i=0;i<16;i++)
for(int x=0;x++<n;)
if(spa[x][i+1]!=0)
upd[spa[x][i+1]].eb(x,i);
dfs(1,0);
cout<<dp2[1]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7680 KB |
Output is correct |
5 |
Correct |
153 ms |
36988 KB |
Output is correct |
6 |
Correct |
139 ms |
52740 KB |
Output is correct |
7 |
Correct |
351 ms |
55820 KB |
Output is correct |
8 |
Correct |
112 ms |
34704 KB |
Output is correct |
9 |
Correct |
369 ms |
54392 KB |
Output is correct |
10 |
Correct |
99 ms |
34472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7936 KB |
Output is correct |
4 |
Correct |
245 ms |
54836 KB |
Output is correct |
5 |
Correct |
227 ms |
54776 KB |
Output is correct |
6 |
Correct |
206 ms |
54776 KB |
Output is correct |
7 |
Correct |
224 ms |
54908 KB |
Output is correct |
8 |
Correct |
220 ms |
54876 KB |
Output is correct |
9 |
Correct |
202 ms |
54776 KB |
Output is correct |
10 |
Correct |
231 ms |
54848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7936 KB |
Output is correct |
4 |
Correct |
245 ms |
54836 KB |
Output is correct |
5 |
Correct |
227 ms |
54776 KB |
Output is correct |
6 |
Correct |
206 ms |
54776 KB |
Output is correct |
7 |
Correct |
224 ms |
54908 KB |
Output is correct |
8 |
Correct |
220 ms |
54876 KB |
Output is correct |
9 |
Correct |
202 ms |
54776 KB |
Output is correct |
10 |
Correct |
231 ms |
54848 KB |
Output is correct |
11 |
Correct |
15 ms |
8320 KB |
Output is correct |
12 |
Correct |
226 ms |
54904 KB |
Output is correct |
13 |
Correct |
221 ms |
54904 KB |
Output is correct |
14 |
Correct |
206 ms |
54776 KB |
Output is correct |
15 |
Correct |
223 ms |
54776 KB |
Output is correct |
16 |
Correct |
205 ms |
54776 KB |
Output is correct |
17 |
Correct |
229 ms |
54776 KB |
Output is correct |
18 |
Correct |
215 ms |
54760 KB |
Output is correct |
19 |
Correct |
210 ms |
54780 KB |
Output is correct |
20 |
Correct |
248 ms |
54880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
216 ms |
38832 KB |
Output is correct |
2 |
Correct |
211 ms |
54776 KB |
Output is correct |
3 |
Correct |
510 ms |
57336 KB |
Output is correct |
4 |
Correct |
131 ms |
36656 KB |
Output is correct |
5 |
Correct |
450 ms |
56956 KB |
Output is correct |
6 |
Correct |
135 ms |
36752 KB |
Output is correct |
7 |
Correct |
495 ms |
56756 KB |
Output is correct |
8 |
Correct |
221 ms |
39392 KB |
Output is correct |
9 |
Correct |
202 ms |
54776 KB |
Output is correct |
10 |
Correct |
494 ms |
55852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7680 KB |
Output is correct |
5 |
Correct |
153 ms |
36988 KB |
Output is correct |
6 |
Correct |
139 ms |
52740 KB |
Output is correct |
7 |
Correct |
351 ms |
55820 KB |
Output is correct |
8 |
Correct |
112 ms |
34704 KB |
Output is correct |
9 |
Correct |
369 ms |
54392 KB |
Output is correct |
10 |
Correct |
99 ms |
34472 KB |
Output is correct |
11 |
Correct |
6 ms |
7680 KB |
Output is correct |
12 |
Correct |
7 ms |
7936 KB |
Output is correct |
13 |
Correct |
7 ms |
7808 KB |
Output is correct |
14 |
Correct |
6 ms |
7680 KB |
Output is correct |
15 |
Correct |
6 ms |
7680 KB |
Output is correct |
16 |
Correct |
7 ms |
7808 KB |
Output is correct |
17 |
Correct |
6 ms |
7680 KB |
Output is correct |
18 |
Correct |
7 ms |
7808 KB |
Output is correct |
19 |
Correct |
6 ms |
7680 KB |
Output is correct |
20 |
Correct |
6 ms |
7936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7680 KB |
Output is correct |
5 |
Correct |
153 ms |
36988 KB |
Output is correct |
6 |
Correct |
139 ms |
52740 KB |
Output is correct |
7 |
Correct |
351 ms |
55820 KB |
Output is correct |
8 |
Correct |
112 ms |
34704 KB |
Output is correct |
9 |
Correct |
369 ms |
54392 KB |
Output is correct |
10 |
Correct |
99 ms |
34472 KB |
Output is correct |
11 |
Correct |
6 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Correct |
6 ms |
7936 KB |
Output is correct |
14 |
Correct |
245 ms |
54836 KB |
Output is correct |
15 |
Correct |
227 ms |
54776 KB |
Output is correct |
16 |
Correct |
206 ms |
54776 KB |
Output is correct |
17 |
Correct |
224 ms |
54908 KB |
Output is correct |
18 |
Correct |
220 ms |
54876 KB |
Output is correct |
19 |
Correct |
202 ms |
54776 KB |
Output is correct |
20 |
Correct |
231 ms |
54848 KB |
Output is correct |
21 |
Correct |
15 ms |
8320 KB |
Output is correct |
22 |
Correct |
226 ms |
54904 KB |
Output is correct |
23 |
Correct |
221 ms |
54904 KB |
Output is correct |
24 |
Correct |
206 ms |
54776 KB |
Output is correct |
25 |
Correct |
223 ms |
54776 KB |
Output is correct |
26 |
Correct |
205 ms |
54776 KB |
Output is correct |
27 |
Correct |
229 ms |
54776 KB |
Output is correct |
28 |
Correct |
215 ms |
54760 KB |
Output is correct |
29 |
Correct |
210 ms |
54780 KB |
Output is correct |
30 |
Correct |
248 ms |
54880 KB |
Output is correct |
31 |
Correct |
216 ms |
38832 KB |
Output is correct |
32 |
Correct |
211 ms |
54776 KB |
Output is correct |
33 |
Correct |
510 ms |
57336 KB |
Output is correct |
34 |
Correct |
131 ms |
36656 KB |
Output is correct |
35 |
Correct |
450 ms |
56956 KB |
Output is correct |
36 |
Correct |
135 ms |
36752 KB |
Output is correct |
37 |
Correct |
495 ms |
56756 KB |
Output is correct |
38 |
Correct |
221 ms |
39392 KB |
Output is correct |
39 |
Correct |
202 ms |
54776 KB |
Output is correct |
40 |
Correct |
494 ms |
55852 KB |
Output is correct |
41 |
Correct |
6 ms |
7680 KB |
Output is correct |
42 |
Correct |
7 ms |
7936 KB |
Output is correct |
43 |
Correct |
7 ms |
7808 KB |
Output is correct |
44 |
Correct |
6 ms |
7680 KB |
Output is correct |
45 |
Correct |
6 ms |
7680 KB |
Output is correct |
46 |
Correct |
7 ms |
7808 KB |
Output is correct |
47 |
Correct |
6 ms |
7680 KB |
Output is correct |
48 |
Correct |
7 ms |
7808 KB |
Output is correct |
49 |
Correct |
6 ms |
7680 KB |
Output is correct |
50 |
Correct |
6 ms |
7936 KB |
Output is correct |
51 |
Correct |
237 ms |
39160 KB |
Output is correct |
52 |
Correct |
225 ms |
54776 KB |
Output is correct |
53 |
Correct |
536 ms |
55884 KB |
Output is correct |
54 |
Correct |
137 ms |
36016 KB |
Output is correct |
55 |
Correct |
210 ms |
38832 KB |
Output is correct |
56 |
Correct |
219 ms |
54776 KB |
Output is correct |
57 |
Correct |
455 ms |
56384 KB |
Output is correct |
58 |
Correct |
132 ms |
36908 KB |
Output is correct |
59 |
Correct |
222 ms |
39000 KB |
Output is correct |
60 |
Correct |
235 ms |
54820 KB |
Output is correct |
61 |
Correct |
453 ms |
56568 KB |
Output is correct |
62 |
Correct |
133 ms |
36560 KB |
Output is correct |
63 |
Correct |
215 ms |
38888 KB |
Output is correct |
64 |
Correct |
234 ms |
54904 KB |
Output is correct |
65 |
Correct |
504 ms |
56324 KB |
Output is correct |
66 |
Correct |
143 ms |
36480 KB |
Output is correct |
67 |
Correct |
217 ms |
38888 KB |
Output is correct |
68 |
Correct |
234 ms |
54772 KB |
Output is correct |
69 |
Correct |
453 ms |
55416 KB |
Output is correct |
70 |
Correct |
141 ms |
36452 KB |
Output is correct |