#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
template<typename T> bool ckmax(T& a, const T& b) {return b>a?a=b,1:0;}
template<typename T> bool ckmin(T& a, const T& b) {return b<a?a=b,1:0;}
const int MN = 1e5+10;
const int MM = 1e5+10;
const int MV = 1e4+10;
struct Qry
{
public:
int a,b,c;
};
struct bit: public std::vector<int>
{
public:
int sz;
void init(int _sz)
{
sz=_sz;
assign(sz*4, 0);
}
bit(int _sz=0): sz(_sz), std::vector<int>(_sz*4, 0) {}
void upd(int n, int v)
{
assert(0<=n&&n<sz);
for(++n;n<=sz;n+=-n&n)
at(n-1)+=v;
}
int qry(int n)
{
assert(0<=n&&n<=sz);
int v=0;
for(;n;n-=-n&n)
v+=at(n-1);
return v;
}
int qry(int l, int r) {return qry(r)-qry(l);}
};
int N, M, dp[MN], p[MN][20], s[MN], d[MN], h[MN], l[MN], t[MN], lv[MN];
std::vector<int> a[MN];
std::vector<Qry> q[MN];
bit hv[MN];
int dfs1(int n=1)
{
for(int i=0;p[n][i]&&p[p[n][i]][i];++i)
p[n][i+1]=p[p[n][i]][i];
s[n]=1;
for(int x:a[n])
if(x!=p[n][0])
d[x]=d[n]+1, p[x][0]=n, s[n]+=dfs1(x);
return s[n];
}
void dfs2(int n=1)
{
for(int x:a[n])
if(x!=p[n][0])
{
if(s[x]*2>s[n]) // >= ok too
{
h[n]=x;
if(!~t[n]) t[n]=n, l[n]=0;
t[x]=t[n], l[x]=l[n]+1;
}
dfs2(x);
}
if(~t[n] && !~h[n])
hv[t[n]].init(l[n]);
}
int mu(int a, int b)
{
for(int i=0;b;++i)
if(b>>i&1)
a=p[a][i], b^=1<<i;
return a;
}
int lca(int a, int b)
{
if(d[a]<d[b]) std::swap(a,b);
a=mu(a, d[a]-d[b]);
if(a==b) return a;
for(int i=19;i>=0;--i)
if(p[a][i]!=p[b][i])
a=p[a][i], b=p[b][i];
return p[a][0];
}
void upd(int n, int v, bool hc)
{
lv[n]+=v;
if(!hc && ~h[n])
hv[t[n]].upd(l[n], v);
}
int get(int n)
{
if(~h[n])
return dp[h[n]]+hv[t[n]].qry(l[n], l[n]+1);
else
return lv[n];
}
int qup(int n, int g) // qry up, n exclusive, g inclusive
{
int f=0;
for(;d[n]>g;)
{
if(~t[n] && t[n]!=n)
if(d[t[n]]>g) // equality shouldn't matter
f+=hv[t[n]].qry(l[n]), n=t[n];
else
return f+hv[t[n]].qry(g-d[t[n]], l[n]);
else
f+=lv[p[n][0]]-dp[n], n=p[n][0];
}
return f;
}
void dfs(int n=1)
{
for(int x:a[n])
if(x!=p[n][0])
dfs(x);
dp[n]=lv[n];
for(auto x:q[n])
{
int v=lv[n]+x.c;
for(int u:{x.a, x.b})
if(u!=n)
{
assert(get(u)==lv[u]); // get is deprecated by just using lv
v+=lv[u];
v+=qup(u, d[n]+1);
v-=dp[mu(u, d[u]-d[n]-1)];
}
ckmax(dp[n], v);
}
//printf("%d: %d\n", n, dp[n]);
if(n!=1) // root
upd(p[n][0], dp[n], h[p[n][0]]==n);
}
int main()
{
memset(h, -1, sizeof h);
memset(t, -1, sizeof t);
memset(l, -1, sizeof l);
scanf("%d", &N);
for(int i=0,u,v;i+1<N;++i)
scanf("%d%d", &u, &v), a[u].push_back(v), a[v].push_back(u);
dfs1();
dfs2();
//for(int i=1;i<=N;++i) printf("%2d: %2d %2d %2d\n", i, d[i], p[i][0], s[i]);
//for(int i=1;i<=N;++i) printf("%2d: %2d %2d %2d\n", i, h[i], t[i], l[i]);
scanf("%d", &M);
for(int i=0, a, b, c;i<M;++i)
{
scanf("%d%d%d", &a, &b, &c);
int l=lca(a,b);
//printf("lca(%d, %d) = %d\n", a, b, l);
q[l].push_back({a,b,c});
}
dfs();
printf("%d\n", dp[1]);
return 0;
}
Compilation message
election_campaign.cpp: In constructor 'bit::bit(int)':
election_campaign.cpp:22:7: warning: 'bit::sz' will be initialized after [-Wreorder]
22 | int sz;
| ^~
election_campaign.cpp:28:53: warning: base 'std::vector<int>' [-Wreorder]
28 | bit(int _sz=0): sz(_sz), std::vector<int>(_sz*4, 0) {}
| ^
election_campaign.cpp:28:3: warning: when initialized here [-Wreorder]
28 | bit(int _sz=0): sz(_sz), std::vector<int>(_sz*4, 0) {}
| ^~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:159:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
159 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:161:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
161 | scanf("%d%d", &u, &v), a[u].push_back(v), a[v].push_back(u);
| ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:166:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
166 | scanf("%d", &M);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:169:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
169 | scanf("%d%d%d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
8 ms |
9324 KB |
Output is correct |
3 |
Correct |
6 ms |
9324 KB |
Output is correct |
4 |
Correct |
7 ms |
9452 KB |
Output is correct |
5 |
Correct |
115 ms |
23532 KB |
Output is correct |
6 |
Correct |
72 ms |
35712 KB |
Output is correct |
7 |
Correct |
134 ms |
31340 KB |
Output is correct |
8 |
Correct |
85 ms |
23164 KB |
Output is correct |
9 |
Correct |
133 ms |
29100 KB |
Output is correct |
10 |
Correct |
81 ms |
23148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9324 KB |
Output is correct |
2 |
Correct |
7 ms |
9324 KB |
Output is correct |
3 |
Correct |
8 ms |
9580 KB |
Output is correct |
4 |
Correct |
158 ms |
38764 KB |
Output is correct |
5 |
Correct |
161 ms |
38764 KB |
Output is correct |
6 |
Correct |
136 ms |
38764 KB |
Output is correct |
7 |
Correct |
164 ms |
38844 KB |
Output is correct |
8 |
Correct |
155 ms |
38892 KB |
Output is correct |
9 |
Correct |
143 ms |
38892 KB |
Output is correct |
10 |
Correct |
154 ms |
38892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9324 KB |
Output is correct |
2 |
Correct |
7 ms |
9324 KB |
Output is correct |
3 |
Correct |
8 ms |
9580 KB |
Output is correct |
4 |
Correct |
158 ms |
38764 KB |
Output is correct |
5 |
Correct |
161 ms |
38764 KB |
Output is correct |
6 |
Correct |
136 ms |
38764 KB |
Output is correct |
7 |
Correct |
164 ms |
38844 KB |
Output is correct |
8 |
Correct |
155 ms |
38892 KB |
Output is correct |
9 |
Correct |
143 ms |
38892 KB |
Output is correct |
10 |
Correct |
154 ms |
38892 KB |
Output is correct |
11 |
Correct |
18 ms |
10348 KB |
Output is correct |
12 |
Correct |
158 ms |
39148 KB |
Output is correct |
13 |
Correct |
155 ms |
39148 KB |
Output is correct |
14 |
Correct |
137 ms |
39148 KB |
Output is correct |
15 |
Correct |
170 ms |
39228 KB |
Output is correct |
16 |
Correct |
138 ms |
39148 KB |
Output is correct |
17 |
Correct |
162 ms |
39020 KB |
Output is correct |
18 |
Correct |
155 ms |
39148 KB |
Output is correct |
19 |
Correct |
137 ms |
39148 KB |
Output is correct |
20 |
Correct |
158 ms |
39148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
247 ms |
26404 KB |
Output is correct |
2 |
Correct |
136 ms |
38764 KB |
Output is correct |
3 |
Correct |
307 ms |
34156 KB |
Output is correct |
4 |
Correct |
130 ms |
26020 KB |
Output is correct |
5 |
Correct |
245 ms |
33448 KB |
Output is correct |
6 |
Correct |
136 ms |
26064 KB |
Output is correct |
7 |
Correct |
309 ms |
33064 KB |
Output is correct |
8 |
Correct |
207 ms |
26732 KB |
Output is correct |
9 |
Correct |
136 ms |
39040 KB |
Output is correct |
10 |
Correct |
330 ms |
31780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
8 ms |
9324 KB |
Output is correct |
3 |
Correct |
6 ms |
9324 KB |
Output is correct |
4 |
Correct |
7 ms |
9452 KB |
Output is correct |
5 |
Correct |
115 ms |
23532 KB |
Output is correct |
6 |
Correct |
72 ms |
35712 KB |
Output is correct |
7 |
Correct |
134 ms |
31340 KB |
Output is correct |
8 |
Correct |
85 ms |
23164 KB |
Output is correct |
9 |
Correct |
133 ms |
29100 KB |
Output is correct |
10 |
Correct |
81 ms |
23148 KB |
Output is correct |
11 |
Correct |
7 ms |
9452 KB |
Output is correct |
12 |
Correct |
7 ms |
9580 KB |
Output is correct |
13 |
Correct |
7 ms |
9564 KB |
Output is correct |
14 |
Correct |
8 ms |
9580 KB |
Output is correct |
15 |
Correct |
7 ms |
9452 KB |
Output is correct |
16 |
Correct |
7 ms |
9580 KB |
Output is correct |
17 |
Correct |
7 ms |
9452 KB |
Output is correct |
18 |
Correct |
7 ms |
9580 KB |
Output is correct |
19 |
Correct |
7 ms |
9580 KB |
Output is correct |
20 |
Correct |
7 ms |
9580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
8 ms |
9324 KB |
Output is correct |
3 |
Correct |
6 ms |
9324 KB |
Output is correct |
4 |
Correct |
7 ms |
9452 KB |
Output is correct |
5 |
Correct |
115 ms |
23532 KB |
Output is correct |
6 |
Correct |
72 ms |
35712 KB |
Output is correct |
7 |
Correct |
134 ms |
31340 KB |
Output is correct |
8 |
Correct |
85 ms |
23164 KB |
Output is correct |
9 |
Correct |
133 ms |
29100 KB |
Output is correct |
10 |
Correct |
81 ms |
23148 KB |
Output is correct |
11 |
Correct |
6 ms |
9324 KB |
Output is correct |
12 |
Correct |
7 ms |
9324 KB |
Output is correct |
13 |
Correct |
8 ms |
9580 KB |
Output is correct |
14 |
Correct |
158 ms |
38764 KB |
Output is correct |
15 |
Correct |
161 ms |
38764 KB |
Output is correct |
16 |
Correct |
136 ms |
38764 KB |
Output is correct |
17 |
Correct |
164 ms |
38844 KB |
Output is correct |
18 |
Correct |
155 ms |
38892 KB |
Output is correct |
19 |
Correct |
143 ms |
38892 KB |
Output is correct |
20 |
Correct |
154 ms |
38892 KB |
Output is correct |
21 |
Correct |
18 ms |
10348 KB |
Output is correct |
22 |
Correct |
158 ms |
39148 KB |
Output is correct |
23 |
Correct |
155 ms |
39148 KB |
Output is correct |
24 |
Correct |
137 ms |
39148 KB |
Output is correct |
25 |
Correct |
170 ms |
39228 KB |
Output is correct |
26 |
Correct |
138 ms |
39148 KB |
Output is correct |
27 |
Correct |
162 ms |
39020 KB |
Output is correct |
28 |
Correct |
155 ms |
39148 KB |
Output is correct |
29 |
Correct |
137 ms |
39148 KB |
Output is correct |
30 |
Correct |
158 ms |
39148 KB |
Output is correct |
31 |
Correct |
247 ms |
26404 KB |
Output is correct |
32 |
Correct |
136 ms |
38764 KB |
Output is correct |
33 |
Correct |
307 ms |
34156 KB |
Output is correct |
34 |
Correct |
130 ms |
26020 KB |
Output is correct |
35 |
Correct |
245 ms |
33448 KB |
Output is correct |
36 |
Correct |
136 ms |
26064 KB |
Output is correct |
37 |
Correct |
309 ms |
33064 KB |
Output is correct |
38 |
Correct |
207 ms |
26732 KB |
Output is correct |
39 |
Correct |
136 ms |
39040 KB |
Output is correct |
40 |
Correct |
330 ms |
31780 KB |
Output is correct |
41 |
Correct |
7 ms |
9452 KB |
Output is correct |
42 |
Correct |
7 ms |
9580 KB |
Output is correct |
43 |
Correct |
7 ms |
9564 KB |
Output is correct |
44 |
Correct |
8 ms |
9580 KB |
Output is correct |
45 |
Correct |
7 ms |
9452 KB |
Output is correct |
46 |
Correct |
7 ms |
9580 KB |
Output is correct |
47 |
Correct |
7 ms |
9452 KB |
Output is correct |
48 |
Correct |
7 ms |
9580 KB |
Output is correct |
49 |
Correct |
7 ms |
9580 KB |
Output is correct |
50 |
Correct |
7 ms |
9580 KB |
Output is correct |
51 |
Correct |
203 ms |
26988 KB |
Output is correct |
52 |
Correct |
159 ms |
39148 KB |
Output is correct |
53 |
Correct |
314 ms |
32292 KB |
Output is correct |
54 |
Correct |
135 ms |
26280 KB |
Output is correct |
55 |
Correct |
246 ms |
26660 KB |
Output is correct |
56 |
Correct |
178 ms |
39076 KB |
Output is correct |
57 |
Correct |
242 ms |
33132 KB |
Output is correct |
58 |
Correct |
135 ms |
26276 KB |
Output is correct |
59 |
Correct |
212 ms |
26860 KB |
Output is correct |
60 |
Correct |
155 ms |
39020 KB |
Output is correct |
61 |
Correct |
242 ms |
33336 KB |
Output is correct |
62 |
Correct |
130 ms |
26056 KB |
Output is correct |
63 |
Correct |
245 ms |
26460 KB |
Output is correct |
64 |
Correct |
160 ms |
39148 KB |
Output is correct |
65 |
Correct |
332 ms |
33024 KB |
Output is correct |
66 |
Correct |
132 ms |
26280 KB |
Output is correct |
67 |
Correct |
242 ms |
26464 KB |
Output is correct |
68 |
Correct |
156 ms |
39148 KB |
Output is correct |
69 |
Correct |
241 ms |
31456 KB |
Output is correct |
70 |
Correct |
133 ms |
26320 KB |
Output is correct |