#include <bits/stdc++.h>
#define ll long long
#define MAXN 100010
#define logn 19
using namespace std;
vector<ll> G[MAXN],V[MAXN];
ll up[MAXN][logn],in[MAXN],out[MAXN],t=1,D[MAXN],a[MAXN],b[MAXN],c[MAXN],dp[MAXN][2];
void DFS_1(ll i,ll p)
{
up[i][0]=p;
in[i]=t;
D[i]=D[p]+1;
t++;
for (ll j=1;j<logn;j++)
up[i][j]=up[up[i][j-1]][j-1];
for (ll next : G[i])
{
if (next!=p)
DFS_1(next,i);
}
out[i]=t;
t++;
}
bool Is_Ancestor(ll u,ll v)
{
return (in[u]<=in[v] && out[u]>=out[v]);
}
ll LCA(ll u,ll v)
{
if (Is_Ancestor(u,v))
return u;
if (Is_Ancestor(v,u))
return v;
for (ll i=logn-1;i>=0;i--)
{
if (!Is_Ancestor(up[u][i],v))
u=up[u][i];
}
return up[u][0];
}
struct bit
{
vector<ll> B;
void Init()
{
B.resize(t+2);
}
void Add(ll val,ll pos)
{
for (ll i=pos;i<t+2;i+=i&(-i))
B[i]+=val;
}
ll Sum(ll pos)
{
ll ans=0;
for (ll i=pos;i>0;i-=i&(-i))
ans+=B[i];
return ans;
}
ll Calc(ll l,ll r)
{
return Sum(r)-Sum(l-1);
}
};
bit B0,B1;
void DFS_2(ll i,ll p)
{
for (ll next : G[i])
{
if (next!=p)
{
DFS_2(next,i);
dp[i][0]+=max(dp[next][0],dp[next][1]);
}
}
B0.Add(dp[i][0],in[i]);
B0.Add(-dp[i][0],out[i]);
for (auto j : V[i])
{
dp[i][1]=max(dp[i][1],c[j]+B0.Calc(in[i],in[a[j]])+B0.Calc(in[i],in[b[j]])-dp[i][0]-B1.Calc(in[i]+1,in[a[j]])-B1.Calc(in[i]+1,in[b[j]]));
}
B1.Add(max(dp[i][0],dp[i][1]),in[i]);
B1.Add(-max(dp[i][0],dp[i][1]),out[i]);
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,m,x,y;
cin >> n;
for (ll i=1;i<n;i++)
{
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
DFS_1(1,1);
cin >> m;
for (ll i=1;i<=m;i++)
{
cin >> a[i] >> b[i] >> c[i];
V[LCA(a[i],b[i])].push_back(i);
}
B0.Init();
B1.Init();
DFS_2(1,1);
cout << max(dp[1][1],dp[1][0]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5332 KB |
Output is correct |
5 |
Correct |
106 ms |
32024 KB |
Output is correct |
6 |
Correct |
59 ms |
40524 KB |
Output is correct |
7 |
Correct |
119 ms |
37772 KB |
Output is correct |
8 |
Correct |
78 ms |
30624 KB |
Output is correct |
9 |
Correct |
106 ms |
35832 KB |
Output is correct |
10 |
Correct |
104 ms |
30516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
105 ms |
45768 KB |
Output is correct |
5 |
Correct |
119 ms |
45784 KB |
Output is correct |
6 |
Correct |
116 ms |
45828 KB |
Output is correct |
7 |
Correct |
97 ms |
45772 KB |
Output is correct |
8 |
Correct |
103 ms |
45832 KB |
Output is correct |
9 |
Correct |
102 ms |
45784 KB |
Output is correct |
10 |
Correct |
120 ms |
45748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
105 ms |
45768 KB |
Output is correct |
5 |
Correct |
119 ms |
45784 KB |
Output is correct |
6 |
Correct |
116 ms |
45828 KB |
Output is correct |
7 |
Correct |
97 ms |
45772 KB |
Output is correct |
8 |
Correct |
103 ms |
45832 KB |
Output is correct |
9 |
Correct |
102 ms |
45784 KB |
Output is correct |
10 |
Correct |
120 ms |
45748 KB |
Output is correct |
11 |
Correct |
12 ms |
6484 KB |
Output is correct |
12 |
Correct |
106 ms |
46104 KB |
Output is correct |
13 |
Correct |
119 ms |
46012 KB |
Output is correct |
14 |
Correct |
138 ms |
46100 KB |
Output is correct |
15 |
Correct |
107 ms |
46108 KB |
Output is correct |
16 |
Correct |
105 ms |
46068 KB |
Output is correct |
17 |
Correct |
102 ms |
46056 KB |
Output is correct |
18 |
Correct |
108 ms |
46024 KB |
Output is correct |
19 |
Correct |
105 ms |
46156 KB |
Output is correct |
20 |
Correct |
103 ms |
46044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
36668 KB |
Output is correct |
2 |
Correct |
104 ms |
45808 KB |
Output is correct |
3 |
Correct |
227 ms |
42532 KB |
Output is correct |
4 |
Correct |
159 ms |
35228 KB |
Output is correct |
5 |
Correct |
215 ms |
42040 KB |
Output is correct |
6 |
Correct |
159 ms |
35192 KB |
Output is correct |
7 |
Correct |
287 ms |
41700 KB |
Output is correct |
8 |
Correct |
168 ms |
36868 KB |
Output is correct |
9 |
Correct |
114 ms |
45872 KB |
Output is correct |
10 |
Correct |
220 ms |
40540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5332 KB |
Output is correct |
5 |
Correct |
106 ms |
32024 KB |
Output is correct |
6 |
Correct |
59 ms |
40524 KB |
Output is correct |
7 |
Correct |
119 ms |
37772 KB |
Output is correct |
8 |
Correct |
78 ms |
30624 KB |
Output is correct |
9 |
Correct |
106 ms |
35832 KB |
Output is correct |
10 |
Correct |
104 ms |
30516 KB |
Output is correct |
11 |
Correct |
4 ms |
5296 KB |
Output is correct |
12 |
Correct |
3 ms |
5428 KB |
Output is correct |
13 |
Correct |
4 ms |
5420 KB |
Output is correct |
14 |
Correct |
4 ms |
5332 KB |
Output is correct |
15 |
Correct |
4 ms |
5332 KB |
Output is correct |
16 |
Correct |
5 ms |
5332 KB |
Output is correct |
17 |
Correct |
4 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5332 KB |
Output is correct |
19 |
Correct |
5 ms |
5300 KB |
Output is correct |
20 |
Correct |
4 ms |
5464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5332 KB |
Output is correct |
5 |
Correct |
106 ms |
32024 KB |
Output is correct |
6 |
Correct |
59 ms |
40524 KB |
Output is correct |
7 |
Correct |
119 ms |
37772 KB |
Output is correct |
8 |
Correct |
78 ms |
30624 KB |
Output is correct |
9 |
Correct |
106 ms |
35832 KB |
Output is correct |
10 |
Correct |
104 ms |
30516 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
5020 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
105 ms |
45768 KB |
Output is correct |
15 |
Correct |
119 ms |
45784 KB |
Output is correct |
16 |
Correct |
116 ms |
45828 KB |
Output is correct |
17 |
Correct |
97 ms |
45772 KB |
Output is correct |
18 |
Correct |
103 ms |
45832 KB |
Output is correct |
19 |
Correct |
102 ms |
45784 KB |
Output is correct |
20 |
Correct |
120 ms |
45748 KB |
Output is correct |
21 |
Correct |
12 ms |
6484 KB |
Output is correct |
22 |
Correct |
106 ms |
46104 KB |
Output is correct |
23 |
Correct |
119 ms |
46012 KB |
Output is correct |
24 |
Correct |
138 ms |
46100 KB |
Output is correct |
25 |
Correct |
107 ms |
46108 KB |
Output is correct |
26 |
Correct |
105 ms |
46068 KB |
Output is correct |
27 |
Correct |
102 ms |
46056 KB |
Output is correct |
28 |
Correct |
108 ms |
46024 KB |
Output is correct |
29 |
Correct |
105 ms |
46156 KB |
Output is correct |
30 |
Correct |
103 ms |
46044 KB |
Output is correct |
31 |
Correct |
191 ms |
36668 KB |
Output is correct |
32 |
Correct |
104 ms |
45808 KB |
Output is correct |
33 |
Correct |
227 ms |
42532 KB |
Output is correct |
34 |
Correct |
159 ms |
35228 KB |
Output is correct |
35 |
Correct |
215 ms |
42040 KB |
Output is correct |
36 |
Correct |
159 ms |
35192 KB |
Output is correct |
37 |
Correct |
287 ms |
41700 KB |
Output is correct |
38 |
Correct |
168 ms |
36868 KB |
Output is correct |
39 |
Correct |
114 ms |
45872 KB |
Output is correct |
40 |
Correct |
220 ms |
40540 KB |
Output is correct |
41 |
Correct |
4 ms |
5296 KB |
Output is correct |
42 |
Correct |
3 ms |
5428 KB |
Output is correct |
43 |
Correct |
4 ms |
5420 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
4 ms |
5332 KB |
Output is correct |
46 |
Correct |
5 ms |
5332 KB |
Output is correct |
47 |
Correct |
4 ms |
5332 KB |
Output is correct |
48 |
Correct |
4 ms |
5332 KB |
Output is correct |
49 |
Correct |
5 ms |
5300 KB |
Output is correct |
50 |
Correct |
4 ms |
5464 KB |
Output is correct |
51 |
Correct |
171 ms |
37164 KB |
Output is correct |
52 |
Correct |
105 ms |
46008 KB |
Output is correct |
53 |
Correct |
213 ms |
41060 KB |
Output is correct |
54 |
Correct |
124 ms |
35528 KB |
Output is correct |
55 |
Correct |
180 ms |
37008 KB |
Output is correct |
56 |
Correct |
108 ms |
46028 KB |
Output is correct |
57 |
Correct |
188 ms |
41868 KB |
Output is correct |
58 |
Correct |
134 ms |
35560 KB |
Output is correct |
59 |
Correct |
188 ms |
37204 KB |
Output is correct |
60 |
Correct |
112 ms |
46004 KB |
Output is correct |
61 |
Correct |
199 ms |
41900 KB |
Output is correct |
62 |
Correct |
146 ms |
35424 KB |
Output is correct |
63 |
Correct |
195 ms |
36752 KB |
Output is correct |
64 |
Correct |
107 ms |
46008 KB |
Output is correct |
65 |
Correct |
227 ms |
41708 KB |
Output is correct |
66 |
Correct |
152 ms |
35576 KB |
Output is correct |
67 |
Correct |
178 ms |
36800 KB |
Output is correct |
68 |
Correct |
105 ms |
46012 KB |
Output is correct |
69 |
Correct |
224 ms |
40328 KB |
Output is correct |
70 |
Correct |
140 ms |
35676 KB |
Output is correct |