#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], t[2];
int sz[N], par[N], dp[N], dep[N], rot[N], pos[N], n, q;
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 );
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;
for( int v : g[u] ) hld( v );
}
int query( vector<int> &t, int l, int r ) {
int ret1 = 0, ret2 = 0;
for( int i = r ; i > 0 ; i -= i & -i ) ret1 += t[i];
for( int i = l-1 ; i > 0 ; i -= i & -i ) ret2 += t[i];
return ret1 - ret2;
}
void update( vector<int> &t, int x, int va ) { for( int i = x ; i < N ; i += i & -i ) t[i] += va; }
int queryhld( int ty, int a, int b ) {
int ret = 0;
while( rot[a] != rot[b] ) {
if( dep[rot[a]] < dep[rot[b]] ) swap( a, b );
ret += query( t[ty], pos[rot[a]], pos[a] );
a = par[rot[a]];
}
if( dep[b] < dep[a] ) swap( a, b );
ret += query( t[ty], pos[a], pos[b] );
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 solve( int u, int p ) {
for( int v : g[u] ) if( v != p ) {
solve( v, u );
dp[u] += dp[v];
}
update( t[0], pos[u], dp[u] );
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 ) );
}
update( t[1], pos[u], dp[u] );
}
int main()
{
t[0].resize( 4*N ), t[1].resize( 4*N );
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:76:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
election_campaign.cpp:78: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:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
election_campaign.cpp:84: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);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
10 ms |
8192 KB |
Output is correct |
5 |
Correct |
87 ms |
13904 KB |
Output is correct |
6 |
Correct |
61 ms |
21624 KB |
Output is correct |
7 |
Correct |
91 ms |
18936 KB |
Output is correct |
8 |
Correct |
65 ms |
13944 KB |
Output is correct |
9 |
Correct |
87 ms |
17272 KB |
Output is correct |
10 |
Correct |
62 ms |
13944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
10 ms |
8320 KB |
Output is correct |
4 |
Correct |
113 ms |
23548 KB |
Output is correct |
5 |
Correct |
116 ms |
23544 KB |
Output is correct |
6 |
Correct |
117 ms |
23664 KB |
Output is correct |
7 |
Correct |
114 ms |
23608 KB |
Output is correct |
8 |
Correct |
118 ms |
23544 KB |
Output is correct |
9 |
Correct |
120 ms |
23672 KB |
Output is correct |
10 |
Correct |
118 ms |
23544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
10 ms |
8320 KB |
Output is correct |
4 |
Correct |
113 ms |
23548 KB |
Output is correct |
5 |
Correct |
116 ms |
23544 KB |
Output is correct |
6 |
Correct |
117 ms |
23664 KB |
Output is correct |
7 |
Correct |
114 ms |
23608 KB |
Output is correct |
8 |
Correct |
118 ms |
23544 KB |
Output is correct |
9 |
Correct |
120 ms |
23672 KB |
Output is correct |
10 |
Correct |
118 ms |
23544 KB |
Output is correct |
11 |
Correct |
20 ms |
8960 KB |
Output is correct |
12 |
Correct |
120 ms |
23544 KB |
Output is correct |
13 |
Correct |
124 ms |
23672 KB |
Output is correct |
14 |
Correct |
117 ms |
23672 KB |
Output is correct |
15 |
Correct |
116 ms |
23544 KB |
Output is correct |
16 |
Correct |
117 ms |
23672 KB |
Output is correct |
17 |
Correct |
121 ms |
23800 KB |
Output is correct |
18 |
Correct |
116 ms |
23568 KB |
Output is correct |
19 |
Correct |
115 ms |
23672 KB |
Output is correct |
20 |
Correct |
120 ms |
23544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
336 ms |
15408 KB |
Output is correct |
2 |
Correct |
110 ms |
23672 KB |
Output is correct |
3 |
Execution timed out |
1095 ms |
17652 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
10 ms |
8192 KB |
Output is correct |
5 |
Correct |
87 ms |
13904 KB |
Output is correct |
6 |
Correct |
61 ms |
21624 KB |
Output is correct |
7 |
Correct |
91 ms |
18936 KB |
Output is correct |
8 |
Correct |
65 ms |
13944 KB |
Output is correct |
9 |
Correct |
87 ms |
17272 KB |
Output is correct |
10 |
Correct |
62 ms |
13944 KB |
Output is correct |
11 |
Correct |
10 ms |
8320 KB |
Output is correct |
12 |
Correct |
10 ms |
8320 KB |
Output is correct |
13 |
Correct |
13 ms |
8496 KB |
Output is correct |
14 |
Correct |
11 ms |
8320 KB |
Output is correct |
15 |
Correct |
14 ms |
8320 KB |
Output is correct |
16 |
Correct |
10 ms |
8320 KB |
Output is correct |
17 |
Correct |
10 ms |
8320 KB |
Output is correct |
18 |
Correct |
12 ms |
8320 KB |
Output is correct |
19 |
Correct |
10 ms |
8320 KB |
Output is correct |
20 |
Correct |
11 ms |
8320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
10 ms |
8192 KB |
Output is correct |
5 |
Correct |
87 ms |
13904 KB |
Output is correct |
6 |
Correct |
61 ms |
21624 KB |
Output is correct |
7 |
Correct |
91 ms |
18936 KB |
Output is correct |
8 |
Correct |
65 ms |
13944 KB |
Output is correct |
9 |
Correct |
87 ms |
17272 KB |
Output is correct |
10 |
Correct |
62 ms |
13944 KB |
Output is correct |
11 |
Correct |
9 ms |
8192 KB |
Output is correct |
12 |
Correct |
9 ms |
8192 KB |
Output is correct |
13 |
Correct |
10 ms |
8320 KB |
Output is correct |
14 |
Correct |
113 ms |
23548 KB |
Output is correct |
15 |
Correct |
116 ms |
23544 KB |
Output is correct |
16 |
Correct |
117 ms |
23664 KB |
Output is correct |
17 |
Correct |
114 ms |
23608 KB |
Output is correct |
18 |
Correct |
118 ms |
23544 KB |
Output is correct |
19 |
Correct |
120 ms |
23672 KB |
Output is correct |
20 |
Correct |
118 ms |
23544 KB |
Output is correct |
21 |
Correct |
20 ms |
8960 KB |
Output is correct |
22 |
Correct |
120 ms |
23544 KB |
Output is correct |
23 |
Correct |
124 ms |
23672 KB |
Output is correct |
24 |
Correct |
117 ms |
23672 KB |
Output is correct |
25 |
Correct |
116 ms |
23544 KB |
Output is correct |
26 |
Correct |
117 ms |
23672 KB |
Output is correct |
27 |
Correct |
121 ms |
23800 KB |
Output is correct |
28 |
Correct |
116 ms |
23568 KB |
Output is correct |
29 |
Correct |
115 ms |
23672 KB |
Output is correct |
30 |
Correct |
120 ms |
23544 KB |
Output is correct |
31 |
Correct |
336 ms |
15408 KB |
Output is correct |
32 |
Correct |
110 ms |
23672 KB |
Output is correct |
33 |
Execution timed out |
1095 ms |
17652 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |