#include <bits/stdc++.h>
using namespace std;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 200005;
int n, q, br, eu[MXN], ev[MXN], a[MXN], b[MXN];
vector<int> eid[MXN];
int ans[MXN];
int nsz[MXN], fsz[MXN], rt;
pii dp[MXN];
void GUIDE(int id, int par) {
nsz[id] = 0;
for (auto &e : eid[id]) {
int i = eu[e] ^ ev[e] ^ id;
if (i == par) continue;
if (id == ev[e]) {
swap(eu[e], ev[e]);
swap(a[e], b[e]);
}
GUIDE(i, id);
nsz[id] += a[e] + nsz[i];
}
}
void PUSH(pii &p, int x) {
if (x >= p.fs) {
p.sc = p.fs;
p.fs = x;
} else p.sc = max(p.sc, x);
}
void DP(int id, int par, int acc) {
fsz[id] = acc;
dp[id] = mp(0LL, 0LL);
for (auto &e : eid[id]) {
if (eu[e] != id) continue;
int i = ev[e];
DP(i, id, acc - a[e] + b[e]);
PUSH(dp[id], dp[i].fs + a[e]);
}
}
int CALC_1() {
return *min_element(fsz + 1, fsz + n + 1);
}
int CALC_2() {
rt = 1;
FOR(i, 2, n + 1) if (fsz[rt] - dp[rt].fs - dp[rt].sc > fsz[i] - dp[i].fs - dp[i].sc) rt = i;
return fsz[rt] - dp[rt].fs - dp[rt].sc;
}
namespace TREE {
pii son[MXN];
int acc[MXN];
multiset<int> MS;
void get_info(int id, int par) {
son[id] = mp(0LL, 0LL);
for (auto &e : eid[id]) {
if (eu[e] != id) continue;
int i = ev[e];
get_info(i, id);
son[id] = max(son[id], mp(a[e] + son[i].fs, e));
}
}
void decompose(int id, int par, int val) {
debug(id, par, val);
if (eid[id].size() == 1) {
MS.insert(val);
return;
}
decompose(ev[son[id].sc], id, val + a[son[id].sc]);
for (auto &e : eid[id]) {
if (eu[e] != id) continue;
if (e == son[id].sc) continue;
decompose(ev[e], id, a[e]);
}
}
void DO(int rt) {
get_info(rt, 0);
for (auto &e : eid[rt]) {
decompose(ev[e], rt, a[e]);
}
if (dp[rt].fs != 0) MS.erase(MS.find(dp[rt].fs));
if (dp[rt].sc != 0) MS.erase(MS.find(dp[rt].sc));
int ptr = 3;
for (auto it = MS.rbegin(); it != MS.rend(); it++, ptr++) ans[ptr] = ans[ptr - 1] - *it;
FOR(i, ptr, n + 1) ans[i] = ans[i - 1];
}
}
void miku() {
cin >> n;
FOR(i, 0, n - 1) {
cin >> eu[i] >> ev[i] >> a[i] >> b[i];
eid[eu[i]].push_back(i);
eid[ev[i]].push_back(i);
}
GUIDE(1, 0);
DP(1, 0, nsz[1]);
ans[1] = CALC_1();
ans[2] = CALC_2();
GUIDE(rt, 0);
TREE::DO(rt);
cin >> q;
while (q--) {
cin >> br;
cout << ans[br] << '\n';
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
Compilation message
designated_cities.cpp: In function 'void TREE::decompose(long long int, long long int, long long int)':
designated_cities.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
designated_cities.cpp:87:9: note: in expansion of macro 'debug'
87 | debug(id, par, val);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
3 ms |
20924 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20964 KB |
Output is correct |
6 |
Correct |
4 ms |
21180 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
4 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
20988 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
304 ms |
34996 KB |
Output is correct |
3 |
Correct |
371 ms |
45288 KB |
Output is correct |
4 |
Correct |
325 ms |
34896 KB |
Output is correct |
5 |
Correct |
304 ms |
35260 KB |
Output is correct |
6 |
Correct |
281 ms |
36916 KB |
Output is correct |
7 |
Correct |
219 ms |
37060 KB |
Output is correct |
8 |
Correct |
424 ms |
46308 KB |
Output is correct |
9 |
Correct |
164 ms |
40808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
302 ms |
34776 KB |
Output is correct |
3 |
Correct |
404 ms |
47944 KB |
Output is correct |
4 |
Correct |
353 ms |
34632 KB |
Output is correct |
5 |
Correct |
263 ms |
35276 KB |
Output is correct |
6 |
Correct |
313 ms |
37052 KB |
Output is correct |
7 |
Correct |
217 ms |
40144 KB |
Output is correct |
8 |
Correct |
387 ms |
43076 KB |
Output is correct |
9 |
Correct |
169 ms |
41060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
3 ms |
20924 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20964 KB |
Output is correct |
6 |
Correct |
4 ms |
21180 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
4 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
20988 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
20824 KB |
Output is correct |
13 |
Correct |
5 ms |
21084 KB |
Output is correct |
14 |
Correct |
5 ms |
21084 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
5 ms |
21084 KB |
Output is correct |
17 |
Correct |
6 ms |
21084 KB |
Output is correct |
18 |
Correct |
5 ms |
21084 KB |
Output is correct |
19 |
Correct |
5 ms |
21084 KB |
Output is correct |
20 |
Correct |
5 ms |
20976 KB |
Output is correct |
21 |
Correct |
5 ms |
21084 KB |
Output is correct |
22 |
Correct |
6 ms |
21336 KB |
Output is correct |
23 |
Correct |
4 ms |
21084 KB |
Output is correct |
24 |
Correct |
5 ms |
21340 KB |
Output is correct |
25 |
Correct |
5 ms |
21084 KB |
Output is correct |
26 |
Correct |
5 ms |
21336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
304 ms |
34996 KB |
Output is correct |
3 |
Correct |
371 ms |
45288 KB |
Output is correct |
4 |
Correct |
325 ms |
34896 KB |
Output is correct |
5 |
Correct |
304 ms |
35260 KB |
Output is correct |
6 |
Correct |
281 ms |
36916 KB |
Output is correct |
7 |
Correct |
219 ms |
37060 KB |
Output is correct |
8 |
Correct |
424 ms |
46308 KB |
Output is correct |
9 |
Correct |
164 ms |
40808 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
302 ms |
34776 KB |
Output is correct |
12 |
Correct |
404 ms |
47944 KB |
Output is correct |
13 |
Correct |
353 ms |
34632 KB |
Output is correct |
14 |
Correct |
263 ms |
35276 KB |
Output is correct |
15 |
Correct |
313 ms |
37052 KB |
Output is correct |
16 |
Correct |
217 ms |
40144 KB |
Output is correct |
17 |
Correct |
387 ms |
43076 KB |
Output is correct |
18 |
Correct |
169 ms |
41060 KB |
Output is correct |
19 |
Correct |
3 ms |
20828 KB |
Output is correct |
20 |
Correct |
350 ms |
34772 KB |
Output is correct |
21 |
Correct |
408 ms |
54432 KB |
Output is correct |
22 |
Correct |
278 ms |
39680 KB |
Output is correct |
23 |
Correct |
338 ms |
41300 KB |
Output is correct |
24 |
Correct |
287 ms |
39984 KB |
Output is correct |
25 |
Correct |
338 ms |
41244 KB |
Output is correct |
26 |
Correct |
307 ms |
40020 KB |
Output is correct |
27 |
Correct |
326 ms |
41536 KB |
Output is correct |
28 |
Correct |
365 ms |
43068 KB |
Output is correct |
29 |
Correct |
340 ms |
40744 KB |
Output is correct |
30 |
Correct |
291 ms |
41420 KB |
Output is correct |
31 |
Correct |
278 ms |
44124 KB |
Output is correct |
32 |
Correct |
424 ms |
49992 KB |
Output is correct |
33 |
Correct |
157 ms |
47440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
3 ms |
20924 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20964 KB |
Output is correct |
6 |
Correct |
4 ms |
21180 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
3 ms |
20828 KB |
Output is correct |
9 |
Correct |
4 ms |
20828 KB |
Output is correct |
10 |
Correct |
3 ms |
20988 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
304 ms |
34996 KB |
Output is correct |
14 |
Correct |
371 ms |
45288 KB |
Output is correct |
15 |
Correct |
325 ms |
34896 KB |
Output is correct |
16 |
Correct |
304 ms |
35260 KB |
Output is correct |
17 |
Correct |
281 ms |
36916 KB |
Output is correct |
18 |
Correct |
219 ms |
37060 KB |
Output is correct |
19 |
Correct |
424 ms |
46308 KB |
Output is correct |
20 |
Correct |
164 ms |
40808 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
302 ms |
34776 KB |
Output is correct |
23 |
Correct |
404 ms |
47944 KB |
Output is correct |
24 |
Correct |
353 ms |
34632 KB |
Output is correct |
25 |
Correct |
263 ms |
35276 KB |
Output is correct |
26 |
Correct |
313 ms |
37052 KB |
Output is correct |
27 |
Correct |
217 ms |
40144 KB |
Output is correct |
28 |
Correct |
387 ms |
43076 KB |
Output is correct |
29 |
Correct |
169 ms |
41060 KB |
Output is correct |
30 |
Correct |
4 ms |
20824 KB |
Output is correct |
31 |
Correct |
5 ms |
21084 KB |
Output is correct |
32 |
Correct |
5 ms |
21084 KB |
Output is correct |
33 |
Correct |
5 ms |
21084 KB |
Output is correct |
34 |
Correct |
5 ms |
21084 KB |
Output is correct |
35 |
Correct |
6 ms |
21084 KB |
Output is correct |
36 |
Correct |
5 ms |
21084 KB |
Output is correct |
37 |
Correct |
5 ms |
21084 KB |
Output is correct |
38 |
Correct |
5 ms |
20976 KB |
Output is correct |
39 |
Correct |
5 ms |
21084 KB |
Output is correct |
40 |
Correct |
6 ms |
21336 KB |
Output is correct |
41 |
Correct |
4 ms |
21084 KB |
Output is correct |
42 |
Correct |
5 ms |
21340 KB |
Output is correct |
43 |
Correct |
5 ms |
21084 KB |
Output is correct |
44 |
Correct |
5 ms |
21336 KB |
Output is correct |
45 |
Correct |
3 ms |
20828 KB |
Output is correct |
46 |
Correct |
350 ms |
34772 KB |
Output is correct |
47 |
Correct |
408 ms |
54432 KB |
Output is correct |
48 |
Correct |
278 ms |
39680 KB |
Output is correct |
49 |
Correct |
338 ms |
41300 KB |
Output is correct |
50 |
Correct |
287 ms |
39984 KB |
Output is correct |
51 |
Correct |
338 ms |
41244 KB |
Output is correct |
52 |
Correct |
307 ms |
40020 KB |
Output is correct |
53 |
Correct |
326 ms |
41536 KB |
Output is correct |
54 |
Correct |
365 ms |
43068 KB |
Output is correct |
55 |
Correct |
340 ms |
40744 KB |
Output is correct |
56 |
Correct |
291 ms |
41420 KB |
Output is correct |
57 |
Correct |
278 ms |
44124 KB |
Output is correct |
58 |
Correct |
424 ms |
49992 KB |
Output is correct |
59 |
Correct |
157 ms |
47440 KB |
Output is correct |
60 |
Correct |
4 ms |
20824 KB |
Output is correct |
61 |
Correct |
316 ms |
43864 KB |
Output is correct |
62 |
Correct |
469 ms |
53460 KB |
Output is correct |
63 |
Correct |
378 ms |
42400 KB |
Output is correct |
64 |
Correct |
381 ms |
43820 KB |
Output is correct |
65 |
Correct |
365 ms |
42580 KB |
Output is correct |
66 |
Correct |
407 ms |
43844 KB |
Output is correct |
67 |
Correct |
315 ms |
42676 KB |
Output is correct |
68 |
Correct |
314 ms |
44236 KB |
Output is correct |
69 |
Correct |
499 ms |
45628 KB |
Output is correct |
70 |
Correct |
390 ms |
42904 KB |
Output is correct |
71 |
Correct |
308 ms |
44180 KB |
Output is correct |
72 |
Correct |
351 ms |
47020 KB |
Output is correct |
73 |
Correct |
474 ms |
50908 KB |
Output is correct |
74 |
Correct |
214 ms |
51648 KB |
Output is correct |