#include<bits/stdc++.h>
using namespace std;
struct AIB{
vector<int> aib;
AIB(int n): aib(n + 1, 0){}
void update(int poz, int val){
for(int i = poz; i < aib.size(); i+= i &(-i))
aib[i] += val;
}
int query(int poz){
int ans = 0;
for(int i = poz; i > 0; i-= i&(-i))
ans += aib[i];
return ans;
}
int getonint(int l, int r){
if(l > r)
return 0;
return query(r) - query(l - 1);
}
};
struct pathlca{
int x, y;
int c;
};
const int N = 100005;
const int LOG_N = 17;
vector<int> gr[N];
vector<pathlca> path[N];
int dp[N];
int dpson[N];
AIB sdp(2*N);
AIB sdpson(2*N);
int first[N], last[N];
int par[LOG_N][N];
int h[N];
void dfs_prec(int nod, int dad, int &cnt){
h[nod] = h[dad] + 1;
par[0][nod] = dad;
first[nod] = ++cnt;
for(auto x:gr[nod]){
if(x != dad)
dfs_prec(x, nod, cnt);
}
last[nod] = ++cnt;
}
void prec_lca(int n){
int aux = 0;
dfs_prec(1, 0, aux);
for(int log = 1; log < LOG_N; log++){
for(int i = 1; i <= n; i++){
par[log][i] = par[log - 1][par[log - 1][i]];
}
}
}
bool is_parent(int x, int y){
if(first[x] <= first[y] && last[y] <= last[x])
return true;
return false;
}
int get_lca(int x, int y){
if(h[x] > h[y])
swap(x, y);
if(is_parent(x, y))
return x;
for(int pas = LOG_N - 1; pas >= 0; pas--){
int old_dad = par[pas][x];
if(old_dad !=0 && is_parent(old_dad, y) == false)
x = old_dad;
}
return par[0][x];
}
int getsumpath(int x, int y, int p, AIB &sums){
int sum = sums.getonint(first[p], first[x]) + sums.getonint(first[p] + 1, first[y]);
return sum;
}
void addtonode(int nod, int val, AIB &sums){
if(nod == 0)
return;
sums.update(first[nod], val);
sums.update(last[nod], -val);
}
void dfs(int nod, int dad){
int dpnod = 0;
for(auto x:gr[nod])
if(x!=dad){
dfs(x, nod);
}
dpnod = dpson[nod];
for(auto tri:path[nod]){
int x = tri.x, y = tri.y, c = tri.c;
int sum = getsumpath(x, y, nod, sdp);
int sumson = getsumpath(x, y, nod, sdpson);
dpnod = max(dpnod, sumson - sum + c);
}
dp[nod] = dpnod;
addtonode(nod, dpnod, sdp);
dpson[par[0][nod]] += dpnod;
addtonode(par[0][nod], dpnod, sdpson);
}
int main()
{
//freopen(".in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin>>n;
for(int i = 1; i < n; i++){
int x, y;
cin>>x>>y;
gr[x].push_back(y);
gr[y].push_back(x);
}
int m;
prec_lca(n);
cin>>m;
for(int i = 1; i <=m; i++){
int x, y, c;
cin>>x>>y>>c;
int lca = get_lca(x, y);
path[lca].push_back({x, y, c});
}
dfs(1, 0);
cout<<dp[1];
return 0;
}
Compilation message
election_campaign.cpp: In member function 'void AIB::update(int, int)':
election_campaign.cpp:7:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | for(int i = poz; i < aib.size(); i+= i &(-i))
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6732 KB |
Output is correct |
5 |
Correct |
94 ms |
19556 KB |
Output is correct |
6 |
Correct |
55 ms |
28828 KB |
Output is correct |
7 |
Correct |
78 ms |
25564 KB |
Output is correct |
8 |
Correct |
72 ms |
19528 KB |
Output is correct |
9 |
Correct |
80 ms |
23712 KB |
Output is correct |
10 |
Correct |
76 ms |
19576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6860 KB |
Output is correct |
4 |
Correct |
105 ms |
29712 KB |
Output is correct |
5 |
Correct |
102 ms |
29712 KB |
Output is correct |
6 |
Correct |
104 ms |
29772 KB |
Output is correct |
7 |
Correct |
105 ms |
29692 KB |
Output is correct |
8 |
Correct |
103 ms |
29668 KB |
Output is correct |
9 |
Correct |
111 ms |
29636 KB |
Output is correct |
10 |
Correct |
106 ms |
29716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6860 KB |
Output is correct |
4 |
Correct |
105 ms |
29712 KB |
Output is correct |
5 |
Correct |
102 ms |
29712 KB |
Output is correct |
6 |
Correct |
104 ms |
29772 KB |
Output is correct |
7 |
Correct |
105 ms |
29692 KB |
Output is correct |
8 |
Correct |
103 ms |
29668 KB |
Output is correct |
9 |
Correct |
111 ms |
29636 KB |
Output is correct |
10 |
Correct |
106 ms |
29716 KB |
Output is correct |
11 |
Correct |
13 ms |
7400 KB |
Output is correct |
12 |
Correct |
109 ms |
29636 KB |
Output is correct |
13 |
Correct |
109 ms |
29672 KB |
Output is correct |
14 |
Correct |
104 ms |
29664 KB |
Output is correct |
15 |
Correct |
116 ms |
29720 KB |
Output is correct |
16 |
Correct |
110 ms |
29636 KB |
Output is correct |
17 |
Correct |
108 ms |
29624 KB |
Output is correct |
18 |
Correct |
107 ms |
29652 KB |
Output is correct |
19 |
Correct |
108 ms |
29684 KB |
Output is correct |
20 |
Correct |
105 ms |
29628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
19836 KB |
Output is correct |
2 |
Correct |
104 ms |
32148 KB |
Output is correct |
3 |
Correct |
171 ms |
28488 KB |
Output is correct |
4 |
Correct |
126 ms |
22524 KB |
Output is correct |
5 |
Correct |
146 ms |
27856 KB |
Output is correct |
6 |
Correct |
125 ms |
22392 KB |
Output is correct |
7 |
Correct |
167 ms |
27660 KB |
Output is correct |
8 |
Correct |
155 ms |
22588 KB |
Output is correct |
9 |
Correct |
105 ms |
32068 KB |
Output is correct |
10 |
Correct |
163 ms |
26424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6732 KB |
Output is correct |
5 |
Correct |
94 ms |
19556 KB |
Output is correct |
6 |
Correct |
55 ms |
28828 KB |
Output is correct |
7 |
Correct |
78 ms |
25564 KB |
Output is correct |
8 |
Correct |
72 ms |
19528 KB |
Output is correct |
9 |
Correct |
80 ms |
23712 KB |
Output is correct |
10 |
Correct |
76 ms |
19576 KB |
Output is correct |
11 |
Correct |
5 ms |
6732 KB |
Output is correct |
12 |
Correct |
6 ms |
6860 KB |
Output is correct |
13 |
Correct |
5 ms |
6860 KB |
Output is correct |
14 |
Correct |
4 ms |
6860 KB |
Output is correct |
15 |
Correct |
5 ms |
6732 KB |
Output is correct |
16 |
Correct |
5 ms |
6872 KB |
Output is correct |
17 |
Correct |
5 ms |
6860 KB |
Output is correct |
18 |
Correct |
5 ms |
6860 KB |
Output is correct |
19 |
Correct |
5 ms |
6860 KB |
Output is correct |
20 |
Correct |
5 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6732 KB |
Output is correct |
5 |
Correct |
94 ms |
19556 KB |
Output is correct |
6 |
Correct |
55 ms |
28828 KB |
Output is correct |
7 |
Correct |
78 ms |
25564 KB |
Output is correct |
8 |
Correct |
72 ms |
19528 KB |
Output is correct |
9 |
Correct |
80 ms |
23712 KB |
Output is correct |
10 |
Correct |
76 ms |
19576 KB |
Output is correct |
11 |
Correct |
4 ms |
6604 KB |
Output is correct |
12 |
Correct |
4 ms |
6604 KB |
Output is correct |
13 |
Correct |
4 ms |
6860 KB |
Output is correct |
14 |
Correct |
105 ms |
29712 KB |
Output is correct |
15 |
Correct |
102 ms |
29712 KB |
Output is correct |
16 |
Correct |
104 ms |
29772 KB |
Output is correct |
17 |
Correct |
105 ms |
29692 KB |
Output is correct |
18 |
Correct |
103 ms |
29668 KB |
Output is correct |
19 |
Correct |
111 ms |
29636 KB |
Output is correct |
20 |
Correct |
106 ms |
29716 KB |
Output is correct |
21 |
Correct |
13 ms |
7400 KB |
Output is correct |
22 |
Correct |
109 ms |
29636 KB |
Output is correct |
23 |
Correct |
109 ms |
29672 KB |
Output is correct |
24 |
Correct |
104 ms |
29664 KB |
Output is correct |
25 |
Correct |
116 ms |
29720 KB |
Output is correct |
26 |
Correct |
110 ms |
29636 KB |
Output is correct |
27 |
Correct |
108 ms |
29624 KB |
Output is correct |
28 |
Correct |
107 ms |
29652 KB |
Output is correct |
29 |
Correct |
108 ms |
29684 KB |
Output is correct |
30 |
Correct |
105 ms |
29628 KB |
Output is correct |
31 |
Correct |
156 ms |
19836 KB |
Output is correct |
32 |
Correct |
104 ms |
32148 KB |
Output is correct |
33 |
Correct |
171 ms |
28488 KB |
Output is correct |
34 |
Correct |
126 ms |
22524 KB |
Output is correct |
35 |
Correct |
146 ms |
27856 KB |
Output is correct |
36 |
Correct |
125 ms |
22392 KB |
Output is correct |
37 |
Correct |
167 ms |
27660 KB |
Output is correct |
38 |
Correct |
155 ms |
22588 KB |
Output is correct |
39 |
Correct |
105 ms |
32068 KB |
Output is correct |
40 |
Correct |
163 ms |
26424 KB |
Output is correct |
41 |
Correct |
5 ms |
6732 KB |
Output is correct |
42 |
Correct |
6 ms |
6860 KB |
Output is correct |
43 |
Correct |
5 ms |
6860 KB |
Output is correct |
44 |
Correct |
4 ms |
6860 KB |
Output is correct |
45 |
Correct |
5 ms |
6732 KB |
Output is correct |
46 |
Correct |
5 ms |
6872 KB |
Output is correct |
47 |
Correct |
5 ms |
6860 KB |
Output is correct |
48 |
Correct |
5 ms |
6860 KB |
Output is correct |
49 |
Correct |
5 ms |
6860 KB |
Output is correct |
50 |
Correct |
5 ms |
6860 KB |
Output is correct |
51 |
Correct |
150 ms |
23080 KB |
Output is correct |
52 |
Correct |
107 ms |
32408 KB |
Output is correct |
53 |
Correct |
166 ms |
26820 KB |
Output is correct |
54 |
Correct |
123 ms |
22796 KB |
Output is correct |
55 |
Correct |
151 ms |
22652 KB |
Output is correct |
56 |
Correct |
111 ms |
32464 KB |
Output is correct |
57 |
Correct |
156 ms |
27604 KB |
Output is correct |
58 |
Correct |
129 ms |
22652 KB |
Output is correct |
59 |
Correct |
160 ms |
22964 KB |
Output is correct |
60 |
Correct |
109 ms |
32472 KB |
Output is correct |
61 |
Correct |
150 ms |
27716 KB |
Output is correct |
62 |
Correct |
126 ms |
22436 KB |
Output is correct |
63 |
Correct |
155 ms |
22440 KB |
Output is correct |
64 |
Correct |
114 ms |
32444 KB |
Output is correct |
65 |
Correct |
164 ms |
27580 KB |
Output is correct |
66 |
Correct |
118 ms |
22656 KB |
Output is correct |
67 |
Correct |
151 ms |
22528 KB |
Output is correct |
68 |
Correct |
110 ms |
32400 KB |
Output is correct |
69 |
Correct |
154 ms |
26308 KB |
Output is correct |
70 |
Correct |
128 ms |
22876 KB |
Output is correct |