#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll P[maxN][18];
ll h[maxN],in[maxN],out[maxN];
ll lca(ll u,ll v)
{
if(h[u]<h[v]) swap(u,v);
for(int i=17;i>=0;i--) if(h[u]-(1<<i)>=h[v]) u=P[u][i];
if(u==v) return u;
for(int i=17;i>=0;i--) if(P[u][i]!=P[v][i]) u=P[u][i],v=P[v][i];
return P[u][0];
}
vector<ll>g[maxN];
ll tg=0;
void dfs(ll u,ll p)
{
P[u][0]=p;in[u]=++tg;
for(int i=1;i<=17;i++) P[u][i]=P[P[u][i-1]][i-1];
for(int v:g[u])
{
if(v!=p)
{
h[v]=h[u]+1;
dfs(v,u);
}
}
out[u]=tg;
}
ll dp[maxN];
struct qq
{
ll x,y,w;
};
vector<qq>vec[maxN];
ll bit[maxN];
ll n;
void update(ll i,ll v)
{
while(i<=n)
{
bit[i]+=v;
i+=i&(-i);
}
}
ll get(ll i)
{
ll ans=0;
while(i>0)
{
ans+=bit[i];
i-=i&(-i);
}
return ans;
}
void DFS(ll u,ll p)
{
for(int v:g[u])
{
if(v!=p)
{
DFS(v,u);
dp[u]+=dp[v];
}
}
ll x=dp[u];
for(auto zz:vec[u])
{
dp[u]=max(dp[u],get(in[zz.x])+get(in[zz.y])+x+zz.w);
if(u==5&&zz.x==6&&zz.y==2)
{
cout << get(in[zz.x])<< ' '<<get(in[zz.y])<<' '<<x<<' '<<zz.x<<'\n';
}
}
update(in[u],x-dp[u]);
update(out[u]+1,dp[u]-x);
}
ll m,k;
void solve()
{
cin >>n;
for(int i=1;i<n;i++)
{
ll u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1,1);
//for(int i=1;i<=n;i++) cout << in[i]<<' ';
//cout << '\n';
cin >> m;
for(int i=1;i<=m;i++)
{
ll x,y,w;
cin >> x>> y>> w;
k=lca(x,y);
vec[k].pb({x,y,w});
//cout << k<<' '<<x<<' '<<y<<'\n';
}
DFS(1,1);
cout << dp[1];
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9736 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9872 KB |
Output is correct |
5 |
Correct |
86 ms |
32800 KB |
Output is correct |
6 |
Correct |
47 ms |
42956 KB |
Output is correct |
7 |
Correct |
91 ms |
39512 KB |
Output is correct |
8 |
Correct |
57 ms |
32200 KB |
Output is correct |
9 |
Correct |
85 ms |
37408 KB |
Output is correct |
10 |
Correct |
60 ms |
32144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
10068 KB |
Output is correct |
4 |
Correct |
96 ms |
47768 KB |
Output is correct |
5 |
Correct |
96 ms |
47736 KB |
Output is correct |
6 |
Correct |
88 ms |
47820 KB |
Output is correct |
7 |
Correct |
100 ms |
47768 KB |
Output is correct |
8 |
Correct |
113 ms |
47824 KB |
Output is correct |
9 |
Correct |
99 ms |
47748 KB |
Output is correct |
10 |
Correct |
99 ms |
47784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
10068 KB |
Output is correct |
4 |
Correct |
96 ms |
47768 KB |
Output is correct |
5 |
Correct |
96 ms |
47736 KB |
Output is correct |
6 |
Correct |
88 ms |
47820 KB |
Output is correct |
7 |
Correct |
100 ms |
47768 KB |
Output is correct |
8 |
Correct |
113 ms |
47824 KB |
Output is correct |
9 |
Correct |
99 ms |
47748 KB |
Output is correct |
10 |
Correct |
99 ms |
47784 KB |
Output is correct |
11 |
Correct |
11 ms |
11220 KB |
Output is correct |
12 |
Correct |
97 ms |
47984 KB |
Output is correct |
13 |
Correct |
106 ms |
47984 KB |
Output is correct |
14 |
Correct |
91 ms |
48028 KB |
Output is correct |
15 |
Correct |
102 ms |
48076 KB |
Output is correct |
16 |
Correct |
90 ms |
48104 KB |
Output is correct |
17 |
Correct |
99 ms |
48088 KB |
Output is correct |
18 |
Correct |
118 ms |
48180 KB |
Output is correct |
19 |
Correct |
94 ms |
48052 KB |
Output is correct |
20 |
Correct |
115 ms |
48076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
36940 KB |
Output is correct |
2 |
Correct |
122 ms |
47820 KB |
Output is correct |
3 |
Correct |
174 ms |
43852 KB |
Output is correct |
4 |
Correct |
93 ms |
36824 KB |
Output is correct |
5 |
Correct |
163 ms |
42980 KB |
Output is correct |
6 |
Correct |
89 ms |
36448 KB |
Output is correct |
7 |
Correct |
179 ms |
42836 KB |
Output is correct |
8 |
Correct |
150 ms |
37036 KB |
Output is correct |
9 |
Correct |
88 ms |
47812 KB |
Output is correct |
10 |
Correct |
166 ms |
41280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9736 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9872 KB |
Output is correct |
5 |
Correct |
86 ms |
32800 KB |
Output is correct |
6 |
Correct |
47 ms |
42956 KB |
Output is correct |
7 |
Correct |
91 ms |
39512 KB |
Output is correct |
8 |
Correct |
57 ms |
32200 KB |
Output is correct |
9 |
Correct |
85 ms |
37408 KB |
Output is correct |
10 |
Correct |
60 ms |
32144 KB |
Output is correct |
11 |
Correct |
5 ms |
10004 KB |
Output is correct |
12 |
Correct |
8 ms |
10040 KB |
Output is correct |
13 |
Correct |
5 ms |
10068 KB |
Output is correct |
14 |
Correct |
5 ms |
9940 KB |
Output is correct |
15 |
Correct |
5 ms |
10032 KB |
Output is correct |
16 |
Correct |
5 ms |
9940 KB |
Output is correct |
17 |
Correct |
5 ms |
10004 KB |
Output is correct |
18 |
Correct |
5 ms |
10016 KB |
Output is correct |
19 |
Correct |
5 ms |
10068 KB |
Output is correct |
20 |
Correct |
6 ms |
10068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9736 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9872 KB |
Output is correct |
5 |
Correct |
86 ms |
32800 KB |
Output is correct |
6 |
Correct |
47 ms |
42956 KB |
Output is correct |
7 |
Correct |
91 ms |
39512 KB |
Output is correct |
8 |
Correct |
57 ms |
32200 KB |
Output is correct |
9 |
Correct |
85 ms |
37408 KB |
Output is correct |
10 |
Correct |
60 ms |
32144 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
10068 KB |
Output is correct |
14 |
Correct |
96 ms |
47768 KB |
Output is correct |
15 |
Correct |
96 ms |
47736 KB |
Output is correct |
16 |
Correct |
88 ms |
47820 KB |
Output is correct |
17 |
Correct |
100 ms |
47768 KB |
Output is correct |
18 |
Correct |
113 ms |
47824 KB |
Output is correct |
19 |
Correct |
99 ms |
47748 KB |
Output is correct |
20 |
Correct |
99 ms |
47784 KB |
Output is correct |
21 |
Correct |
11 ms |
11220 KB |
Output is correct |
22 |
Correct |
97 ms |
47984 KB |
Output is correct |
23 |
Correct |
106 ms |
47984 KB |
Output is correct |
24 |
Correct |
91 ms |
48028 KB |
Output is correct |
25 |
Correct |
102 ms |
48076 KB |
Output is correct |
26 |
Correct |
90 ms |
48104 KB |
Output is correct |
27 |
Correct |
99 ms |
48088 KB |
Output is correct |
28 |
Correct |
118 ms |
48180 KB |
Output is correct |
29 |
Correct |
94 ms |
48052 KB |
Output is correct |
30 |
Correct |
115 ms |
48076 KB |
Output is correct |
31 |
Correct |
123 ms |
36940 KB |
Output is correct |
32 |
Correct |
122 ms |
47820 KB |
Output is correct |
33 |
Correct |
174 ms |
43852 KB |
Output is correct |
34 |
Correct |
93 ms |
36824 KB |
Output is correct |
35 |
Correct |
163 ms |
42980 KB |
Output is correct |
36 |
Correct |
89 ms |
36448 KB |
Output is correct |
37 |
Correct |
179 ms |
42836 KB |
Output is correct |
38 |
Correct |
150 ms |
37036 KB |
Output is correct |
39 |
Correct |
88 ms |
47812 KB |
Output is correct |
40 |
Correct |
166 ms |
41280 KB |
Output is correct |
41 |
Correct |
5 ms |
10004 KB |
Output is correct |
42 |
Correct |
8 ms |
10040 KB |
Output is correct |
43 |
Correct |
5 ms |
10068 KB |
Output is correct |
44 |
Correct |
5 ms |
9940 KB |
Output is correct |
45 |
Correct |
5 ms |
10032 KB |
Output is correct |
46 |
Correct |
5 ms |
9940 KB |
Output is correct |
47 |
Correct |
5 ms |
10004 KB |
Output is correct |
48 |
Correct |
5 ms |
10016 KB |
Output is correct |
49 |
Correct |
5 ms |
10068 KB |
Output is correct |
50 |
Correct |
6 ms |
10068 KB |
Output is correct |
51 |
Correct |
138 ms |
37364 KB |
Output is correct |
52 |
Correct |
97 ms |
48020 KB |
Output is correct |
53 |
Correct |
172 ms |
41724 KB |
Output is correct |
54 |
Correct |
94 ms |
36736 KB |
Output is correct |
55 |
Correct |
123 ms |
37180 KB |
Output is correct |
56 |
Correct |
96 ms |
48000 KB |
Output is correct |
57 |
Correct |
146 ms |
42632 KB |
Output is correct |
58 |
Correct |
97 ms |
36664 KB |
Output is correct |
59 |
Correct |
129 ms |
37392 KB |
Output is correct |
60 |
Correct |
96 ms |
48076 KB |
Output is correct |
61 |
Correct |
158 ms |
42764 KB |
Output is correct |
62 |
Correct |
95 ms |
36380 KB |
Output is correct |
63 |
Correct |
126 ms |
37032 KB |
Output is correct |
64 |
Correct |
96 ms |
48016 KB |
Output is correct |
65 |
Correct |
161 ms |
42628 KB |
Output is correct |
66 |
Correct |
95 ms |
36620 KB |
Output is correct |
67 |
Correct |
128 ms |
37168 KB |
Output is correct |
68 |
Correct |
98 ms |
48012 KB |
Output is correct |
69 |
Correct |
168 ms |
41064 KB |
Output is correct |
70 |
Correct |
100 ms |
36708 KB |
Output is correct |