#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, int> pi;
struct edge_t {
int to, w, w_rev;
edge_t(int x, int y, int z) : to(x), w(y), w_rev(z) {}
};
const int N = (int)2e5 + 10;
int n, pa[N], pcost[N];
vector<edge_t> aj[N];
long long ans[N], r1[N];
pi maxDep[N];
void dfs5(int v, int p) {
maxDep[v] = pi(0, v);
for(const edge_t& e : aj[v]) {
if(e.to == p)
continue;
pa[e.to] = v;
pcost[e.to] = e.w;
dfs5(e.to, v);
pi tmp = maxDep[e.to];
tmp.first += e.w;
if(maxDep[v] < tmp)
maxDep[v] = tmp;
}
}
void solve(int r) {
dfs5(r, -1);
priority_queue<pi> q;
for(const edge_t& e : aj[r]) {
pi tmp = maxDep[e.to];
tmp.first += e.w;
q.push(tmp);
}
static bool vst[N];
long long tot = r1[r];
int cur = 2;
vst[r] = 1;
for(; cur <= n && !q.empty(); ++cur) {
pi tmp = q.top();
q.pop();
tot += tmp.first;
ans[cur] = tot;
for(int x = tmp.second; !vst[x]; x = pa[x]) {
vst[x] = 1;
for(const edge_t& e : aj[x]) {
if(e.to == pa[x] || vst[e.to])
continue;
pi tmp = maxDep[e.to];
tmp.first += e.w;
q.push(tmp);
}
}
}
for(; cur <= n; ++cur)
ans[cur] = tot;
}
pi dfs3(int v, int p, tuple<long long, int, int>& res) {
pi mx(r1[v], v), mx2(-1, -1);
for(const edge_t& e : aj[v]) {
if(e.to == p)
continue;
pi tmp = dfs3(e.to, v, res);
tmp.first += e.w + e.w_rev;
if(mx < tmp)
mx2 = mx, mx = tmp;
else if(mx2 < tmp)
mx2 = tmp;
}
if(mx2.first != -1 && get<0> (res) < mx.first + mx2.first) {
res = { mx.first + mx2.first, mx.second, mx2.second };
}
return mx;
}
pair<int, int> solve2() {
tuple<long long, int, int> res = { -1, -1, -1 };
dfs3(1, -1, res);
ans[2] = get<0> (res) / 2;
return make_pair( get<1>(res), get<2>(res) );
}
void dfs2(int v, int p, long long val) {
for(const edge_t& e : aj[v]) {
if(e.to == p)
continue;
dfs2(e.to, v, val + r1[v] - r1[e.to] - e.w_rev + e.w);
}
r1[v] += val;
}
void dfs1(int v, int p) {
r1[v] = 0;
for(const edge_t& e : aj[v]) {
if(e.to == p)
continue;
dfs1(e.to, v);
r1[v] += r1[e.to] + e.w_rev;
}
}
void solve1() {
dfs1(1, -1);
dfs2(1, -1, 0);
ans[1] = *max_element(r1 + 1, r1 + 1 + n);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
long long tot = 0;
for(int i = 1, u, v, x, y; i < n; ++i) {
cin >> u >> v >> x >> y;
aj[u].emplace_back(v, x, y);
aj[v].emplace_back(u, y, x);
tot += x + y;
}
solve1();
auto p = solve2();
solve(p.first);
int q;
cin >> q;
while(q--) {
int i;
cin >> i;
cout << tot - ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5168 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
154 ms |
28660 KB |
Output is correct |
3 |
Correct |
163 ms |
38480 KB |
Output is correct |
4 |
Correct |
139 ms |
26416 KB |
Output is correct |
5 |
Correct |
178 ms |
29132 KB |
Output is correct |
6 |
Correct |
161 ms |
30556 KB |
Output is correct |
7 |
Correct |
128 ms |
30332 KB |
Output is correct |
8 |
Correct |
161 ms |
39400 KB |
Output is correct |
9 |
Correct |
117 ms |
33292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
132 ms |
28760 KB |
Output is correct |
3 |
Correct |
161 ms |
40492 KB |
Output is correct |
4 |
Correct |
152 ms |
26384 KB |
Output is correct |
5 |
Correct |
128 ms |
29184 KB |
Output is correct |
6 |
Correct |
141 ms |
30660 KB |
Output is correct |
7 |
Correct |
119 ms |
32760 KB |
Output is correct |
8 |
Correct |
149 ms |
36292 KB |
Output is correct |
9 |
Correct |
119 ms |
33012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5168 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
5208 KB |
Output is correct |
13 |
Correct |
4 ms |
5464 KB |
Output is correct |
14 |
Correct |
3 ms |
5468 KB |
Output is correct |
15 |
Correct |
3 ms |
5468 KB |
Output is correct |
16 |
Correct |
3 ms |
5460 KB |
Output is correct |
17 |
Correct |
4 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5464 KB |
Output is correct |
19 |
Correct |
3 ms |
5212 KB |
Output is correct |
20 |
Correct |
3 ms |
5468 KB |
Output is correct |
21 |
Correct |
4 ms |
5464 KB |
Output is correct |
22 |
Correct |
3 ms |
5468 KB |
Output is correct |
23 |
Correct |
5 ms |
5308 KB |
Output is correct |
24 |
Correct |
3 ms |
5468 KB |
Output is correct |
25 |
Correct |
5 ms |
5468 KB |
Output is correct |
26 |
Correct |
5 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
154 ms |
28660 KB |
Output is correct |
3 |
Correct |
163 ms |
38480 KB |
Output is correct |
4 |
Correct |
139 ms |
26416 KB |
Output is correct |
5 |
Correct |
178 ms |
29132 KB |
Output is correct |
6 |
Correct |
161 ms |
30556 KB |
Output is correct |
7 |
Correct |
128 ms |
30332 KB |
Output is correct |
8 |
Correct |
161 ms |
39400 KB |
Output is correct |
9 |
Correct |
117 ms |
33292 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
132 ms |
28760 KB |
Output is correct |
12 |
Correct |
161 ms |
40492 KB |
Output is correct |
13 |
Correct |
152 ms |
26384 KB |
Output is correct |
14 |
Correct |
128 ms |
29184 KB |
Output is correct |
15 |
Correct |
141 ms |
30660 KB |
Output is correct |
16 |
Correct |
119 ms |
32760 KB |
Output is correct |
17 |
Correct |
149 ms |
36292 KB |
Output is correct |
18 |
Correct |
119 ms |
33012 KB |
Output is correct |
19 |
Correct |
3 ms |
4956 KB |
Output is correct |
20 |
Correct |
167 ms |
28780 KB |
Output is correct |
21 |
Correct |
163 ms |
40656 KB |
Output is correct |
22 |
Correct |
132 ms |
26420 KB |
Output is correct |
23 |
Correct |
148 ms |
28868 KB |
Output is correct |
24 |
Correct |
142 ms |
27588 KB |
Output is correct |
25 |
Correct |
140 ms |
28768 KB |
Output is correct |
26 |
Correct |
195 ms |
27592 KB |
Output is correct |
27 |
Correct |
142 ms |
28804 KB |
Output is correct |
28 |
Correct |
149 ms |
30408 KB |
Output is correct |
29 |
Correct |
139 ms |
28464 KB |
Output is correct |
30 |
Correct |
171 ms |
28060 KB |
Output is correct |
31 |
Correct |
128 ms |
30720 KB |
Output is correct |
32 |
Correct |
162 ms |
36840 KB |
Output is correct |
33 |
Correct |
119 ms |
33268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5168 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
154 ms |
28660 KB |
Output is correct |
14 |
Correct |
163 ms |
38480 KB |
Output is correct |
15 |
Correct |
139 ms |
26416 KB |
Output is correct |
16 |
Correct |
178 ms |
29132 KB |
Output is correct |
17 |
Correct |
161 ms |
30556 KB |
Output is correct |
18 |
Correct |
128 ms |
30332 KB |
Output is correct |
19 |
Correct |
161 ms |
39400 KB |
Output is correct |
20 |
Correct |
117 ms |
33292 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
132 ms |
28760 KB |
Output is correct |
23 |
Correct |
161 ms |
40492 KB |
Output is correct |
24 |
Correct |
152 ms |
26384 KB |
Output is correct |
25 |
Correct |
128 ms |
29184 KB |
Output is correct |
26 |
Correct |
141 ms |
30660 KB |
Output is correct |
27 |
Correct |
119 ms |
32760 KB |
Output is correct |
28 |
Correct |
149 ms |
36292 KB |
Output is correct |
29 |
Correct |
119 ms |
33012 KB |
Output is correct |
30 |
Correct |
2 ms |
5208 KB |
Output is correct |
31 |
Correct |
4 ms |
5464 KB |
Output is correct |
32 |
Correct |
3 ms |
5468 KB |
Output is correct |
33 |
Correct |
3 ms |
5468 KB |
Output is correct |
34 |
Correct |
3 ms |
5460 KB |
Output is correct |
35 |
Correct |
4 ms |
5212 KB |
Output is correct |
36 |
Correct |
4 ms |
5464 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5468 KB |
Output is correct |
39 |
Correct |
4 ms |
5464 KB |
Output is correct |
40 |
Correct |
3 ms |
5468 KB |
Output is correct |
41 |
Correct |
5 ms |
5308 KB |
Output is correct |
42 |
Correct |
3 ms |
5468 KB |
Output is correct |
43 |
Correct |
5 ms |
5468 KB |
Output is correct |
44 |
Correct |
5 ms |
5468 KB |
Output is correct |
45 |
Correct |
3 ms |
4956 KB |
Output is correct |
46 |
Correct |
167 ms |
28780 KB |
Output is correct |
47 |
Correct |
163 ms |
40656 KB |
Output is correct |
48 |
Correct |
132 ms |
26420 KB |
Output is correct |
49 |
Correct |
148 ms |
28868 KB |
Output is correct |
50 |
Correct |
142 ms |
27588 KB |
Output is correct |
51 |
Correct |
140 ms |
28768 KB |
Output is correct |
52 |
Correct |
195 ms |
27592 KB |
Output is correct |
53 |
Correct |
142 ms |
28804 KB |
Output is correct |
54 |
Correct |
149 ms |
30408 KB |
Output is correct |
55 |
Correct |
139 ms |
28464 KB |
Output is correct |
56 |
Correct |
171 ms |
28060 KB |
Output is correct |
57 |
Correct |
128 ms |
30720 KB |
Output is correct |
58 |
Correct |
162 ms |
36840 KB |
Output is correct |
59 |
Correct |
119 ms |
33268 KB |
Output is correct |
60 |
Correct |
2 ms |
4952 KB |
Output is correct |
61 |
Correct |
181 ms |
30596 KB |
Output is correct |
62 |
Correct |
187 ms |
40216 KB |
Output is correct |
63 |
Correct |
156 ms |
29104 KB |
Output is correct |
64 |
Correct |
168 ms |
30528 KB |
Output is correct |
65 |
Correct |
160 ms |
29248 KB |
Output is correct |
66 |
Correct |
158 ms |
30544 KB |
Output is correct |
67 |
Correct |
142 ms |
29272 KB |
Output is correct |
68 |
Correct |
153 ms |
30648 KB |
Output is correct |
69 |
Correct |
163 ms |
32044 KB |
Output is correct |
70 |
Correct |
165 ms |
30108 KB |
Output is correct |
71 |
Correct |
150 ms |
30004 KB |
Output is correct |
72 |
Correct |
138 ms |
32012 KB |
Output is correct |
73 |
Correct |
190 ms |
37780 KB |
Output is correct |
74 |
Correct |
132 ms |
34328 KB |
Output is correct |