#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define lun(x) (int)x.size()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<60);
const int inf=(1<<30);
const int nmax=1e5+50;
const ll mod=1e9+7;
using namespace std;
int x,y,z,c[nmax],ex[nmax],ey[nmax],i,n,up[nmax][20],tin[nmax],tout[nmax],t,f[nmax],dp[nmax],fw[nmax],m;
vector<int>a[nmax],vc[nmax];
void dfs(int x,int p)
{
tin[x]=++t;
up[x][0]=p;
for(int i=1;i<17;i++)up[x][i]=up[up[x][i-1]][i-1];
for(int i=0;i<lun(a[x]);i++)if(a[x][i]!=p)dfs(a[x][i],x);
tout[x]=t;
}
int anc(int x,int y)
{
return tin[x]<=tin[y] && tout[x]>=tout[y];
}
int lca(int x,int y)
{
if(anc(x,y))return x;
if(anc(y,x))return y;
for(int i=16;i>=0;i--)if(!anc(up[x][i],y))x=up[x][i];
return up[x][0];
}
void ufw(int i,int v)
{
for(;i<=n;i+=i&(-i))fw[i]+=v;
}
int qfw(int i)
{
ll ans=0;
for(;i>=1;i-=i&(-i))ans+=fw[i];
return ans;
}
void asd(int x,int p)
{
for(int i=0;i<lun(a[x]);i++)
{
int y=a[x][i];
if(y==p)continue;
asd(y,x);
dp[x]+=f[y];
}
f[x]=dp[x];
ufw(tin[x],dp[x]);
ufw(tout[x]+1,-dp[x]);
for(int i=0;i<lun(vc[x]);i++)
{
int u=ex[vc[x][i]],v=ey[vc[x][i]],z=c[vc[x][i]];
f[x]=max(f[x],qfw(tin[u])+qfw(tin[v])-dp[x]+z);
}
ufw(tin[x],-f[x]);
ufw(tout[x]+1,f[x]);
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n;
for(i=1;i<n;i++)
{
cin>>x>>y;
a[x].pb(y);
a[y].pb(x);
}
dfs(1,1);
cin>>m;
for(i=1;i<=m;i++)
{
cin>>x>>y>>z;
vc[lca(x,y)].pb(i);
ex[i]=x,ey[i]=y,c[i]=z;
}
asd(1,1);
cout<<f[1]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5248 KB |
Output is correct |
5 |
Correct |
102 ms |
19448 KB |
Output is correct |
6 |
Correct |
63 ms |
27000 KB |
Output is correct |
7 |
Correct |
101 ms |
24440 KB |
Output is correct |
8 |
Correct |
86 ms |
19320 KB |
Output is correct |
9 |
Correct |
103 ms |
22648 KB |
Output is correct |
10 |
Correct |
76 ms |
19324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
107 ms |
30584 KB |
Output is correct |
5 |
Correct |
123 ms |
30580 KB |
Output is correct |
6 |
Correct |
103 ms |
30584 KB |
Output is correct |
7 |
Correct |
103 ms |
30560 KB |
Output is correct |
8 |
Correct |
106 ms |
30456 KB |
Output is correct |
9 |
Correct |
109 ms |
30584 KB |
Output is correct |
10 |
Correct |
114 ms |
30504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
107 ms |
30584 KB |
Output is correct |
5 |
Correct |
123 ms |
30580 KB |
Output is correct |
6 |
Correct |
103 ms |
30584 KB |
Output is correct |
7 |
Correct |
103 ms |
30560 KB |
Output is correct |
8 |
Correct |
106 ms |
30456 KB |
Output is correct |
9 |
Correct |
109 ms |
30584 KB |
Output is correct |
10 |
Correct |
114 ms |
30504 KB |
Output is correct |
11 |
Correct |
15 ms |
6016 KB |
Output is correct |
12 |
Correct |
108 ms |
30844 KB |
Output is correct |
13 |
Correct |
127 ms |
30840 KB |
Output is correct |
14 |
Correct |
111 ms |
30884 KB |
Output is correct |
15 |
Correct |
111 ms |
30860 KB |
Output is correct |
16 |
Correct |
108 ms |
30840 KB |
Output is correct |
17 |
Correct |
134 ms |
30840 KB |
Output is correct |
18 |
Correct |
132 ms |
30840 KB |
Output is correct |
19 |
Correct |
114 ms |
30844 KB |
Output is correct |
20 |
Correct |
115 ms |
30968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
22260 KB |
Output is correct |
2 |
Correct |
106 ms |
30584 KB |
Output is correct |
3 |
Correct |
209 ms |
27384 KB |
Output is correct |
4 |
Correct |
126 ms |
22260 KB |
Output is correct |
5 |
Correct |
163 ms |
27128 KB |
Output is correct |
6 |
Correct |
113 ms |
22392 KB |
Output is correct |
7 |
Correct |
214 ms |
26792 KB |
Output is correct |
8 |
Correct |
170 ms |
22776 KB |
Output is correct |
9 |
Correct |
105 ms |
30584 KB |
Output is correct |
10 |
Correct |
198 ms |
25972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5248 KB |
Output is correct |
5 |
Correct |
102 ms |
19448 KB |
Output is correct |
6 |
Correct |
63 ms |
27000 KB |
Output is correct |
7 |
Correct |
101 ms |
24440 KB |
Output is correct |
8 |
Correct |
86 ms |
19320 KB |
Output is correct |
9 |
Correct |
103 ms |
22648 KB |
Output is correct |
10 |
Correct |
76 ms |
19324 KB |
Output is correct |
11 |
Correct |
9 ms |
5248 KB |
Output is correct |
12 |
Correct |
9 ms |
5376 KB |
Output is correct |
13 |
Correct |
9 ms |
5248 KB |
Output is correct |
14 |
Correct |
10 ms |
5248 KB |
Output is correct |
15 |
Correct |
9 ms |
5248 KB |
Output is correct |
16 |
Correct |
10 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
9 ms |
5248 KB |
Output is correct |
20 |
Correct |
9 ms |
5376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5248 KB |
Output is correct |
5 |
Correct |
102 ms |
19448 KB |
Output is correct |
6 |
Correct |
63 ms |
27000 KB |
Output is correct |
7 |
Correct |
101 ms |
24440 KB |
Output is correct |
8 |
Correct |
86 ms |
19320 KB |
Output is correct |
9 |
Correct |
103 ms |
22648 KB |
Output is correct |
10 |
Correct |
76 ms |
19324 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5248 KB |
Output is correct |
14 |
Correct |
107 ms |
30584 KB |
Output is correct |
15 |
Correct |
123 ms |
30580 KB |
Output is correct |
16 |
Correct |
103 ms |
30584 KB |
Output is correct |
17 |
Correct |
103 ms |
30560 KB |
Output is correct |
18 |
Correct |
106 ms |
30456 KB |
Output is correct |
19 |
Correct |
109 ms |
30584 KB |
Output is correct |
20 |
Correct |
114 ms |
30504 KB |
Output is correct |
21 |
Correct |
15 ms |
6016 KB |
Output is correct |
22 |
Correct |
108 ms |
30844 KB |
Output is correct |
23 |
Correct |
127 ms |
30840 KB |
Output is correct |
24 |
Correct |
111 ms |
30884 KB |
Output is correct |
25 |
Correct |
111 ms |
30860 KB |
Output is correct |
26 |
Correct |
108 ms |
30840 KB |
Output is correct |
27 |
Correct |
134 ms |
30840 KB |
Output is correct |
28 |
Correct |
132 ms |
30840 KB |
Output is correct |
29 |
Correct |
114 ms |
30844 KB |
Output is correct |
30 |
Correct |
115 ms |
30968 KB |
Output is correct |
31 |
Correct |
151 ms |
22260 KB |
Output is correct |
32 |
Correct |
106 ms |
30584 KB |
Output is correct |
33 |
Correct |
209 ms |
27384 KB |
Output is correct |
34 |
Correct |
126 ms |
22260 KB |
Output is correct |
35 |
Correct |
163 ms |
27128 KB |
Output is correct |
36 |
Correct |
113 ms |
22392 KB |
Output is correct |
37 |
Correct |
214 ms |
26792 KB |
Output is correct |
38 |
Correct |
170 ms |
22776 KB |
Output is correct |
39 |
Correct |
105 ms |
30584 KB |
Output is correct |
40 |
Correct |
198 ms |
25972 KB |
Output is correct |
41 |
Correct |
9 ms |
5248 KB |
Output is correct |
42 |
Correct |
9 ms |
5376 KB |
Output is correct |
43 |
Correct |
9 ms |
5248 KB |
Output is correct |
44 |
Correct |
10 ms |
5248 KB |
Output is correct |
45 |
Correct |
9 ms |
5248 KB |
Output is correct |
46 |
Correct |
10 ms |
5248 KB |
Output is correct |
47 |
Correct |
8 ms |
5248 KB |
Output is correct |
48 |
Correct |
9 ms |
5248 KB |
Output is correct |
49 |
Correct |
9 ms |
5248 KB |
Output is correct |
50 |
Correct |
9 ms |
5376 KB |
Output is correct |
51 |
Correct |
165 ms |
23276 KB |
Output is correct |
52 |
Correct |
106 ms |
30844 KB |
Output is correct |
53 |
Correct |
181 ms |
26284 KB |
Output is correct |
54 |
Correct |
121 ms |
22648 KB |
Output is correct |
55 |
Correct |
193 ms |
22644 KB |
Output is correct |
56 |
Correct |
116 ms |
30816 KB |
Output is correct |
57 |
Correct |
171 ms |
26912 KB |
Output is correct |
58 |
Correct |
156 ms |
22520 KB |
Output is correct |
59 |
Correct |
184 ms |
23032 KB |
Output is correct |
60 |
Correct |
111 ms |
30840 KB |
Output is correct |
61 |
Correct |
162 ms |
27000 KB |
Output is correct |
62 |
Correct |
128 ms |
22576 KB |
Output is correct |
63 |
Correct |
158 ms |
22656 KB |
Output is correct |
64 |
Correct |
106 ms |
30840 KB |
Output is correct |
65 |
Correct |
186 ms |
26864 KB |
Output is correct |
66 |
Correct |
133 ms |
22648 KB |
Output is correct |
67 |
Correct |
161 ms |
22840 KB |
Output is correct |
68 |
Correct |
105 ms |
30840 KB |
Output is correct |
69 |
Correct |
168 ms |
25848 KB |
Output is correct |
70 |
Correct |
114 ms |
22648 KB |
Output is correct |