#include<bits/stdc++.h>
#define fs first
#define sn second
#define mp make_pair
#define N 100100
#define ll long long
using namespace std;
ll n,m,ans,x,y,z,w,tot,h[N],f[N],fr[N],sz[N],hs[N],top[N],fa[N],dep[N],dfn[N],low[N],v[N];
vector<pair<pair<ll,ll>,ll> >d[N];
struct rec
{
ll to,nx;
}e[N<<1];
void addl(ll x,ll y)
{
e[++tot].to=y;
e[tot].nx=h[x];
h[x]=tot;
return;
}
struct TT
{
ll sum[N];
void add(ll y,ll z)
{
sum[y]+=z;
return;
}
ll ask(ll l,ll r)
{
return sum[l]-(hs[fr[r]]?sum[r+1]:0);
}
}T;
void dfs(ll x)
{
sz[x]=1;
for(ll i=h[x];i;i=e[i].nx){
ll y=e[i].to;
if(y==fa[x])continue;
fa[y]=x;
dep[y]=dep[x]+1;
dfs(y);
sz[x]+=sz[y];
if(sz[y]>sz[hs[x]])hs[x]=y;
}
return;
}
void dfs1(ll x,ll anc)
{
dfn[x]=++w;
fr[w]=x;
top[x]=anc;
if(hs[x])dfs1(hs[x],anc);
for(ll i=h[x];i;i=e[i].nx){
ll y=e[i].to;
if(y==fa[x]||y==hs[x])continue;
dfs1(y,y);
}
low[x]=w;
return;
}
ll lca(ll x,ll y)
{
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=fa[top[x]];
}
return (dep[x]<dep[y]?x:y);
}
ll ask(ll x,ll y)
{
ll sum=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
if(hs[x])sum+=f[hs[x]];
sum+=T.ask(dfn[top[x]],dfn[x])-f[top[x]];
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
if(hs[y])sum+=f[hs[y]];
sum+=T.ask(dfn[x],dfn[y]);
return sum;
}
bool cmp(ll x,ll y)
{
return dep[x]>dep[y];
}
int main()
{
scanf("%lld",&n);
for(ll i=1;i<n;++i){
scanf("%lld%lld",&x,&y);
addl(x,y);
addl(y,x);
}
fa[1]=1;
dep[1]=1;
dfs(1);
dfs1(1,1);
scanf("%lld",&m);
for(ll i=1;i<=m;++i){
scanf("%lld%lld%lld",&x,&y,&z);
ll u=lca(x,y);
d[u].push_back(mp(mp(x,y),z));
}
for(ll i=1;i<=n;++i)
v[i]=i;
sort(v+1,v+1+n,cmp);
for(ll i=1;i<=n;++i){
x=v[i];
if(hs[x])T.add(dfn[x],T.sum[dfn[x]+1]);
f[x]=ask(x,x);
for(ll j=0;j<d[x].size();++j){
f[x]=max(f[x],ask(d[x][j].fs.fs,d[x][j].fs.sn)+d[x][j].sn);
}
ans=max(ans,f[x]);
if(x!=1&&x==top[x])T.add(dfn[fa[x]],f[x]);
}
printf("%lld\n",ans);
return 0;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:113:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(ll j=0;j<d[x].size();++j){
| ~^~~~~~~~~~~~
election_campaign.cpp:90:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
election_campaign.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%lld%lld",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~~~~
election_campaign.cpp:100:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%lld",&m);
| ~~~~~^~~~~~~~~~~
election_campaign.cpp:102:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%lld%lld%lld",&x,&y,&z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12764 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
38 ms |
16280 KB |
Output is correct |
6 |
Correct |
29 ms |
22612 KB |
Output is correct |
7 |
Correct |
40 ms |
20344 KB |
Output is correct |
8 |
Correct |
30 ms |
16212 KB |
Output is correct |
9 |
Correct |
38 ms |
19240 KB |
Output is correct |
10 |
Correct |
26 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12736 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
55 ms |
27368 KB |
Output is correct |
5 |
Correct |
55 ms |
27356 KB |
Output is correct |
6 |
Correct |
52 ms |
27220 KB |
Output is correct |
7 |
Correct |
53 ms |
27216 KB |
Output is correct |
8 |
Correct |
57 ms |
27392 KB |
Output is correct |
9 |
Correct |
52 ms |
27320 KB |
Output is correct |
10 |
Correct |
59 ms |
27176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12736 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
55 ms |
27368 KB |
Output is correct |
5 |
Correct |
55 ms |
27356 KB |
Output is correct |
6 |
Correct |
52 ms |
27220 KB |
Output is correct |
7 |
Correct |
53 ms |
27216 KB |
Output is correct |
8 |
Correct |
57 ms |
27392 KB |
Output is correct |
9 |
Correct |
52 ms |
27320 KB |
Output is correct |
10 |
Correct |
59 ms |
27176 KB |
Output is correct |
11 |
Correct |
8 ms |
13916 KB |
Output is correct |
12 |
Correct |
56 ms |
27384 KB |
Output is correct |
13 |
Correct |
57 ms |
27276 KB |
Output is correct |
14 |
Correct |
61 ms |
27628 KB |
Output is correct |
15 |
Correct |
59 ms |
27220 KB |
Output is correct |
16 |
Correct |
54 ms |
27216 KB |
Output is correct |
17 |
Correct |
60 ms |
27220 KB |
Output is correct |
18 |
Correct |
55 ms |
27180 KB |
Output is correct |
19 |
Correct |
54 ms |
27220 KB |
Output is correct |
20 |
Correct |
56 ms |
27488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
20488 KB |
Output is correct |
2 |
Correct |
61 ms |
27252 KB |
Output is correct |
3 |
Correct |
72 ms |
24824 KB |
Output is correct |
4 |
Correct |
53 ms |
20596 KB |
Output is correct |
5 |
Correct |
69 ms |
24208 KB |
Output is correct |
6 |
Correct |
52 ms |
20668 KB |
Output is correct |
7 |
Correct |
80 ms |
24076 KB |
Output is correct |
8 |
Correct |
67 ms |
20732 KB |
Output is correct |
9 |
Correct |
54 ms |
27292 KB |
Output is correct |
10 |
Correct |
73 ms |
23020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12764 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
38 ms |
16280 KB |
Output is correct |
6 |
Correct |
29 ms |
22612 KB |
Output is correct |
7 |
Correct |
40 ms |
20344 KB |
Output is correct |
8 |
Correct |
30 ms |
16212 KB |
Output is correct |
9 |
Correct |
38 ms |
19240 KB |
Output is correct |
10 |
Correct |
26 ms |
16212 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
12748 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
12748 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
4 ms |
12752 KB |
Output is correct |
19 |
Correct |
3 ms |
12636 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12764 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
38 ms |
16280 KB |
Output is correct |
6 |
Correct |
29 ms |
22612 KB |
Output is correct |
7 |
Correct |
40 ms |
20344 KB |
Output is correct |
8 |
Correct |
30 ms |
16212 KB |
Output is correct |
9 |
Correct |
38 ms |
19240 KB |
Output is correct |
10 |
Correct |
26 ms |
16212 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12736 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
55 ms |
27368 KB |
Output is correct |
15 |
Correct |
55 ms |
27356 KB |
Output is correct |
16 |
Correct |
52 ms |
27220 KB |
Output is correct |
17 |
Correct |
53 ms |
27216 KB |
Output is correct |
18 |
Correct |
57 ms |
27392 KB |
Output is correct |
19 |
Correct |
52 ms |
27320 KB |
Output is correct |
20 |
Correct |
59 ms |
27176 KB |
Output is correct |
21 |
Correct |
8 ms |
13916 KB |
Output is correct |
22 |
Correct |
56 ms |
27384 KB |
Output is correct |
23 |
Correct |
57 ms |
27276 KB |
Output is correct |
24 |
Correct |
61 ms |
27628 KB |
Output is correct |
25 |
Correct |
59 ms |
27220 KB |
Output is correct |
26 |
Correct |
54 ms |
27216 KB |
Output is correct |
27 |
Correct |
60 ms |
27220 KB |
Output is correct |
28 |
Correct |
55 ms |
27180 KB |
Output is correct |
29 |
Correct |
54 ms |
27220 KB |
Output is correct |
30 |
Correct |
56 ms |
27488 KB |
Output is correct |
31 |
Correct |
74 ms |
20488 KB |
Output is correct |
32 |
Correct |
61 ms |
27252 KB |
Output is correct |
33 |
Correct |
72 ms |
24824 KB |
Output is correct |
34 |
Correct |
53 ms |
20596 KB |
Output is correct |
35 |
Correct |
69 ms |
24208 KB |
Output is correct |
36 |
Correct |
52 ms |
20668 KB |
Output is correct |
37 |
Correct |
80 ms |
24076 KB |
Output is correct |
38 |
Correct |
67 ms |
20732 KB |
Output is correct |
39 |
Correct |
54 ms |
27292 KB |
Output is correct |
40 |
Correct |
73 ms |
23020 KB |
Output is correct |
41 |
Correct |
3 ms |
12636 KB |
Output is correct |
42 |
Correct |
3 ms |
12748 KB |
Output is correct |
43 |
Correct |
3 ms |
12636 KB |
Output is correct |
44 |
Correct |
3 ms |
12636 KB |
Output is correct |
45 |
Correct |
3 ms |
12636 KB |
Output is correct |
46 |
Correct |
3 ms |
12748 KB |
Output is correct |
47 |
Correct |
3 ms |
12636 KB |
Output is correct |
48 |
Correct |
4 ms |
12752 KB |
Output is correct |
49 |
Correct |
3 ms |
12636 KB |
Output is correct |
50 |
Correct |
3 ms |
12892 KB |
Output is correct |
51 |
Correct |
77 ms |
20736 KB |
Output is correct |
52 |
Correct |
59 ms |
27272 KB |
Output is correct |
53 |
Correct |
71 ms |
23160 KB |
Output is correct |
54 |
Correct |
53 ms |
20296 KB |
Output is correct |
55 |
Correct |
74 ms |
20484 KB |
Output is correct |
56 |
Correct |
56 ms |
27332 KB |
Output is correct |
57 |
Correct |
83 ms |
23752 KB |
Output is correct |
58 |
Correct |
64 ms |
20236 KB |
Output is correct |
59 |
Correct |
76 ms |
20488 KB |
Output is correct |
60 |
Correct |
56 ms |
27220 KB |
Output is correct |
61 |
Correct |
74 ms |
23956 KB |
Output is correct |
62 |
Correct |
62 ms |
20092 KB |
Output is correct |
63 |
Correct |
80 ms |
20392 KB |
Output is correct |
64 |
Correct |
55 ms |
27220 KB |
Output is correct |
65 |
Correct |
77 ms |
23812 KB |
Output is correct |
66 |
Correct |
53 ms |
20332 KB |
Output is correct |
67 |
Correct |
78 ms |
20260 KB |
Output is correct |
68 |
Correct |
54 ms |
27256 KB |
Output is correct |
69 |
Correct |
87 ms |
22912 KB |
Output is correct |
70 |
Correct |
56 ms |
20236 KB |
Output is correct |