#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb emplace_back
#define int long long
#define ii pair<int,int>
#define ed tuple<int,int,int>
#define FOR(i,a,b) for(int i = a ; i < b ; ++i)
const int N = 2e5 + 5;
priority_queue<int> pq;
vector<ed> g[N];
int ans[N];
int Combi = 0;
bool Q;
ii dfs(int u,int p) {
ii res(0,u);
for(ed e : g[u]) if (get<0>(e) != p) {
int x, y, z;
tie(x, y, z) = e;
ii cur = dfs(x,u); cur.X += y;
res = max(res,cur);
if (Q) Combi += z;
}
return res;
}
int cal(int u,int p) {
int M = 0;
for(ed e : g[u]) if (get<0>(e) != p) {
int x, y, z;
tie(x, y, z) = e;
ans[1] += z;
int t = cal(x,u) + y;
if (t <= M) pq.push(t);
if (t > M) pq.push(M), M = t;
}
return M;
}
void sol(int u,int p) {
if (ans[1] < Combi)
ans[1] = Combi;
for(ed e : g[u]) if (get<0>(e) != p) {
int x, y, z;
tie(x, y, z) = e;
Combi += y - z; sol(x,u);
Combi -= y - z;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int S = 0;
FOR(i,1,n) {
int x, y; cin >> x >> y;
int c, d; cin >> c >> d;
g[x].pb(y,c,d);
g[y].pb(x,d,c);
S += c + d;
}
Q = 1; int L = dfs(1,0).Y; //the optimal result for query e = 2 must have L as one of 2 resort cities
Q = 0; int R = dfs(L,0).Y; //now, the optimal result is (L,R)
pq.push(cal(R,0));
//we gradually add the furthest node from current skeleton tree
//if there was only 1 chosen node, the next node is the farthest node in the outward direction
//if there was at least 2 node chosen, the next node is the second farthest node in the outward direction in some
FOR(i,2,n + 1) {
ans[i] = ans[i - 1] + pq.top();
pq.pop();
}
sol(1,0);
int q; cin >> q;
FOR(i,0,q) {
int x; cin >> x;
cout << S - ans[x] << "\n";
}
}
/*
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
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
14 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
5 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
388 ms |
27920 KB |
Output is correct |
3 |
Correct |
486 ms |
49768 KB |
Output is correct |
4 |
Correct |
352 ms |
26536 KB |
Output is correct |
5 |
Correct |
359 ms |
28448 KB |
Output is correct |
6 |
Correct |
406 ms |
30900 KB |
Output is correct |
7 |
Correct |
302 ms |
28580 KB |
Output is correct |
8 |
Correct |
472 ms |
48872 KB |
Output is correct |
9 |
Correct |
246 ms |
27928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
401 ms |
22032 KB |
Output is correct |
3 |
Correct |
487 ms |
44004 KB |
Output is correct |
4 |
Correct |
377 ms |
22016 KB |
Output is correct |
5 |
Correct |
375 ms |
22560 KB |
Output is correct |
6 |
Correct |
428 ms |
25672 KB |
Output is correct |
7 |
Correct |
270 ms |
21932 KB |
Output is correct |
8 |
Correct |
458 ms |
36152 KB |
Output is correct |
9 |
Correct |
259 ms |
21784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
14 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
5 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
9 ms |
5368 KB |
Output is correct |
14 |
Correct |
9 ms |
5496 KB |
Output is correct |
15 |
Correct |
8 ms |
5368 KB |
Output is correct |
16 |
Correct |
9 ms |
5240 KB |
Output is correct |
17 |
Correct |
9 ms |
5368 KB |
Output is correct |
18 |
Correct |
9 ms |
5368 KB |
Output is correct |
19 |
Correct |
9 ms |
5240 KB |
Output is correct |
20 |
Correct |
9 ms |
5368 KB |
Output is correct |
21 |
Correct |
9 ms |
5368 KB |
Output is correct |
22 |
Correct |
9 ms |
5368 KB |
Output is correct |
23 |
Correct |
8 ms |
5332 KB |
Output is correct |
24 |
Correct |
9 ms |
5352 KB |
Output is correct |
25 |
Correct |
9 ms |
5544 KB |
Output is correct |
26 |
Correct |
9 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
388 ms |
27920 KB |
Output is correct |
3 |
Correct |
486 ms |
49768 KB |
Output is correct |
4 |
Correct |
352 ms |
26536 KB |
Output is correct |
5 |
Correct |
359 ms |
28448 KB |
Output is correct |
6 |
Correct |
406 ms |
30900 KB |
Output is correct |
7 |
Correct |
302 ms |
28580 KB |
Output is correct |
8 |
Correct |
472 ms |
48872 KB |
Output is correct |
9 |
Correct |
246 ms |
27928 KB |
Output is correct |
10 |
Correct |
7 ms |
4984 KB |
Output is correct |
11 |
Correct |
401 ms |
22032 KB |
Output is correct |
12 |
Correct |
487 ms |
44004 KB |
Output is correct |
13 |
Correct |
377 ms |
22016 KB |
Output is correct |
14 |
Correct |
375 ms |
22560 KB |
Output is correct |
15 |
Correct |
428 ms |
25672 KB |
Output is correct |
16 |
Correct |
270 ms |
21932 KB |
Output is correct |
17 |
Correct |
458 ms |
36152 KB |
Output is correct |
18 |
Correct |
259 ms |
21784 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
434 ms |
27996 KB |
Output is correct |
21 |
Correct |
499 ms |
49872 KB |
Output is correct |
22 |
Correct |
378 ms |
26344 KB |
Output is correct |
23 |
Correct |
395 ms |
28136 KB |
Output is correct |
24 |
Correct |
382 ms |
26988 KB |
Output is correct |
25 |
Correct |
403 ms |
28136 KB |
Output is correct |
26 |
Correct |
382 ms |
26844 KB |
Output is correct |
27 |
Correct |
383 ms |
28072 KB |
Output is correct |
28 |
Correct |
456 ms |
30988 KB |
Output is correct |
29 |
Correct |
397 ms |
28520 KB |
Output is correct |
30 |
Correct |
370 ms |
27536 KB |
Output is correct |
31 |
Correct |
317 ms |
28344 KB |
Output is correct |
32 |
Correct |
492 ms |
42732 KB |
Output is correct |
33 |
Correct |
270 ms |
27700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
14 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5112 KB |
Output is correct |
6 |
Correct |
5 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
388 ms |
27920 KB |
Output is correct |
14 |
Correct |
486 ms |
49768 KB |
Output is correct |
15 |
Correct |
352 ms |
26536 KB |
Output is correct |
16 |
Correct |
359 ms |
28448 KB |
Output is correct |
17 |
Correct |
406 ms |
30900 KB |
Output is correct |
18 |
Correct |
302 ms |
28580 KB |
Output is correct |
19 |
Correct |
472 ms |
48872 KB |
Output is correct |
20 |
Correct |
246 ms |
27928 KB |
Output is correct |
21 |
Correct |
7 ms |
4984 KB |
Output is correct |
22 |
Correct |
401 ms |
22032 KB |
Output is correct |
23 |
Correct |
487 ms |
44004 KB |
Output is correct |
24 |
Correct |
377 ms |
22016 KB |
Output is correct |
25 |
Correct |
375 ms |
22560 KB |
Output is correct |
26 |
Correct |
428 ms |
25672 KB |
Output is correct |
27 |
Correct |
270 ms |
21932 KB |
Output is correct |
28 |
Correct |
458 ms |
36152 KB |
Output is correct |
29 |
Correct |
259 ms |
21784 KB |
Output is correct |
30 |
Correct |
6 ms |
4984 KB |
Output is correct |
31 |
Correct |
9 ms |
5368 KB |
Output is correct |
32 |
Correct |
9 ms |
5496 KB |
Output is correct |
33 |
Correct |
8 ms |
5368 KB |
Output is correct |
34 |
Correct |
9 ms |
5240 KB |
Output is correct |
35 |
Correct |
9 ms |
5368 KB |
Output is correct |
36 |
Correct |
9 ms |
5368 KB |
Output is correct |
37 |
Correct |
9 ms |
5240 KB |
Output is correct |
38 |
Correct |
9 ms |
5368 KB |
Output is correct |
39 |
Correct |
9 ms |
5368 KB |
Output is correct |
40 |
Correct |
9 ms |
5368 KB |
Output is correct |
41 |
Correct |
8 ms |
5332 KB |
Output is correct |
42 |
Correct |
9 ms |
5352 KB |
Output is correct |
43 |
Correct |
9 ms |
5544 KB |
Output is correct |
44 |
Correct |
9 ms |
5376 KB |
Output is correct |
45 |
Correct |
6 ms |
5112 KB |
Output is correct |
46 |
Correct |
434 ms |
27996 KB |
Output is correct |
47 |
Correct |
499 ms |
49872 KB |
Output is correct |
48 |
Correct |
378 ms |
26344 KB |
Output is correct |
49 |
Correct |
395 ms |
28136 KB |
Output is correct |
50 |
Correct |
382 ms |
26988 KB |
Output is correct |
51 |
Correct |
403 ms |
28136 KB |
Output is correct |
52 |
Correct |
382 ms |
26844 KB |
Output is correct |
53 |
Correct |
383 ms |
28072 KB |
Output is correct |
54 |
Correct |
456 ms |
30988 KB |
Output is correct |
55 |
Correct |
397 ms |
28520 KB |
Output is correct |
56 |
Correct |
370 ms |
27536 KB |
Output is correct |
57 |
Correct |
317 ms |
28344 KB |
Output is correct |
58 |
Correct |
492 ms |
42732 KB |
Output is correct |
59 |
Correct |
270 ms |
27700 KB |
Output is correct |
60 |
Correct |
6 ms |
4984 KB |
Output is correct |
61 |
Correct |
436 ms |
30536 KB |
Output is correct |
62 |
Correct |
510 ms |
51560 KB |
Output is correct |
63 |
Correct |
426 ms |
29232 KB |
Output is correct |
64 |
Correct |
433 ms |
30796 KB |
Output is correct |
65 |
Correct |
440 ms |
29292 KB |
Output is correct |
66 |
Correct |
423 ms |
30948 KB |
Output is correct |
67 |
Correct |
419 ms |
29372 KB |
Output is correct |
68 |
Correct |
436 ms |
30868 KB |
Output is correct |
69 |
Correct |
451 ms |
33596 KB |
Output is correct |
70 |
Correct |
465 ms |
31124 KB |
Output is correct |
71 |
Correct |
478 ms |
30160 KB |
Output is correct |
72 |
Correct |
363 ms |
31812 KB |
Output is correct |
73 |
Correct |
491 ms |
44828 KB |
Output is correct |
74 |
Correct |
312 ms |
31900 KB |
Output is correct |