#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
const ll maxn=1e5+10;
const ll mod =1e9+7 ;
const ll base=3e18;
/// con 15 ngay thoi thang ngu
vector<pair<pll,ll>> adj1[maxn];
vector<ll> adj[maxn];
ll dp[maxn];
ll dep[maxn];
ll anc[maxn][20];
ll cnt=0;
ll f[maxn];
ll l[maxn];
ll fwt[2*maxn];
void update(ll l, ll r, ll d) {
for (int p = l; p <= cnt; p += p & -p) fwt[p] = (fwt[p] + d) % mod;
++r;
for (int p = r; p <= cnt; p += p & -p) fwt[p] = (fwt[p] + mod - d) % mod;
}
ll get(ll p) {
ll ret = 0;
for (; p; p -= p & -p) ret = (ret + fwt[p]) % mod;
return ret;
}
void dfs(ll u,ll par)
{
cnt++;
f[u]=cnt;
anc[u][0]=par;
for (int i=1;i<=19;i++)
{
anc[u][i]=anc[anc[u][i-1]][i-1];
}
for (auto to:adj[u])
{
if (to==par) continue;
dep[to]=dep[u]+1;
dfs(to, u);
}
l[u]=cnt;
}
ll lca(ll x,ll y)
{
if (dep[x]<dep[y]) swap(x,y);
ll kc=dep[x]-dep[y];
for (int i=19;i>=0;i--)
{
if (kc&(1ll<<i))
{
x=anc[x][i];
}
}
if (x==y) return x;
for (int i=19;i>=0;i--)
{
if (anc[x][i]!=anc[y][i])
{
x=anc[x][i];
y=anc[y][i];
}
}
return anc[x][0];
}
ll pos[maxn];
ll near(ll x,ll y)
{
ll kc=dep[x]-dep[y]-1;
for (int i=19;i>=0;i--)
{
if (kc&(1ll<<i))
{
x=anc[x][i];
}
}
return x;
}
ll dp1[maxn];
void dfs1(ll u,ll par)
{
dp[u]=0;
ll cnt=0;
for (auto to:adj[u])
{
if (to==par) continue;
dfs1(to,u);
update(f[u],l[u],dp[to]);
update(f[to],l[to],-dp[to]);
dp[u]+=dp[to];
}
// if (u==3) cout <<adj1[u].size()<<" "<<cnt<<" "<<f[cnt]<<endl;
for (auto p:adj1[u])
{
ll x=p.ff.ff;
ll y=p.ff.ss;
ll w=p.ss;
dp[u]=max(dp[u],get(f[x])+get(f[y])-get(f[par])-get(f[u])+w);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n;
cin>> n;
for (int i=1;i<=n-1;i++)
{
ll x, y;
cin>> x>> y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1,0);
ll q;
cin>> q;
for (int i=1;i<=q;i++)
{
ll x, y, w;
cin>> x>> y>> w;
adj1[lca(x,y)].pb(make_pair(make_pair(x,y),w));
}
dfs1(1,0);
cout <<dp[1];
/*7
2 1
3 2
4 1
5 2
6 3
7 5
3
3 6 4
2 3 5
4 7 7
*/
}
Compilation message
election_campaign.cpp: In function 'void dfs1(long long int, long long int)':
election_campaign.cpp:98:8: warning: unused variable 'cnt' [-Wunused-variable]
98 | ll cnt=0;
| ^~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
124 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
125 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
150 ms |
29676 KB |
Output is correct |
6 |
Correct |
74 ms |
38252 KB |
Output is correct |
7 |
Correct |
153 ms |
35436 KB |
Output is correct |
8 |
Correct |
108 ms |
29804 KB |
Output is correct |
9 |
Correct |
140 ms |
33644 KB |
Output is correct |
10 |
Correct |
105 ms |
29804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5356 KB |
Output is correct |
4 |
Correct |
191 ms |
40684 KB |
Output is correct |
5 |
Correct |
188 ms |
40704 KB |
Output is correct |
6 |
Correct |
166 ms |
40812 KB |
Output is correct |
7 |
Correct |
190 ms |
40812 KB |
Output is correct |
8 |
Correct |
188 ms |
40684 KB |
Output is correct |
9 |
Correct |
164 ms |
40684 KB |
Output is correct |
10 |
Correct |
190 ms |
40752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5356 KB |
Output is correct |
4 |
Correct |
191 ms |
40684 KB |
Output is correct |
5 |
Correct |
188 ms |
40704 KB |
Output is correct |
6 |
Correct |
166 ms |
40812 KB |
Output is correct |
7 |
Correct |
190 ms |
40812 KB |
Output is correct |
8 |
Correct |
188 ms |
40684 KB |
Output is correct |
9 |
Correct |
164 ms |
40684 KB |
Output is correct |
10 |
Correct |
190 ms |
40752 KB |
Output is correct |
11 |
Correct |
14 ms |
6380 KB |
Output is correct |
12 |
Correct |
193 ms |
40684 KB |
Output is correct |
13 |
Correct |
206 ms |
40684 KB |
Output is correct |
14 |
Correct |
164 ms |
40684 KB |
Output is correct |
15 |
Correct |
193 ms |
40812 KB |
Output is correct |
16 |
Correct |
162 ms |
40684 KB |
Output is correct |
17 |
Correct |
186 ms |
40612 KB |
Output is correct |
18 |
Correct |
190 ms |
40684 KB |
Output is correct |
19 |
Correct |
168 ms |
40684 KB |
Output is correct |
20 |
Correct |
190 ms |
40684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
31520 KB |
Output is correct |
2 |
Correct |
165 ms |
43148 KB |
Output is correct |
3 |
Correct |
319 ms |
39848 KB |
Output is correct |
4 |
Correct |
177 ms |
34336 KB |
Output is correct |
5 |
Correct |
290 ms |
39336 KB |
Output is correct |
6 |
Correct |
187 ms |
34136 KB |
Output is correct |
7 |
Correct |
311 ms |
38948 KB |
Output is correct |
8 |
Correct |
238 ms |
34156 KB |
Output is correct |
9 |
Correct |
167 ms |
43116 KB |
Output is correct |
10 |
Correct |
318 ms |
37536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
150 ms |
29676 KB |
Output is correct |
6 |
Correct |
74 ms |
38252 KB |
Output is correct |
7 |
Correct |
153 ms |
35436 KB |
Output is correct |
8 |
Correct |
108 ms |
29804 KB |
Output is correct |
9 |
Correct |
140 ms |
33644 KB |
Output is correct |
10 |
Correct |
105 ms |
29804 KB |
Output is correct |
11 |
Correct |
5 ms |
5356 KB |
Output is correct |
12 |
Correct |
5 ms |
5484 KB |
Output is correct |
13 |
Correct |
5 ms |
5356 KB |
Output is correct |
14 |
Correct |
5 ms |
5356 KB |
Output is correct |
15 |
Correct |
5 ms |
5356 KB |
Output is correct |
16 |
Correct |
5 ms |
5356 KB |
Output is correct |
17 |
Correct |
5 ms |
5356 KB |
Output is correct |
18 |
Correct |
5 ms |
5504 KB |
Output is correct |
19 |
Correct |
5 ms |
5356 KB |
Output is correct |
20 |
Correct |
5 ms |
5484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
150 ms |
29676 KB |
Output is correct |
6 |
Correct |
74 ms |
38252 KB |
Output is correct |
7 |
Correct |
153 ms |
35436 KB |
Output is correct |
8 |
Correct |
108 ms |
29804 KB |
Output is correct |
9 |
Correct |
140 ms |
33644 KB |
Output is correct |
10 |
Correct |
105 ms |
29804 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5356 KB |
Output is correct |
14 |
Correct |
191 ms |
40684 KB |
Output is correct |
15 |
Correct |
188 ms |
40704 KB |
Output is correct |
16 |
Correct |
166 ms |
40812 KB |
Output is correct |
17 |
Correct |
190 ms |
40812 KB |
Output is correct |
18 |
Correct |
188 ms |
40684 KB |
Output is correct |
19 |
Correct |
164 ms |
40684 KB |
Output is correct |
20 |
Correct |
190 ms |
40752 KB |
Output is correct |
21 |
Correct |
14 ms |
6380 KB |
Output is correct |
22 |
Correct |
193 ms |
40684 KB |
Output is correct |
23 |
Correct |
206 ms |
40684 KB |
Output is correct |
24 |
Correct |
164 ms |
40684 KB |
Output is correct |
25 |
Correct |
193 ms |
40812 KB |
Output is correct |
26 |
Correct |
162 ms |
40684 KB |
Output is correct |
27 |
Correct |
186 ms |
40612 KB |
Output is correct |
28 |
Correct |
190 ms |
40684 KB |
Output is correct |
29 |
Correct |
168 ms |
40684 KB |
Output is correct |
30 |
Correct |
190 ms |
40684 KB |
Output is correct |
31 |
Correct |
225 ms |
31520 KB |
Output is correct |
32 |
Correct |
165 ms |
43148 KB |
Output is correct |
33 |
Correct |
319 ms |
39848 KB |
Output is correct |
34 |
Correct |
177 ms |
34336 KB |
Output is correct |
35 |
Correct |
290 ms |
39336 KB |
Output is correct |
36 |
Correct |
187 ms |
34136 KB |
Output is correct |
37 |
Correct |
311 ms |
38948 KB |
Output is correct |
38 |
Correct |
238 ms |
34156 KB |
Output is correct |
39 |
Correct |
167 ms |
43116 KB |
Output is correct |
40 |
Correct |
318 ms |
37536 KB |
Output is correct |
41 |
Correct |
5 ms |
5356 KB |
Output is correct |
42 |
Correct |
5 ms |
5484 KB |
Output is correct |
43 |
Correct |
5 ms |
5356 KB |
Output is correct |
44 |
Correct |
5 ms |
5356 KB |
Output is correct |
45 |
Correct |
5 ms |
5356 KB |
Output is correct |
46 |
Correct |
5 ms |
5356 KB |
Output is correct |
47 |
Correct |
5 ms |
5356 KB |
Output is correct |
48 |
Correct |
5 ms |
5504 KB |
Output is correct |
49 |
Correct |
5 ms |
5356 KB |
Output is correct |
50 |
Correct |
5 ms |
5484 KB |
Output is correct |
51 |
Correct |
247 ms |
34472 KB |
Output is correct |
52 |
Correct |
201 ms |
43500 KB |
Output is correct |
53 |
Correct |
314 ms |
38048 KB |
Output is correct |
54 |
Correct |
176 ms |
34528 KB |
Output is correct |
55 |
Correct |
232 ms |
34208 KB |
Output is correct |
56 |
Correct |
192 ms |
43372 KB |
Output is correct |
57 |
Correct |
267 ms |
38824 KB |
Output is correct |
58 |
Correct |
175 ms |
34464 KB |
Output is correct |
59 |
Correct |
238 ms |
34276 KB |
Output is correct |
60 |
Correct |
191 ms |
43372 KB |
Output is correct |
61 |
Correct |
268 ms |
39036 KB |
Output is correct |
62 |
Correct |
174 ms |
33984 KB |
Output is correct |
63 |
Correct |
223 ms |
33924 KB |
Output is correct |
64 |
Correct |
185 ms |
43372 KB |
Output is correct |
65 |
Correct |
313 ms |
38816 KB |
Output is correct |
66 |
Correct |
175 ms |
34380 KB |
Output is correct |
67 |
Correct |
226 ms |
34264 KB |
Output is correct |
68 |
Correct |
190 ms |
43500 KB |
Output is correct |
69 |
Correct |
274 ms |
37544 KB |
Output is correct |
70 |
Correct |
181 ms |
34452 KB |
Output is correct |