#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
using namespace std;
using pii = pair<int, int>;
using ll = long long;
const int MAXN = 2e5+1;
const ll INF = 1e18+1;
int n, a[MAXN], b[MAXN], c[MAXN], d[MAXN];
ll total;
vector<pii> g[MAXN];
ll ans[MAXN];
ll cur_score, cur_val[MAXN];
void solve1(int x, int p) {
ans[1] = max(ans[1], cur_score);
for(auto& [i, w]: g[x]) {
if(i!=p) {
ll _score = cur_score, _val = cur_val[i];
cur_score += w-cur_val[i];
solve1(i, x);
cur_score = _score;
cur_val[i] = _val;
cur_val[x] = 0;
}
}
}
pair<ll, int> get_furthest(int x, int p, ll dist) {
pair<ll, int> res = mp(dist, x);
for(auto& [i, w]: g[x]) {
if(i!=p) {
res = max(res, get_furthest(i, x, dist+w));
}
}
return res;
}
ll get_score(int x, int p) {
ll res = 0;
cur_val[x] = 0;
for(auto& [i, w]: g[x]) {
if(i!=p) {
res += get_score(i, x);
}
else {
cur_val[x] = w;
res += w;
}
}
return res;
}
int par[MAXN], parw[MAXN], pre[MAXN], range[MAXN], nr;
ll val[MAXN];
bool vis[MAXN];
void dfs(int x, int p, ll dist) {
par[x] = p;
vis[x] = 0;
pre[x] = range[x] = ++nr;
val[x] = dist;
for(auto& [i, w]: g[x]) {
if(i!=p) {
parw[i] = w;
dfs(i, x, dist + w);
range[x] = max(range[x], range[i]);
}
}
}
const int base = (1<<18);
pair<ll, int> tree[2*base+1];
ll push[2*base+1];
void upd(int x, ll v) {
tree[x].st += v;
push[x] += v;
}
void ins(int l, int r, ll v, int id=1, int rl=1, int rr=base) {
if(l > rr || r < rl) return;
if(l<=rl && r>=rr) {
upd(id, v);
return;
}
upd(id*2, push[id]), upd(id*2+1, push[id]);
push[id] = 0;
ins(l, r, v, id*2, rl, (rl+rr)/2), ins(l, r, v, id*2+1, (rl+rr)/2+1, rr);
tree[id] = max(tree[id*2], tree[id*2+1]);
}
int get_best() {
return tree[1].nd;
}
void build() {
for(int i=1; i<=n; ++i) {
tree[pre[i]+base-1] = mp(val[i], i);
}
for(int i=base-1; i>=1; --i) {
tree[i] = max(tree[i*2], tree[i*2+1]);
}
}
void solve(int root) {
nr = 0;
dfs(root, root, 0);
build();
vector<ll> res(n+1);
res[1] = get_score(root, root);
vis[root] = 1;
for(int i=2; i<=n; ++i) {
res[i] = res[i-1];
int cur = get_best();
while(!vis[cur]) {
vis[cur] = 1;
res[i] += parw[cur];
ins(pre[cur], range[cur], -parw[cur]);
cur = par[cur];
}
}
for(int i=1; i<=n; ++i) {
ans[i] = max(ans[i], res[i]);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
for(int i=0; i<n-1; ++i) {
cin>>a[i]>>b[i]>>c[i]>>d[i];
g[a[i]].push_back(mp(b[i], c[i]));
g[b[i]].push_back(mp(a[i], d[i]));
total += c[i]+d[i];
}
cur_score = get_score(1, 1);
solve1(1, 1);
int root = get_furthest(1, 1, 0).nd;
solve(root);
root = get_furthest(root, root, 0).nd;
solve(root);
int q; cin>>q;
for(int i=0; i<q; ++i) {
int e; cin>>e;
cout<<total - ans[e]<<'\n';
}
}
Compilation message
designated_cities.cpp: In function 'void solve1(int, int)':
designated_cities.cpp:18:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for(auto& [i, w]: g[x]) {
| ^
designated_cities.cpp: In function 'std::pair<long long int, int> get_furthest(int, int, ll)':
designated_cities.cpp:32:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for(auto& [i, w]: g[x]) {
| ^
designated_cities.cpp: In function 'll get_score(int, int)':
designated_cities.cpp:42:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | for(auto& [i, w]: g[x]) {
| ^
designated_cities.cpp: In function 'void dfs(int, int, ll)':
designated_cities.cpp:61:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | for(auto& [i, w]: g[x]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
8 ms |
9324 KB |
Output is correct |
3 |
Correct |
7 ms |
9204 KB |
Output is correct |
4 |
Correct |
7 ms |
9324 KB |
Output is correct |
5 |
Correct |
8 ms |
9324 KB |
Output is correct |
6 |
Correct |
8 ms |
9324 KB |
Output is correct |
7 |
Correct |
8 ms |
9324 KB |
Output is correct |
8 |
Correct |
8 ms |
9324 KB |
Output is correct |
9 |
Correct |
7 ms |
9324 KB |
Output is correct |
10 |
Correct |
7 ms |
9324 KB |
Output is correct |
11 |
Correct |
8 ms |
9324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
766 ms |
35692 KB |
Output is correct |
3 |
Correct |
876 ms |
53532 KB |
Output is correct |
4 |
Correct |
709 ms |
35692 KB |
Output is correct |
5 |
Correct |
737 ms |
35556 KB |
Output is correct |
6 |
Correct |
793 ms |
37964 KB |
Output is correct |
7 |
Correct |
684 ms |
35776 KB |
Output is correct |
8 |
Correct |
883 ms |
52556 KB |
Output is correct |
9 |
Correct |
580 ms |
36160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9452 KB |
Output is correct |
2 |
Correct |
761 ms |
35744 KB |
Output is correct |
3 |
Correct |
860 ms |
53580 KB |
Output is correct |
4 |
Correct |
713 ms |
35564 KB |
Output is correct |
5 |
Correct |
746 ms |
35524 KB |
Output is correct |
6 |
Correct |
809 ms |
38696 KB |
Output is correct |
7 |
Correct |
606 ms |
35904 KB |
Output is correct |
8 |
Correct |
863 ms |
47180 KB |
Output is correct |
9 |
Correct |
573 ms |
36060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
8 ms |
9324 KB |
Output is correct |
3 |
Correct |
7 ms |
9204 KB |
Output is correct |
4 |
Correct |
7 ms |
9324 KB |
Output is correct |
5 |
Correct |
8 ms |
9324 KB |
Output is correct |
6 |
Correct |
8 ms |
9324 KB |
Output is correct |
7 |
Correct |
8 ms |
9324 KB |
Output is correct |
8 |
Correct |
8 ms |
9324 KB |
Output is correct |
9 |
Correct |
7 ms |
9324 KB |
Output is correct |
10 |
Correct |
7 ms |
9324 KB |
Output is correct |
11 |
Correct |
8 ms |
9324 KB |
Output is correct |
12 |
Correct |
7 ms |
9324 KB |
Output is correct |
13 |
Correct |
12 ms |
9580 KB |
Output is correct |
14 |
Correct |
12 ms |
9836 KB |
Output is correct |
15 |
Correct |
12 ms |
9580 KB |
Output is correct |
16 |
Correct |
12 ms |
9708 KB |
Output is correct |
17 |
Correct |
11 ms |
9580 KB |
Output is correct |
18 |
Correct |
11 ms |
9580 KB |
Output is correct |
19 |
Correct |
12 ms |
9580 KB |
Output is correct |
20 |
Correct |
12 ms |
9580 KB |
Output is correct |
21 |
Correct |
11 ms |
9708 KB |
Output is correct |
22 |
Correct |
11 ms |
9580 KB |
Output is correct |
23 |
Correct |
11 ms |
9580 KB |
Output is correct |
24 |
Correct |
11 ms |
9708 KB |
Output is correct |
25 |
Correct |
12 ms |
9836 KB |
Output is correct |
26 |
Correct |
11 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
766 ms |
35692 KB |
Output is correct |
3 |
Correct |
876 ms |
53532 KB |
Output is correct |
4 |
Correct |
709 ms |
35692 KB |
Output is correct |
5 |
Correct |
737 ms |
35556 KB |
Output is correct |
6 |
Correct |
793 ms |
37964 KB |
Output is correct |
7 |
Correct |
684 ms |
35776 KB |
Output is correct |
8 |
Correct |
883 ms |
52556 KB |
Output is correct |
9 |
Correct |
580 ms |
36160 KB |
Output is correct |
10 |
Correct |
8 ms |
9452 KB |
Output is correct |
11 |
Correct |
761 ms |
35744 KB |
Output is correct |
12 |
Correct |
860 ms |
53580 KB |
Output is correct |
13 |
Correct |
713 ms |
35564 KB |
Output is correct |
14 |
Correct |
746 ms |
35524 KB |
Output is correct |
15 |
Correct |
809 ms |
38696 KB |
Output is correct |
16 |
Correct |
606 ms |
35904 KB |
Output is correct |
17 |
Correct |
863 ms |
47180 KB |
Output is correct |
18 |
Correct |
573 ms |
36060 KB |
Output is correct |
19 |
Correct |
8 ms |
9324 KB |
Output is correct |
20 |
Correct |
767 ms |
35820 KB |
Output is correct |
21 |
Correct |
863 ms |
59652 KB |
Output is correct |
22 |
Correct |
721 ms |
40572 KB |
Output is correct |
23 |
Correct |
782 ms |
42184 KB |
Output is correct |
24 |
Correct |
777 ms |
40940 KB |
Output is correct |
25 |
Correct |
780 ms |
42060 KB |
Output is correct |
26 |
Correct |
755 ms |
40908 KB |
Output is correct |
27 |
Correct |
733 ms |
41928 KB |
Output is correct |
28 |
Correct |
788 ms |
44364 KB |
Output is correct |
29 |
Correct |
786 ms |
42092 KB |
Output is correct |
30 |
Correct |
710 ms |
41060 KB |
Output is correct |
31 |
Correct |
634 ms |
42264 KB |
Output is correct |
32 |
Correct |
847 ms |
54172 KB |
Output is correct |
33 |
Correct |
566 ms |
42588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9324 KB |
Output is correct |
2 |
Correct |
8 ms |
9324 KB |
Output is correct |
3 |
Correct |
7 ms |
9204 KB |
Output is correct |
4 |
Correct |
7 ms |
9324 KB |
Output is correct |
5 |
Correct |
8 ms |
9324 KB |
Output is correct |
6 |
Correct |
8 ms |
9324 KB |
Output is correct |
7 |
Correct |
8 ms |
9324 KB |
Output is correct |
8 |
Correct |
8 ms |
9324 KB |
Output is correct |
9 |
Correct |
7 ms |
9324 KB |
Output is correct |
10 |
Correct |
7 ms |
9324 KB |
Output is correct |
11 |
Correct |
8 ms |
9324 KB |
Output is correct |
12 |
Correct |
7 ms |
9324 KB |
Output is correct |
13 |
Correct |
766 ms |
35692 KB |
Output is correct |
14 |
Correct |
876 ms |
53532 KB |
Output is correct |
15 |
Correct |
709 ms |
35692 KB |
Output is correct |
16 |
Correct |
737 ms |
35556 KB |
Output is correct |
17 |
Correct |
793 ms |
37964 KB |
Output is correct |
18 |
Correct |
684 ms |
35776 KB |
Output is correct |
19 |
Correct |
883 ms |
52556 KB |
Output is correct |
20 |
Correct |
580 ms |
36160 KB |
Output is correct |
21 |
Correct |
8 ms |
9452 KB |
Output is correct |
22 |
Correct |
761 ms |
35744 KB |
Output is correct |
23 |
Correct |
860 ms |
53580 KB |
Output is correct |
24 |
Correct |
713 ms |
35564 KB |
Output is correct |
25 |
Correct |
746 ms |
35524 KB |
Output is correct |
26 |
Correct |
809 ms |
38696 KB |
Output is correct |
27 |
Correct |
606 ms |
35904 KB |
Output is correct |
28 |
Correct |
863 ms |
47180 KB |
Output is correct |
29 |
Correct |
573 ms |
36060 KB |
Output is correct |
30 |
Correct |
7 ms |
9324 KB |
Output is correct |
31 |
Correct |
12 ms |
9580 KB |
Output is correct |
32 |
Correct |
12 ms |
9836 KB |
Output is correct |
33 |
Correct |
12 ms |
9580 KB |
Output is correct |
34 |
Correct |
12 ms |
9708 KB |
Output is correct |
35 |
Correct |
11 ms |
9580 KB |
Output is correct |
36 |
Correct |
11 ms |
9580 KB |
Output is correct |
37 |
Correct |
12 ms |
9580 KB |
Output is correct |
38 |
Correct |
12 ms |
9580 KB |
Output is correct |
39 |
Correct |
11 ms |
9708 KB |
Output is correct |
40 |
Correct |
11 ms |
9580 KB |
Output is correct |
41 |
Correct |
11 ms |
9580 KB |
Output is correct |
42 |
Correct |
11 ms |
9708 KB |
Output is correct |
43 |
Correct |
12 ms |
9836 KB |
Output is correct |
44 |
Correct |
11 ms |
9708 KB |
Output is correct |
45 |
Correct |
8 ms |
9324 KB |
Output is correct |
46 |
Correct |
767 ms |
35820 KB |
Output is correct |
47 |
Correct |
863 ms |
59652 KB |
Output is correct |
48 |
Correct |
721 ms |
40572 KB |
Output is correct |
49 |
Correct |
782 ms |
42184 KB |
Output is correct |
50 |
Correct |
777 ms |
40940 KB |
Output is correct |
51 |
Correct |
780 ms |
42060 KB |
Output is correct |
52 |
Correct |
755 ms |
40908 KB |
Output is correct |
53 |
Correct |
733 ms |
41928 KB |
Output is correct |
54 |
Correct |
788 ms |
44364 KB |
Output is correct |
55 |
Correct |
786 ms |
42092 KB |
Output is correct |
56 |
Correct |
710 ms |
41060 KB |
Output is correct |
57 |
Correct |
634 ms |
42264 KB |
Output is correct |
58 |
Correct |
847 ms |
54172 KB |
Output is correct |
59 |
Correct |
566 ms |
42588 KB |
Output is correct |
60 |
Correct |
7 ms |
9324 KB |
Output is correct |
61 |
Correct |
801 ms |
44492 KB |
Output is correct |
62 |
Correct |
877 ms |
61596 KB |
Output is correct |
63 |
Correct |
742 ms |
43468 KB |
Output is correct |
64 |
Correct |
804 ms |
44748 KB |
Output is correct |
65 |
Correct |
812 ms |
43340 KB |
Output is correct |
66 |
Correct |
802 ms |
44876 KB |
Output is correct |
67 |
Correct |
796 ms |
43468 KB |
Output is correct |
68 |
Correct |
768 ms |
44616 KB |
Output is correct |
69 |
Correct |
806 ms |
47180 KB |
Output is correct |
70 |
Correct |
793 ms |
44644 KB |
Output is correct |
71 |
Correct |
739 ms |
43860 KB |
Output is correct |
72 |
Correct |
702 ms |
45312 KB |
Output is correct |
73 |
Correct |
912 ms |
56060 KB |
Output is correct |
74 |
Correct |
604 ms |
46656 KB |
Output is correct |