/*input
15
14 5 12 7
14 12 6 5
14 10 14 16
9 14 16 12
13 7 4 15
1 3 8 1
6 7 15 13
15 4 4 6
9 1 12 6
13 1 7 6
13 4 5 15
2 6 11 19
8 4 12 7
13 11 14 5
3
3
6
7
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <numeric>
using namespace std;
const int MAXN = 200007;
const int64_t INF = 1e17;
int N, Q, A[MAXN], B[MAXN], C[MAXN], D[MAXN];
int root = 0;
int64_t ans[MAXN], up[MAXN], down[MAXN], dep[MAXN];
int furthest[MAXN];
vector<int> adj[MAXN];
vector<int64_t> vals;
void dfs_down(int u, int p)
{
down[u] = 0;
furthest[u] = u;
for (int id : adj[u]) {
int v = A[id] ^ B[id] ^ u, c = (A[id] == u ? C[id] : D[id]);
if (v == p) continue;
dep[v] = dep[u] + c;
dfs_down(v, u);
if (dep[furthest[u]] < dep[furthest[v]]) furthest[u] = furthest[v];
down[u] += c + down[v];
}
}
void dfs_up(int u, int p)
{
int leaf[2] = {0, 0};
for (int id : adj[u]) {
int v = A[id] ^ B[id] ^ u, c = (A[id] == u ? C[id] : D[id]);
if (v == p) continue;
up[v] = up[u] + down[u] - down[v] + (C[id] ^ D[id] ^ c) - c;
dfs_up(v, u);
int cur = furthest[v];
if (dep[cur] > dep[leaf[0]]) swap(cur, leaf[0]);
if (dep[cur] > dep[leaf[1]]) swap(cur, leaf[1]);
}
if (up[u] + down[u] < ans[1]) ans[1] = up[u] + down[u];
if (up[u] + down[u] - dep[leaf[0]] - dep[leaf[1]] + 2 * dep[u] < ans[2]) {
ans[2] = up[u] + down[u] - dep[leaf[0]] - dep[leaf[1]] + 2 * dep[u];
root = leaf[0];
}
}
void dfs_calc(int u, int p, int64_t inc)
{
vals.push_back((u == furthest[u]) * inc);
for (int id : adj[u]) {
int v = A[id] ^ B[id] ^ u, c = (A[id] == u ? C[id] : D[id]);
if (v == p) continue;
dfs_calc(v, u, c + (furthest[u] == furthest[v]) * inc);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N;
for (int i = 0; i < N - 1; ++i) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
adj[A[i]].push_back(i);
adj[B[i]].push_back(i);
}
ans[1] = ans[2] = INF;
vals.reserve(N);
dfs_down(1, 0);
dfs_up(1, 0);
dep[root] = 0, dfs_down(root, 0);
dfs_calc(root, 0, 0);
sort(vals.begin(), vals.end()), reverse(vals.begin(), vals.end());
int64_t total = accumulate(vals.begin(), vals.end(), 0ll);
assert(ans[2] == total - vals[0]);
for (int i = 2; i <= N; ++i) {
ans[i] = (total -= vals[i - 2]);
}
cin >> Q;
while (Q--) {
int x;
cin >> x;
cout << ans[x] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
397 ms |
29560 KB |
Output is correct |
3 |
Correct |
519 ms |
45176 KB |
Output is correct |
4 |
Correct |
400 ms |
28204 KB |
Output is correct |
5 |
Correct |
373 ms |
29812 KB |
Output is correct |
6 |
Correct |
399 ms |
31608 KB |
Output is correct |
7 |
Correct |
328 ms |
29976 KB |
Output is correct |
8 |
Correct |
491 ms |
45048 KB |
Output is correct |
9 |
Correct |
219 ms |
30312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
387 ms |
29560 KB |
Output is correct |
3 |
Correct |
511 ms |
47228 KB |
Output is correct |
4 |
Correct |
383 ms |
28408 KB |
Output is correct |
5 |
Correct |
372 ms |
29684 KB |
Output is correct |
6 |
Correct |
416 ms |
32120 KB |
Output is correct |
7 |
Correct |
243 ms |
30060 KB |
Output is correct |
8 |
Correct |
466 ms |
39108 KB |
Output is correct |
9 |
Correct |
211 ms |
29904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
14 |
Correct |
9 ms |
5504 KB |
Output is correct |
15 |
Correct |
9 ms |
5376 KB |
Output is correct |
16 |
Correct |
10 ms |
5376 KB |
Output is correct |
17 |
Correct |
9 ms |
5376 KB |
Output is correct |
18 |
Correct |
9 ms |
5376 KB |
Output is correct |
19 |
Correct |
9 ms |
5376 KB |
Output is correct |
20 |
Correct |
18 ms |
5376 KB |
Output is correct |
21 |
Correct |
9 ms |
5376 KB |
Output is correct |
22 |
Correct |
9 ms |
5376 KB |
Output is correct |
23 |
Correct |
10 ms |
5376 KB |
Output is correct |
24 |
Correct |
9 ms |
5376 KB |
Output is correct |
25 |
Correct |
10 ms |
5504 KB |
Output is correct |
26 |
Correct |
11 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
397 ms |
29560 KB |
Output is correct |
3 |
Correct |
519 ms |
45176 KB |
Output is correct |
4 |
Correct |
400 ms |
28204 KB |
Output is correct |
5 |
Correct |
373 ms |
29812 KB |
Output is correct |
6 |
Correct |
399 ms |
31608 KB |
Output is correct |
7 |
Correct |
328 ms |
29976 KB |
Output is correct |
8 |
Correct |
491 ms |
45048 KB |
Output is correct |
9 |
Correct |
219 ms |
30312 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
387 ms |
29560 KB |
Output is correct |
12 |
Correct |
511 ms |
47228 KB |
Output is correct |
13 |
Correct |
383 ms |
28408 KB |
Output is correct |
14 |
Correct |
372 ms |
29684 KB |
Output is correct |
15 |
Correct |
416 ms |
32120 KB |
Output is correct |
16 |
Correct |
243 ms |
30060 KB |
Output is correct |
17 |
Correct |
466 ms |
39108 KB |
Output is correct |
18 |
Correct |
211 ms |
29904 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
430 ms |
29536 KB |
Output is correct |
21 |
Correct |
514 ms |
47484 KB |
Output is correct |
22 |
Correct |
375 ms |
28152 KB |
Output is correct |
23 |
Correct |
402 ms |
29800 KB |
Output is correct |
24 |
Correct |
406 ms |
28352 KB |
Output is correct |
25 |
Correct |
405 ms |
29508 KB |
Output is correct |
26 |
Correct |
367 ms |
28408 KB |
Output is correct |
27 |
Correct |
375 ms |
29688 KB |
Output is correct |
28 |
Correct |
401 ms |
31608 KB |
Output is correct |
29 |
Correct |
412 ms |
29816 KB |
Output is correct |
30 |
Correct |
350 ms |
28788 KB |
Output is correct |
31 |
Correct |
310 ms |
30064 KB |
Output is correct |
32 |
Correct |
472 ms |
39672 KB |
Output is correct |
33 |
Correct |
220 ms |
30448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
397 ms |
29560 KB |
Output is correct |
14 |
Correct |
519 ms |
45176 KB |
Output is correct |
15 |
Correct |
400 ms |
28204 KB |
Output is correct |
16 |
Correct |
373 ms |
29812 KB |
Output is correct |
17 |
Correct |
399 ms |
31608 KB |
Output is correct |
18 |
Correct |
328 ms |
29976 KB |
Output is correct |
19 |
Correct |
491 ms |
45048 KB |
Output is correct |
20 |
Correct |
219 ms |
30312 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
387 ms |
29560 KB |
Output is correct |
23 |
Correct |
511 ms |
47228 KB |
Output is correct |
24 |
Correct |
383 ms |
28408 KB |
Output is correct |
25 |
Correct |
372 ms |
29684 KB |
Output is correct |
26 |
Correct |
416 ms |
32120 KB |
Output is correct |
27 |
Correct |
243 ms |
30060 KB |
Output is correct |
28 |
Correct |
466 ms |
39108 KB |
Output is correct |
29 |
Correct |
211 ms |
29904 KB |
Output is correct |
30 |
Correct |
7 ms |
5120 KB |
Output is correct |
31 |
Correct |
9 ms |
5376 KB |
Output is correct |
32 |
Correct |
9 ms |
5504 KB |
Output is correct |
33 |
Correct |
9 ms |
5376 KB |
Output is correct |
34 |
Correct |
10 ms |
5376 KB |
Output is correct |
35 |
Correct |
9 ms |
5376 KB |
Output is correct |
36 |
Correct |
9 ms |
5376 KB |
Output is correct |
37 |
Correct |
9 ms |
5376 KB |
Output is correct |
38 |
Correct |
18 ms |
5376 KB |
Output is correct |
39 |
Correct |
9 ms |
5376 KB |
Output is correct |
40 |
Correct |
9 ms |
5376 KB |
Output is correct |
41 |
Correct |
10 ms |
5376 KB |
Output is correct |
42 |
Correct |
9 ms |
5376 KB |
Output is correct |
43 |
Correct |
10 ms |
5504 KB |
Output is correct |
44 |
Correct |
11 ms |
5376 KB |
Output is correct |
45 |
Correct |
7 ms |
5120 KB |
Output is correct |
46 |
Correct |
430 ms |
29536 KB |
Output is correct |
47 |
Correct |
514 ms |
47484 KB |
Output is correct |
48 |
Correct |
375 ms |
28152 KB |
Output is correct |
49 |
Correct |
402 ms |
29800 KB |
Output is correct |
50 |
Correct |
406 ms |
28352 KB |
Output is correct |
51 |
Correct |
405 ms |
29508 KB |
Output is correct |
52 |
Correct |
367 ms |
28408 KB |
Output is correct |
53 |
Correct |
375 ms |
29688 KB |
Output is correct |
54 |
Correct |
401 ms |
31608 KB |
Output is correct |
55 |
Correct |
412 ms |
29816 KB |
Output is correct |
56 |
Correct |
350 ms |
28788 KB |
Output is correct |
57 |
Correct |
310 ms |
30064 KB |
Output is correct |
58 |
Correct |
472 ms |
39672 KB |
Output is correct |
59 |
Correct |
220 ms |
30448 KB |
Output is correct |
60 |
Correct |
7 ms |
5120 KB |
Output is correct |
61 |
Correct |
425 ms |
32208 KB |
Output is correct |
62 |
Correct |
549 ms |
46840 KB |
Output is correct |
63 |
Correct |
421 ms |
30968 KB |
Output is correct |
64 |
Correct |
425 ms |
32248 KB |
Output is correct |
65 |
Correct |
427 ms |
30916 KB |
Output is correct |
66 |
Correct |
425 ms |
32248 KB |
Output is correct |
67 |
Correct |
405 ms |
30840 KB |
Output is correct |
68 |
Correct |
401 ms |
32376 KB |
Output is correct |
69 |
Correct |
435 ms |
34168 KB |
Output is correct |
70 |
Correct |
447 ms |
32376 KB |
Output is correct |
71 |
Correct |
387 ms |
31476 KB |
Output is correct |
72 |
Correct |
350 ms |
33320 KB |
Output is correct |
73 |
Correct |
491 ms |
41848 KB |
Output is correct |
74 |
Correct |
261 ms |
34544 KB |
Output is correct |