#include<bits/stdc++.h>
using namespace std;
using ld = long double;
#define int long long
#define pii pair<int,int>
#define forn(i,j) for(int i = 0; i < j; ++i)
#define forrange(i,j,k) for(int i = j; i < k; ++i)
#define vi vector<int>
#define vpii vector<pii>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
const int MOD = 1e9+7; const int INF = 1e17+1; const int maxN = 2e5+1;
void setIO(string name){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(!name.empty()){
freopen((name + ".in").c_str(),"r",stdin);
freopen((name + ".out").c_str(),"w",stdout);
}
}
struct Query{
int a,b,c;
};
int bit[maxN];
void update(int idx, int val){
while(idx < maxN){
bit[idx] += val;
idx += idx&-idx;
}
}
int sum(int idx){
int res = 0;
while(idx > 0){
res += bit[idx];
idx -= idx&-idx;
}
return res;
}
int dp[maxN][2],tin[maxN],tout[maxN],blift[maxN][20],timer = 0;
vi graph[maxN];
vector<Query> Q[maxN];
void dfs(int src, int prev){
tin[src] = ++timer;
for(int ch : graph[src]){
if(ch == prev) continue;
blift[ch][0] = src;
dfs(ch,src);
}
tout[src] = timer;
}
void dfs2(int src, int prev){
for(int ch : graph[src]){
if(ch == prev) continue;
dfs2(ch,src);
dp[src][0] += dp[ch][1];
}
dp[src][1] = dp[src][0];
for(auto [a,b,cost] : Q[src]){
dp[src][1] = max(dp[src][1],dp[src][0] + sum(tin[a]) + sum(tin[b]) + cost);
}
update(tin[src],dp[src][0]-dp[src][1]);
update(tout[src]+1,dp[src][1]-dp[src][0]);
}
bool ancestor(int a, int b){
return ((tin[a] <= tin[b]) && (tout[a] >= tout[b]));
}
int lca(int a, int b){
for(int j = 19; j >= 0; --j){
if(!ancestor(blift[a][j],b)){
a = blift[a][j];
}
}
if(ancestor(a,b)) return a;
return blift[a][0];
}
signed main(){
setIO("");
int n; cin >> n;
tout[0] = INF;
forn(i,n-1){
int a,b; cin >> a >> b;
graph[a].pb(b); graph[b].pb(a);
}
dfs(1,-1);
forrange(j,1,20){
forrange(i,1,n+1){
blift[i][j] = blift[blift[i][j-1]][j-1];
}
}
int m; cin >> m;
while(m--){
int a,b,c; cin >> a >> b >> c;
Q[lca(a,b)].pb({a,b,c});
}
dfs2(1,-1);
cout << dp[1][1] << endl;
}
Compilation message
election_campaign.cpp: In function 'void dfs2(long long int, long long int)':
election_campaign.cpp:79:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
79 | for(auto [a,b,cost] : Q[src]){
| ^
election_campaign.cpp: In function 'void setIO(std::string)':
election_campaign.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen((name + ".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen((name + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16996 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
65 ms |
36688 KB |
Output is correct |
6 |
Correct |
47 ms |
43604 KB |
Output is correct |
7 |
Correct |
75 ms |
43168 KB |
Output is correct |
8 |
Correct |
56 ms |
36688 KB |
Output is correct |
9 |
Correct |
66 ms |
39684 KB |
Output is correct |
10 |
Correct |
56 ms |
36580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
17244 KB |
Output is correct |
3 |
Correct |
4 ms |
16984 KB |
Output is correct |
4 |
Correct |
121 ms |
47936 KB |
Output is correct |
5 |
Correct |
102 ms |
47824 KB |
Output is correct |
6 |
Correct |
104 ms |
47928 KB |
Output is correct |
7 |
Correct |
108 ms |
47796 KB |
Output is correct |
8 |
Correct |
112 ms |
48056 KB |
Output is correct |
9 |
Correct |
105 ms |
47888 KB |
Output is correct |
10 |
Correct |
119 ms |
48064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
17244 KB |
Output is correct |
3 |
Correct |
4 ms |
16984 KB |
Output is correct |
4 |
Correct |
121 ms |
47936 KB |
Output is correct |
5 |
Correct |
102 ms |
47824 KB |
Output is correct |
6 |
Correct |
104 ms |
47928 KB |
Output is correct |
7 |
Correct |
108 ms |
47796 KB |
Output is correct |
8 |
Correct |
112 ms |
48056 KB |
Output is correct |
9 |
Correct |
105 ms |
47888 KB |
Output is correct |
10 |
Correct |
119 ms |
48064 KB |
Output is correct |
11 |
Correct |
12 ms |
18268 KB |
Output is correct |
12 |
Correct |
122 ms |
47876 KB |
Output is correct |
13 |
Correct |
108 ms |
47864 KB |
Output is correct |
14 |
Correct |
112 ms |
47920 KB |
Output is correct |
15 |
Correct |
116 ms |
47924 KB |
Output is correct |
16 |
Correct |
102 ms |
47824 KB |
Output is correct |
17 |
Correct |
105 ms |
47852 KB |
Output is correct |
18 |
Correct |
110 ms |
47780 KB |
Output is correct |
19 |
Correct |
107 ms |
48004 KB |
Output is correct |
20 |
Correct |
115 ms |
47864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
40456 KB |
Output is correct |
2 |
Correct |
104 ms |
47840 KB |
Output is correct |
3 |
Correct |
173 ms |
45240 KB |
Output is correct |
4 |
Correct |
121 ms |
42556 KB |
Output is correct |
5 |
Correct |
138 ms |
44432 KB |
Output is correct |
6 |
Correct |
82 ms |
40380 KB |
Output is correct |
7 |
Correct |
166 ms |
44344 KB |
Output is correct |
8 |
Correct |
144 ms |
40272 KB |
Output is correct |
9 |
Correct |
107 ms |
47996 KB |
Output is correct |
10 |
Correct |
148 ms |
43196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16996 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
65 ms |
36688 KB |
Output is correct |
6 |
Correct |
47 ms |
43604 KB |
Output is correct |
7 |
Correct |
75 ms |
43168 KB |
Output is correct |
8 |
Correct |
56 ms |
36688 KB |
Output is correct |
9 |
Correct |
66 ms |
39684 KB |
Output is correct |
10 |
Correct |
56 ms |
36580 KB |
Output is correct |
11 |
Correct |
4 ms |
16988 KB |
Output is correct |
12 |
Correct |
4 ms |
17244 KB |
Output is correct |
13 |
Correct |
5 ms |
16992 KB |
Output is correct |
14 |
Correct |
4 ms |
16984 KB |
Output is correct |
15 |
Correct |
6 ms |
17140 KB |
Output is correct |
16 |
Correct |
4 ms |
16988 KB |
Output is correct |
17 |
Correct |
5 ms |
16996 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
5 ms |
16988 KB |
Output is correct |
20 |
Correct |
5 ms |
17168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16996 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
65 ms |
36688 KB |
Output is correct |
6 |
Correct |
47 ms |
43604 KB |
Output is correct |
7 |
Correct |
75 ms |
43168 KB |
Output is correct |
8 |
Correct |
56 ms |
36688 KB |
Output is correct |
9 |
Correct |
66 ms |
39684 KB |
Output is correct |
10 |
Correct |
56 ms |
36580 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
4 ms |
17244 KB |
Output is correct |
13 |
Correct |
4 ms |
16984 KB |
Output is correct |
14 |
Correct |
121 ms |
47936 KB |
Output is correct |
15 |
Correct |
102 ms |
47824 KB |
Output is correct |
16 |
Correct |
104 ms |
47928 KB |
Output is correct |
17 |
Correct |
108 ms |
47796 KB |
Output is correct |
18 |
Correct |
112 ms |
48056 KB |
Output is correct |
19 |
Correct |
105 ms |
47888 KB |
Output is correct |
20 |
Correct |
119 ms |
48064 KB |
Output is correct |
21 |
Correct |
12 ms |
18268 KB |
Output is correct |
22 |
Correct |
122 ms |
47876 KB |
Output is correct |
23 |
Correct |
108 ms |
47864 KB |
Output is correct |
24 |
Correct |
112 ms |
47920 KB |
Output is correct |
25 |
Correct |
116 ms |
47924 KB |
Output is correct |
26 |
Correct |
102 ms |
47824 KB |
Output is correct |
27 |
Correct |
105 ms |
47852 KB |
Output is correct |
28 |
Correct |
110 ms |
47780 KB |
Output is correct |
29 |
Correct |
107 ms |
48004 KB |
Output is correct |
30 |
Correct |
115 ms |
47864 KB |
Output is correct |
31 |
Correct |
102 ms |
40456 KB |
Output is correct |
32 |
Correct |
104 ms |
47840 KB |
Output is correct |
33 |
Correct |
173 ms |
45240 KB |
Output is correct |
34 |
Correct |
121 ms |
42556 KB |
Output is correct |
35 |
Correct |
138 ms |
44432 KB |
Output is correct |
36 |
Correct |
82 ms |
40380 KB |
Output is correct |
37 |
Correct |
166 ms |
44344 KB |
Output is correct |
38 |
Correct |
144 ms |
40272 KB |
Output is correct |
39 |
Correct |
107 ms |
47996 KB |
Output is correct |
40 |
Correct |
148 ms |
43196 KB |
Output is correct |
41 |
Correct |
4 ms |
16988 KB |
Output is correct |
42 |
Correct |
4 ms |
17244 KB |
Output is correct |
43 |
Correct |
5 ms |
16992 KB |
Output is correct |
44 |
Correct |
4 ms |
16984 KB |
Output is correct |
45 |
Correct |
6 ms |
17140 KB |
Output is correct |
46 |
Correct |
4 ms |
16988 KB |
Output is correct |
47 |
Correct |
5 ms |
16996 KB |
Output is correct |
48 |
Correct |
4 ms |
16988 KB |
Output is correct |
49 |
Correct |
5 ms |
16988 KB |
Output is correct |
50 |
Correct |
5 ms |
17168 KB |
Output is correct |
51 |
Correct |
125 ms |
40216 KB |
Output is correct |
52 |
Correct |
110 ms |
47812 KB |
Output is correct |
53 |
Correct |
183 ms |
43384 KB |
Output is correct |
54 |
Correct |
95 ms |
40540 KB |
Output is correct |
55 |
Correct |
122 ms |
40272 KB |
Output is correct |
56 |
Correct |
132 ms |
48028 KB |
Output is correct |
57 |
Correct |
169 ms |
44096 KB |
Output is correct |
58 |
Correct |
87 ms |
40544 KB |
Output is correct |
59 |
Correct |
124 ms |
42124 KB |
Output is correct |
60 |
Correct |
112 ms |
47816 KB |
Output is correct |
61 |
Correct |
159 ms |
44172 KB |
Output is correct |
62 |
Correct |
94 ms |
40060 KB |
Output is correct |
63 |
Correct |
109 ms |
40072 KB |
Output is correct |
64 |
Correct |
127 ms |
47940 KB |
Output is correct |
65 |
Correct |
143 ms |
44008 KB |
Output is correct |
66 |
Correct |
90 ms |
40396 KB |
Output is correct |
67 |
Correct |
120 ms |
40252 KB |
Output is correct |
68 |
Correct |
112 ms |
47848 KB |
Output is correct |
69 |
Correct |
141 ms |
42896 KB |
Output is correct |
70 |
Correct |
102 ms |
40440 KB |
Output is correct |