#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define SZ(x) (int)x.size()
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
#define lid (id*2)
#define rid (id*2+1)
#define mid ((l+r)/2)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 2e5 + 50;
const ll inf = 1e18;
ll n, val, res[N], h[N], timer, st[N], ft[N], tim[N];
pll par[N];
bool mark[N];
vector<pair<int, pii>> adj[N];
void dfs1(int v, int p = 0){
for(auto [u, w]: adj[v]){
if(u == p) continue;
dfs1(u, v); val += w.F;
}
}
void dfs2(int v, int p = 0){
res[1] = min(res[1], val);
for(auto [u, w]: adj[v]){
if(u == p) continue;
val += w.S - w.F;
dfs2(u, v);
val -= w.S - w.F;
}
}
void dfs3(int v, int p = 0){
for(auto [u, w]: adj[v]){
if(u == p) continue;
h[u] = h[v] + w.F;
dfs3(u, v);
}
}
void dfs(int v, int p = 0){
tim[timer] = v;
st[v] = timer++;
for(auto [u, w]: adj[v]){
if(u == p) continue;
h[u] = h[v] + w.F;
res[1] += w.F;
par[u] = {v, w.F};
dfs(u, v);
}
ft[v] = timer;
}
pll seg[N*4]; ll lz[N*4];
void build(int l, int r, int id = 1){
if(l+1 == r){
seg[id] = {h[tim[l]], tim[l]};
return;
}
build(l, mid, lid);
build(mid, r, rid);
seg[id] = max(seg[lid], seg[rid]);
}
void quex(int id, ll x){
seg[id].F += x; lz[id] += x;
}
void relax(int id){
quex(lid, lz[id]);
quex(rid, lz[id]);
lz[id] = 0;
}
void upd(int ql, int qr, ll x, int l = 0, int r = n, int id = 1){
if(ql <= l && r <= qr){
quex(id, x); return;
}
if(qr <= l || r <= ql) return;
relax(id);
upd(ql, qr, x, l, mid, lid);
upd(ql, qr, x, mid, r, rid);
seg[id] = max(seg[lid], seg[rid]);
}
pll get(int ql, int qr, int l = 0, int r = n, int id = 1){
if(ql <= l && r <= qr) return seg[id];
if(qr <= l || r <= ql) return {0, 0};
relax(id);
return max(get(ql, qr, l, mid, lid), get(ql, qr, mid, r, rid));
}
int main(){
fast_io;
cin >> n;
for(int i = 1; i < n; i++){
int a, b, c, d;
cin >> a >> b >> c >> d;
adj[a].pb({b, {c, d}});
adj[b].pb({a, {d, c}});
}
dfs3(1);
int far = 0;
for(int i = 1; i <= n; i++) if(h[far] <= h[i]) far = i;
h[far] = 0; dfs(far);
build(0, n);
mark[far] = true;
for(int i = 2; i <= n; i++){
pll mx = get(0, n);
res[i] = res[i-1] - mx.F;
int v = mx.S;
while(!mark[v]){
upd(st[v], ft[v], -par[v].S);
mark[v] = true; v = par[v].F;
}
}
dfs1(1);
res[1] = inf;
dfs2(1);
int q; cin >> q;
for(int i = 1; i <= q; i++){
int x; cin >> x;
cout << res[x] << endl;
}
}
Compilation message
designated_cities.cpp: In function 'void dfs1(int, int)':
designated_cities.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for(auto [u, w]: adj[v]){
| ^
designated_cities.cpp: In function 'void dfs2(int, int)':
designated_cities.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | for(auto [u, w]: adj[v]){
| ^
designated_cities.cpp: In function 'void dfs3(int, int)':
designated_cities.cpp:58:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [u, w]: adj[v]){
| ^
designated_cities.cpp: In function 'void dfs(int, int)':
designated_cities.cpp:68:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | for(auto [u, w]: adj[v]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
3 ms |
5108 KB |
Output is correct |
7 |
Correct |
2 ms |
5208 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5172 KB |
Output is correct |
2 |
Correct |
198 ms |
43068 KB |
Output is correct |
3 |
Correct |
208 ms |
57172 KB |
Output is correct |
4 |
Correct |
172 ms |
41832 KB |
Output is correct |
5 |
Correct |
181 ms |
43020 KB |
Output is correct |
6 |
Correct |
202 ms |
44932 KB |
Output is correct |
7 |
Correct |
168 ms |
43008 KB |
Output is correct |
8 |
Correct |
238 ms |
56148 KB |
Output is correct |
9 |
Correct |
159 ms |
43512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5212 KB |
Output is correct |
2 |
Correct |
185 ms |
43240 KB |
Output is correct |
3 |
Correct |
181 ms |
56912 KB |
Output is correct |
4 |
Correct |
201 ms |
41808 KB |
Output is correct |
5 |
Correct |
183 ms |
43244 KB |
Output is correct |
6 |
Correct |
187 ms |
45540 KB |
Output is correct |
7 |
Correct |
163 ms |
43220 KB |
Output is correct |
8 |
Correct |
239 ms |
52564 KB |
Output is correct |
9 |
Correct |
152 ms |
43112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
3 ms |
5108 KB |
Output is correct |
7 |
Correct |
2 ms |
5208 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
3 ms |
5468 KB |
Output is correct |
14 |
Correct |
3 ms |
5528 KB |
Output is correct |
15 |
Correct |
3 ms |
5436 KB |
Output is correct |
16 |
Correct |
4 ms |
5468 KB |
Output is correct |
17 |
Correct |
4 ms |
5468 KB |
Output is correct |
18 |
Correct |
4 ms |
5468 KB |
Output is correct |
19 |
Correct |
5 ms |
5724 KB |
Output is correct |
20 |
Correct |
5 ms |
5524 KB |
Output is correct |
21 |
Correct |
4 ms |
5528 KB |
Output is correct |
22 |
Correct |
3 ms |
5464 KB |
Output is correct |
23 |
Correct |
3 ms |
5468 KB |
Output is correct |
24 |
Correct |
3 ms |
5604 KB |
Output is correct |
25 |
Correct |
4 ms |
5468 KB |
Output is correct |
26 |
Correct |
3 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5172 KB |
Output is correct |
2 |
Correct |
198 ms |
43068 KB |
Output is correct |
3 |
Correct |
208 ms |
57172 KB |
Output is correct |
4 |
Correct |
172 ms |
41832 KB |
Output is correct |
5 |
Correct |
181 ms |
43020 KB |
Output is correct |
6 |
Correct |
202 ms |
44932 KB |
Output is correct |
7 |
Correct |
168 ms |
43008 KB |
Output is correct |
8 |
Correct |
238 ms |
56148 KB |
Output is correct |
9 |
Correct |
159 ms |
43512 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
185 ms |
43240 KB |
Output is correct |
12 |
Correct |
181 ms |
56912 KB |
Output is correct |
13 |
Correct |
201 ms |
41808 KB |
Output is correct |
14 |
Correct |
183 ms |
43244 KB |
Output is correct |
15 |
Correct |
187 ms |
45540 KB |
Output is correct |
16 |
Correct |
163 ms |
43220 KB |
Output is correct |
17 |
Correct |
239 ms |
52564 KB |
Output is correct |
18 |
Correct |
152 ms |
43112 KB |
Output is correct |
19 |
Correct |
3 ms |
5212 KB |
Output is correct |
20 |
Correct |
195 ms |
43092 KB |
Output is correct |
21 |
Correct |
187 ms |
56912 KB |
Output is correct |
22 |
Correct |
168 ms |
41808 KB |
Output is correct |
23 |
Correct |
183 ms |
43088 KB |
Output is correct |
24 |
Correct |
176 ms |
42064 KB |
Output is correct |
25 |
Correct |
178 ms |
43088 KB |
Output is correct |
26 |
Correct |
176 ms |
41936 KB |
Output is correct |
27 |
Correct |
173 ms |
42928 KB |
Output is correct |
28 |
Correct |
179 ms |
45136 KB |
Output is correct |
29 |
Correct |
206 ms |
43124 KB |
Output is correct |
30 |
Correct |
168 ms |
42248 KB |
Output is correct |
31 |
Correct |
158 ms |
43008 KB |
Output is correct |
32 |
Correct |
201 ms |
52896 KB |
Output is correct |
33 |
Correct |
146 ms |
43712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
3 ms |
5108 KB |
Output is correct |
7 |
Correct |
2 ms |
5208 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5172 KB |
Output is correct |
13 |
Correct |
198 ms |
43068 KB |
Output is correct |
14 |
Correct |
208 ms |
57172 KB |
Output is correct |
15 |
Correct |
172 ms |
41832 KB |
Output is correct |
16 |
Correct |
181 ms |
43020 KB |
Output is correct |
17 |
Correct |
202 ms |
44932 KB |
Output is correct |
18 |
Correct |
168 ms |
43008 KB |
Output is correct |
19 |
Correct |
238 ms |
56148 KB |
Output is correct |
20 |
Correct |
159 ms |
43512 KB |
Output is correct |
21 |
Correct |
3 ms |
5212 KB |
Output is correct |
22 |
Correct |
185 ms |
43240 KB |
Output is correct |
23 |
Correct |
181 ms |
56912 KB |
Output is correct |
24 |
Correct |
201 ms |
41808 KB |
Output is correct |
25 |
Correct |
183 ms |
43244 KB |
Output is correct |
26 |
Correct |
187 ms |
45540 KB |
Output is correct |
27 |
Correct |
163 ms |
43220 KB |
Output is correct |
28 |
Correct |
239 ms |
52564 KB |
Output is correct |
29 |
Correct |
152 ms |
43112 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
3 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
5528 KB |
Output is correct |
33 |
Correct |
3 ms |
5436 KB |
Output is correct |
34 |
Correct |
4 ms |
5468 KB |
Output is correct |
35 |
Correct |
4 ms |
5468 KB |
Output is correct |
36 |
Correct |
4 ms |
5468 KB |
Output is correct |
37 |
Correct |
5 ms |
5724 KB |
Output is correct |
38 |
Correct |
5 ms |
5524 KB |
Output is correct |
39 |
Correct |
4 ms |
5528 KB |
Output is correct |
40 |
Correct |
3 ms |
5464 KB |
Output is correct |
41 |
Correct |
3 ms |
5468 KB |
Output is correct |
42 |
Correct |
3 ms |
5604 KB |
Output is correct |
43 |
Correct |
4 ms |
5468 KB |
Output is correct |
44 |
Correct |
3 ms |
5468 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
195 ms |
43092 KB |
Output is correct |
47 |
Correct |
187 ms |
56912 KB |
Output is correct |
48 |
Correct |
168 ms |
41808 KB |
Output is correct |
49 |
Correct |
183 ms |
43088 KB |
Output is correct |
50 |
Correct |
176 ms |
42064 KB |
Output is correct |
51 |
Correct |
178 ms |
43088 KB |
Output is correct |
52 |
Correct |
176 ms |
41936 KB |
Output is correct |
53 |
Correct |
173 ms |
42928 KB |
Output is correct |
54 |
Correct |
179 ms |
45136 KB |
Output is correct |
55 |
Correct |
206 ms |
43124 KB |
Output is correct |
56 |
Correct |
168 ms |
42248 KB |
Output is correct |
57 |
Correct |
158 ms |
43008 KB |
Output is correct |
58 |
Correct |
201 ms |
52896 KB |
Output is correct |
59 |
Correct |
146 ms |
43712 KB |
Output is correct |
60 |
Correct |
2 ms |
5208 KB |
Output is correct |
61 |
Correct |
204 ms |
45716 KB |
Output is correct |
62 |
Correct |
208 ms |
58448 KB |
Output is correct |
63 |
Correct |
214 ms |
44448 KB |
Output is correct |
64 |
Correct |
229 ms |
45904 KB |
Output is correct |
65 |
Correct |
203 ms |
44320 KB |
Output is correct |
66 |
Correct |
227 ms |
45788 KB |
Output is correct |
67 |
Correct |
206 ms |
44368 KB |
Output is correct |
68 |
Correct |
252 ms |
45852 KB |
Output is correct |
69 |
Correct |
254 ms |
47952 KB |
Output is correct |
70 |
Correct |
232 ms |
45612 KB |
Output is correct |
71 |
Correct |
195 ms |
45020 KB |
Output is correct |
72 |
Correct |
184 ms |
46336 KB |
Output is correct |
73 |
Correct |
234 ms |
55068 KB |
Output is correct |
74 |
Correct |
171 ms |
47844 KB |
Output is correct |