#include <bits/stdc++.h>
#define iii tuple<int, int, int>
using namespace std;
const int N = 1e5 + 10;
vector<iii> path[N];
vector<int> g[N];
int sz[N], par[N], dp[N], dep[N], rot[N], pos[N], n, q, sum[2][N], key[N];
void dfs( int u, int p ) {
if( p != 0 ) g[u].erase( find( g[u].begin(), g[u].end(), p ) );
par[u] = p, sz[u] = 1, dep[u] = dep[p] + 1;
for( int &v : g[u] ) if( v != p ) {
dfs( v, u );
sz[u] += sz[v];
if( sz[v] > sz[g[u][0]] ) swap( v, g[u][0] );
}
}
void hld( int u ) {
static int idx = 0;
if( par[u] == 0 || g[par[u]][0] != u ) rot[u] = u;
else rot[u] = rot[par[u]];
pos[u] = ++idx;
key[idx] = u;
for( int v : g[u] ) hld( v );
}
int queryhld( int ty, int a, int b ) {
int ret = 0, mi;
while( rot[a] != rot[b] ) {
if( dep[rot[a]] < dep[rot[b]] ) swap( a, b );
if( rot[key[pos[a]+1]] != rot[a] ) mi = 0;
else mi = sum[ty][pos[a]+1];
ret += sum[ty][pos[rot[a]]] - mi;
a = par[rot[a]];
}
if( dep[b] < dep[a] ) swap( a, b );
if( rot[key[pos[b]+1]] != rot[b] ) mi = 0;
else mi = sum[ty][pos[b]+1];
ret += sum[ty][pos[a]] - mi;
return ret;
}
int lca( int a, int b ) {
while( rot[a] != rot[b] ) {
if( dep[rot[a]] < dep[rot[b]] ) swap( a, b );
a = par[rot[a]];
}
if( dep[b] < dep[a] ) return b;
else return a;
}
void updt( int ty, int u, int va ) {
if( rot[key[pos[u]+1]] != rot[u] ) sum[ty][pos[u]] = va;
else sum[ty][pos[u]] = sum[ty][pos[u]+1] + va;
}
void solve( int u, int p ) {
for( int v : g[u] ) if( v != p ) {
solve( v, u );
dp[u] += dp[v];
}
updt( 0, u, dp[u] );
updt( 1, u, 0 );
for( iii x : path[u] ) {
int a, b, c;
tie( a, b, c ) = x;
dp[u] = max( dp[u], c + queryhld( 0, a, b ) - queryhld( 1, a, b ) );
}
updt( 1, u, dp[u] );
}
int main()
{
scanf("%d",&n);
for( int i = 1, a, b ; i < n ; i++ ) {
scanf("%d %d",&a,&b);
g[a].emplace_back( b ), g[b].emplace_back( a );
}
dfs( 1, 0 ), hld( 1 );
scanf("%d",&q);
for( int i = 1, a, b, c ; i <= q ; i++ ) {
scanf("%d %d %d",&a,&b,&c);
int x = lca( a, b );
path[x].emplace_back( a, b, c );
}
solve( 1, 0 );
printf("%d",dp[1]);
return 0;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
election_campaign.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&a,&b);
~~~~~^~~~~~~~~~~~~~~
election_campaign.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
election_campaign.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&a,&b,&c);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
5096 KB |
Output is correct |
5 |
Correct |
82 ms |
12664 KB |
Output is correct |
6 |
Correct |
57 ms |
21880 KB |
Output is correct |
7 |
Correct |
69 ms |
18552 KB |
Output is correct |
8 |
Correct |
60 ms |
12664 KB |
Output is correct |
9 |
Correct |
67 ms |
16764 KB |
Output is correct |
10 |
Correct |
56 ms |
12536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
104 ms |
23800 KB |
Output is correct |
5 |
Correct |
107 ms |
23928 KB |
Output is correct |
6 |
Correct |
103 ms |
23928 KB |
Output is correct |
7 |
Correct |
103 ms |
23800 KB |
Output is correct |
8 |
Correct |
103 ms |
23800 KB |
Output is correct |
9 |
Correct |
105 ms |
23800 KB |
Output is correct |
10 |
Correct |
104 ms |
23844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
104 ms |
23800 KB |
Output is correct |
5 |
Correct |
107 ms |
23928 KB |
Output is correct |
6 |
Correct |
103 ms |
23928 KB |
Output is correct |
7 |
Correct |
103 ms |
23800 KB |
Output is correct |
8 |
Correct |
103 ms |
23800 KB |
Output is correct |
9 |
Correct |
105 ms |
23800 KB |
Output is correct |
10 |
Correct |
104 ms |
23844 KB |
Output is correct |
11 |
Correct |
17 ms |
6016 KB |
Output is correct |
12 |
Correct |
109 ms |
23776 KB |
Output is correct |
13 |
Correct |
107 ms |
23928 KB |
Output is correct |
14 |
Correct |
108 ms |
23804 KB |
Output is correct |
15 |
Correct |
111 ms |
23932 KB |
Output is correct |
16 |
Correct |
105 ms |
23800 KB |
Output is correct |
17 |
Correct |
106 ms |
23800 KB |
Output is correct |
18 |
Correct |
106 ms |
23800 KB |
Output is correct |
19 |
Correct |
111 ms |
24056 KB |
Output is correct |
20 |
Correct |
113 ms |
23928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
14128 KB |
Output is correct |
2 |
Correct |
114 ms |
23800 KB |
Output is correct |
3 |
Correct |
143 ms |
20856 KB |
Output is correct |
4 |
Correct |
104 ms |
14000 KB |
Output is correct |
5 |
Correct |
129 ms |
19480 KB |
Output is correct |
6 |
Correct |
103 ms |
13932 KB |
Output is correct |
7 |
Correct |
134 ms |
19124 KB |
Output is correct |
8 |
Correct |
158 ms |
14328 KB |
Output is correct |
9 |
Correct |
102 ms |
23672 KB |
Output is correct |
10 |
Correct |
127 ms |
17968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
5096 KB |
Output is correct |
5 |
Correct |
82 ms |
12664 KB |
Output is correct |
6 |
Correct |
57 ms |
21880 KB |
Output is correct |
7 |
Correct |
69 ms |
18552 KB |
Output is correct |
8 |
Correct |
60 ms |
12664 KB |
Output is correct |
9 |
Correct |
67 ms |
16764 KB |
Output is correct |
10 |
Correct |
56 ms |
12536 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5248 KB |
Output is correct |
14 |
Correct |
9 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
8 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
5096 KB |
Output is correct |
5 |
Correct |
82 ms |
12664 KB |
Output is correct |
6 |
Correct |
57 ms |
21880 KB |
Output is correct |
7 |
Correct |
69 ms |
18552 KB |
Output is correct |
8 |
Correct |
60 ms |
12664 KB |
Output is correct |
9 |
Correct |
67 ms |
16764 KB |
Output is correct |
10 |
Correct |
56 ms |
12536 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5248 KB |
Output is correct |
14 |
Correct |
104 ms |
23800 KB |
Output is correct |
15 |
Correct |
107 ms |
23928 KB |
Output is correct |
16 |
Correct |
103 ms |
23928 KB |
Output is correct |
17 |
Correct |
103 ms |
23800 KB |
Output is correct |
18 |
Correct |
103 ms |
23800 KB |
Output is correct |
19 |
Correct |
105 ms |
23800 KB |
Output is correct |
20 |
Correct |
104 ms |
23844 KB |
Output is correct |
21 |
Correct |
17 ms |
6016 KB |
Output is correct |
22 |
Correct |
109 ms |
23776 KB |
Output is correct |
23 |
Correct |
107 ms |
23928 KB |
Output is correct |
24 |
Correct |
108 ms |
23804 KB |
Output is correct |
25 |
Correct |
111 ms |
23932 KB |
Output is correct |
26 |
Correct |
105 ms |
23800 KB |
Output is correct |
27 |
Correct |
106 ms |
23800 KB |
Output is correct |
28 |
Correct |
106 ms |
23800 KB |
Output is correct |
29 |
Correct |
111 ms |
24056 KB |
Output is correct |
30 |
Correct |
113 ms |
23928 KB |
Output is correct |
31 |
Correct |
172 ms |
14128 KB |
Output is correct |
32 |
Correct |
114 ms |
23800 KB |
Output is correct |
33 |
Correct |
143 ms |
20856 KB |
Output is correct |
34 |
Correct |
104 ms |
14000 KB |
Output is correct |
35 |
Correct |
129 ms |
19480 KB |
Output is correct |
36 |
Correct |
103 ms |
13932 KB |
Output is correct |
37 |
Correct |
134 ms |
19124 KB |
Output is correct |
38 |
Correct |
158 ms |
14328 KB |
Output is correct |
39 |
Correct |
102 ms |
23672 KB |
Output is correct |
40 |
Correct |
127 ms |
17968 KB |
Output is correct |
41 |
Correct |
8 ms |
5120 KB |
Output is correct |
42 |
Correct |
8 ms |
5248 KB |
Output is correct |
43 |
Correct |
8 ms |
5248 KB |
Output is correct |
44 |
Correct |
9 ms |
5248 KB |
Output is correct |
45 |
Correct |
8 ms |
5120 KB |
Output is correct |
46 |
Correct |
8 ms |
5248 KB |
Output is correct |
47 |
Correct |
8 ms |
5248 KB |
Output is correct |
48 |
Correct |
8 ms |
5248 KB |
Output is correct |
49 |
Correct |
8 ms |
5248 KB |
Output is correct |
50 |
Correct |
8 ms |
5248 KB |
Output is correct |
51 |
Correct |
162 ms |
14204 KB |
Output is correct |
52 |
Correct |
107 ms |
23672 KB |
Output is correct |
53 |
Correct |
137 ms |
18096 KB |
Output is correct |
54 |
Correct |
115 ms |
14004 KB |
Output is correct |
55 |
Correct |
167 ms |
14000 KB |
Output is correct |
56 |
Correct |
106 ms |
23928 KB |
Output is correct |
57 |
Correct |
131 ms |
18816 KB |
Output is correct |
58 |
Correct |
105 ms |
13872 KB |
Output is correct |
59 |
Correct |
156 ms |
14200 KB |
Output is correct |
60 |
Correct |
114 ms |
23956 KB |
Output is correct |
61 |
Correct |
135 ms |
19064 KB |
Output is correct |
62 |
Correct |
107 ms |
13780 KB |
Output is correct |
63 |
Correct |
167 ms |
13792 KB |
Output is correct |
64 |
Correct |
115 ms |
23800 KB |
Output is correct |
65 |
Correct |
132 ms |
18868 KB |
Output is correct |
66 |
Correct |
105 ms |
14004 KB |
Output is correct |
67 |
Correct |
167 ms |
13932 KB |
Output is correct |
68 |
Correct |
107 ms |
23672 KB |
Output is correct |
69 |
Correct |
131 ms |
17656 KB |
Output is correct |
70 |
Correct |
105 ms |
14172 KB |
Output is correct |