#include<bits/stdc++.h>
#define mp make_pair
#define no cout << "ickgf\n"
using namespace std;
typedef long long ll;
int n, mi1;
ll an[200001], ma = 0;
vector<pair<int, pair<ll, ll>>> e[200001];
priority_queue<pair<ll, int>> pq[200001];
pair<ll, ll> eat(int x, int p){
pair<ll, ll> re = {0ll, 0ll}, tm;
for (pair<int, pair<ll, ll>> pi : e[x]) {
if (pi.first != p) {
tm = eat(pi.first, x);
re.first += tm.first + pi.second.second;
re.second = max(re.second, tm.second + pi.second.first - pi.second.second);
}
}
return re;
}
pair<ll, ll> nom(int x, int p, ll ba) {
pair<ll, ll> m1 = {0, 0}, m2 = {0, 0}, tm;
for (pair<int, pair<ll, ll>> pi : e[x]) {
if (pi.first != p) {
tm = nom(pi.first, x, ba + pi.second.first - pi.second.second);
tm.first += (ll) pi.second.first;
if (tm.first > m1.first) {
m2 = m1, m1 = tm;
} else if (tm.first > m2.first) {
m2 = tm;
}
}
}
if (m2.first > 0ll) {
ba += m1.first + m2.first;
if (ba > ma) {
ma = ba, mi1 = m1.second;
}
} else if (m1.first == 0ll) {
m1.second = x;
}
return m1;
}
ll yum(int x, int p){
if (e[x].size() == 1 && p != 0) {
return 0;
}
for (pair<int, pair<ll, ll>> pi : e[x]) {
if (pi.first != p) {
pq[x].push({yum(pi.first, x) + pi.second.first, pi.first});
}
}
return pq[x].top().first;
}
void mhm(int x) {
if (!pq[x].empty()) {
pair<ll, int> ppp = pq[x].top();
pq[x].pop();
mhm(ppp.second);
}
while (!pq[x].empty()) {
pq[mi1].push(pq[x].top());
pq[x].pop();
}
}
int main(){
int a, b, c, d, ej, q;
ll tot = 0;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a >> b >> c >> d;
e[a].push_back({b, {c, d}});
e[b].push_back({a, {d, c}});
tot += (ll) c + d;
}
if (n == 2) {
an[1] = min(c, d);
} else {
int sta = 1;
if (e[1].size() == 1) {
sta = e[1][0].first;
}
pair<ll, ll> re = eat(sta, 0), re2 = nom(sta, 0, re.first);
an[1] = tot - re.first - re.second;
yum(mi1, 0);
tot -= ma;
an[2] = tot;
pair<ll, int> ppp;
ppp = pq[mi1].top();
pq[mi1].pop();
mhm(ppp.second);
for (int i = 3; tot > 0 && i <= n; i++) {
ppp = pq[mi1].top();
pq[mi1].pop();
mhm(ppp.second);
tot -= ppp.first;
an[i] = tot;
}
}
cin >> q;
for (int i = 0; i < q; i++) {
cin >> ej;
cout << an[ej] << '\n';
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:83:40: warning: variable 're2' set but not used [-Wunused-but-set-variable]
83 | pair<ll, ll> re = eat(sta, 0), re2 = nom(sta, 0, re.first);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11212 KB |
Output is correct |
2 |
Correct |
7 ms |
11212 KB |
Output is correct |
3 |
Correct |
8 ms |
11260 KB |
Output is correct |
4 |
Correct |
7 ms |
11212 KB |
Output is correct |
5 |
Correct |
7 ms |
11232 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
9 ms |
11208 KB |
Output is correct |
8 |
Correct |
7 ms |
11212 KB |
Output is correct |
9 |
Correct |
8 ms |
11212 KB |
Output is correct |
10 |
Correct |
8 ms |
11256 KB |
Output is correct |
11 |
Correct |
7 ms |
11260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11212 KB |
Output is correct |
2 |
Correct |
633 ms |
31264 KB |
Output is correct |
3 |
Correct |
609 ms |
58312 KB |
Output is correct |
4 |
Correct |
551 ms |
35368 KB |
Output is correct |
5 |
Correct |
627 ms |
38068 KB |
Output is correct |
6 |
Correct |
609 ms |
40252 KB |
Output is correct |
7 |
Correct |
537 ms |
38640 KB |
Output is correct |
8 |
Correct |
665 ms |
57388 KB |
Output is correct |
9 |
Correct |
479 ms |
40708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11212 KB |
Output is correct |
2 |
Correct |
577 ms |
31292 KB |
Output is correct |
3 |
Correct |
619 ms |
58228 KB |
Output is correct |
4 |
Correct |
524 ms |
35300 KB |
Output is correct |
5 |
Correct |
585 ms |
38256 KB |
Output is correct |
6 |
Correct |
622 ms |
41248 KB |
Output is correct |
7 |
Correct |
480 ms |
40420 KB |
Output is correct |
8 |
Correct |
606 ms |
51052 KB |
Output is correct |
9 |
Correct |
496 ms |
40176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11212 KB |
Output is correct |
2 |
Correct |
7 ms |
11212 KB |
Output is correct |
3 |
Correct |
8 ms |
11260 KB |
Output is correct |
4 |
Correct |
7 ms |
11212 KB |
Output is correct |
5 |
Correct |
7 ms |
11232 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
9 ms |
11208 KB |
Output is correct |
8 |
Correct |
7 ms |
11212 KB |
Output is correct |
9 |
Correct |
8 ms |
11212 KB |
Output is correct |
10 |
Correct |
8 ms |
11256 KB |
Output is correct |
11 |
Correct |
7 ms |
11260 KB |
Output is correct |
12 |
Correct |
6 ms |
11212 KB |
Output is correct |
13 |
Correct |
14 ms |
11496 KB |
Output is correct |
14 |
Correct |
14 ms |
11724 KB |
Output is correct |
15 |
Correct |
15 ms |
11416 KB |
Output is correct |
16 |
Correct |
15 ms |
11524 KB |
Output is correct |
17 |
Correct |
14 ms |
11468 KB |
Output is correct |
18 |
Correct |
14 ms |
11540 KB |
Output is correct |
19 |
Correct |
14 ms |
11548 KB |
Output is correct |
20 |
Correct |
14 ms |
11548 KB |
Output is correct |
21 |
Correct |
15 ms |
11468 KB |
Output is correct |
22 |
Correct |
15 ms |
11468 KB |
Output is correct |
23 |
Correct |
14 ms |
11468 KB |
Output is correct |
24 |
Correct |
14 ms |
11528 KB |
Output is correct |
25 |
Correct |
17 ms |
11780 KB |
Output is correct |
26 |
Correct |
15 ms |
11528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11212 KB |
Output is correct |
2 |
Correct |
633 ms |
31264 KB |
Output is correct |
3 |
Correct |
609 ms |
58312 KB |
Output is correct |
4 |
Correct |
551 ms |
35368 KB |
Output is correct |
5 |
Correct |
627 ms |
38068 KB |
Output is correct |
6 |
Correct |
609 ms |
40252 KB |
Output is correct |
7 |
Correct |
537 ms |
38640 KB |
Output is correct |
8 |
Correct |
665 ms |
57388 KB |
Output is correct |
9 |
Correct |
479 ms |
40708 KB |
Output is correct |
10 |
Correct |
7 ms |
11212 KB |
Output is correct |
11 |
Correct |
577 ms |
31292 KB |
Output is correct |
12 |
Correct |
619 ms |
58228 KB |
Output is correct |
13 |
Correct |
524 ms |
35300 KB |
Output is correct |
14 |
Correct |
585 ms |
38256 KB |
Output is correct |
15 |
Correct |
622 ms |
41248 KB |
Output is correct |
16 |
Correct |
480 ms |
40420 KB |
Output is correct |
17 |
Correct |
606 ms |
51052 KB |
Output is correct |
18 |
Correct |
496 ms |
40176 KB |
Output is correct |
19 |
Correct |
7 ms |
11212 KB |
Output is correct |
20 |
Correct |
583 ms |
37724 KB |
Output is correct |
21 |
Correct |
631 ms |
58320 KB |
Output is correct |
22 |
Correct |
510 ms |
35348 KB |
Output is correct |
23 |
Correct |
589 ms |
37860 KB |
Output is correct |
24 |
Correct |
573 ms |
36384 KB |
Output is correct |
25 |
Correct |
604 ms |
37696 KB |
Output is correct |
26 |
Correct |
576 ms |
36660 KB |
Output is correct |
27 |
Correct |
582 ms |
37568 KB |
Output is correct |
28 |
Correct |
617 ms |
40552 KB |
Output is correct |
29 |
Correct |
612 ms |
37752 KB |
Output is correct |
30 |
Correct |
518 ms |
36796 KB |
Output is correct |
31 |
Correct |
510 ms |
38540 KB |
Output is correct |
32 |
Correct |
623 ms |
51740 KB |
Output is correct |
33 |
Correct |
495 ms |
40648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11212 KB |
Output is correct |
2 |
Correct |
7 ms |
11212 KB |
Output is correct |
3 |
Correct |
8 ms |
11260 KB |
Output is correct |
4 |
Correct |
7 ms |
11212 KB |
Output is correct |
5 |
Correct |
7 ms |
11232 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
9 ms |
11208 KB |
Output is correct |
8 |
Correct |
7 ms |
11212 KB |
Output is correct |
9 |
Correct |
8 ms |
11212 KB |
Output is correct |
10 |
Correct |
8 ms |
11256 KB |
Output is correct |
11 |
Correct |
7 ms |
11260 KB |
Output is correct |
12 |
Correct |
8 ms |
11212 KB |
Output is correct |
13 |
Correct |
633 ms |
31264 KB |
Output is correct |
14 |
Correct |
609 ms |
58312 KB |
Output is correct |
15 |
Correct |
551 ms |
35368 KB |
Output is correct |
16 |
Correct |
627 ms |
38068 KB |
Output is correct |
17 |
Correct |
609 ms |
40252 KB |
Output is correct |
18 |
Correct |
537 ms |
38640 KB |
Output is correct |
19 |
Correct |
665 ms |
57388 KB |
Output is correct |
20 |
Correct |
479 ms |
40708 KB |
Output is correct |
21 |
Correct |
7 ms |
11212 KB |
Output is correct |
22 |
Correct |
577 ms |
31292 KB |
Output is correct |
23 |
Correct |
619 ms |
58228 KB |
Output is correct |
24 |
Correct |
524 ms |
35300 KB |
Output is correct |
25 |
Correct |
585 ms |
38256 KB |
Output is correct |
26 |
Correct |
622 ms |
41248 KB |
Output is correct |
27 |
Correct |
480 ms |
40420 KB |
Output is correct |
28 |
Correct |
606 ms |
51052 KB |
Output is correct |
29 |
Correct |
496 ms |
40176 KB |
Output is correct |
30 |
Correct |
6 ms |
11212 KB |
Output is correct |
31 |
Correct |
14 ms |
11496 KB |
Output is correct |
32 |
Correct |
14 ms |
11724 KB |
Output is correct |
33 |
Correct |
15 ms |
11416 KB |
Output is correct |
34 |
Correct |
15 ms |
11524 KB |
Output is correct |
35 |
Correct |
14 ms |
11468 KB |
Output is correct |
36 |
Correct |
14 ms |
11540 KB |
Output is correct |
37 |
Correct |
14 ms |
11548 KB |
Output is correct |
38 |
Correct |
14 ms |
11548 KB |
Output is correct |
39 |
Correct |
15 ms |
11468 KB |
Output is correct |
40 |
Correct |
15 ms |
11468 KB |
Output is correct |
41 |
Correct |
14 ms |
11468 KB |
Output is correct |
42 |
Correct |
14 ms |
11528 KB |
Output is correct |
43 |
Correct |
17 ms |
11780 KB |
Output is correct |
44 |
Correct |
15 ms |
11528 KB |
Output is correct |
45 |
Correct |
7 ms |
11212 KB |
Output is correct |
46 |
Correct |
583 ms |
37724 KB |
Output is correct |
47 |
Correct |
631 ms |
58320 KB |
Output is correct |
48 |
Correct |
510 ms |
35348 KB |
Output is correct |
49 |
Correct |
589 ms |
37860 KB |
Output is correct |
50 |
Correct |
573 ms |
36384 KB |
Output is correct |
51 |
Correct |
604 ms |
37696 KB |
Output is correct |
52 |
Correct |
576 ms |
36660 KB |
Output is correct |
53 |
Correct |
582 ms |
37568 KB |
Output is correct |
54 |
Correct |
617 ms |
40552 KB |
Output is correct |
55 |
Correct |
612 ms |
37752 KB |
Output is correct |
56 |
Correct |
518 ms |
36796 KB |
Output is correct |
57 |
Correct |
510 ms |
38540 KB |
Output is correct |
58 |
Correct |
623 ms |
51740 KB |
Output is correct |
59 |
Correct |
495 ms |
40648 KB |
Output is correct |
60 |
Correct |
6 ms |
11180 KB |
Output is correct |
61 |
Correct |
1016 ms |
40528 KB |
Output is correct |
62 |
Correct |
1040 ms |
59992 KB |
Output is correct |
63 |
Correct |
925 ms |
38008 KB |
Output is correct |
64 |
Correct |
1025 ms |
40464 KB |
Output is correct |
65 |
Correct |
949 ms |
38924 KB |
Output is correct |
66 |
Correct |
967 ms |
40264 KB |
Output is correct |
67 |
Correct |
952 ms |
38912 KB |
Output is correct |
68 |
Correct |
988 ms |
40416 KB |
Output is correct |
69 |
Correct |
1004 ms |
43196 KB |
Output is correct |
70 |
Correct |
1002 ms |
40404 KB |
Output is correct |
71 |
Correct |
917 ms |
39676 KB |
Output is correct |
72 |
Correct |
920 ms |
41944 KB |
Output is correct |
73 |
Correct |
1046 ms |
53932 KB |
Output is correct |
74 |
Correct |
892 ms |
44724 KB |
Output is correct |