//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
#define ll long long
#define maxn 200005
#define pii pair<int, ll>
#define ff first
#define ss second
#define mod 1000000007
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
vector<pii> adj[maxn];
ll tup[maxn], up[maxn], best[maxn], bind[maxn], ans[maxn];
int par[maxn];
bool done[maxn];
void dfs(int n, int pa) {
par[n] = pa;
for (auto v:adj[n]) {
if (v.ff != pa) {
dfs(v.ff, n);
tup[n] += tup[v.ff] + up[v.ff];
if (v.ss + best[v.ff] > best[n]) {
best[n] = v.ss + best[v.ff];
bind[n] = bind[v.ff] == 0 ? v.ff : bind[v.ff];
}
} else {
up[n] = v.ss;
}
}
}
void reroot(int u, int v, ll w) {
tup[u] -= tup[v] + up[v];
up[u] = w;
tup[v] += tup[u] + up[u];
up[v] = 0;
}
int type = 1;
void solve(int n, int par) {
ans[1] = max(ans[1], tup[n]);
for (auto v:adj[n]) {
if (v.ff != par) {
ll tmp = up[v.ff];
reroot(n, v.ff, v.ss);
solve(v.ff, n);
reroot(v.ff, n, tmp);
}
}
}
ll add = 0, addind = 0;
void getweight(int n, int par, ll cur) {
if (cur > add) {
addind = n;
add = cur;
}
for (auto v:adj[n]) {
if (v.ff != par) {
getweight(v.ff, n, cur + (done[v.ff] ? 0 : v.ss));
}
}
}
int main() {
io
int n;
cin >> n;
ll tot = 0;
for (int i = 0;i < n - 1;i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
tot += c + d;
adj[a].push_back(make_pair(b, c));
adj[b].push_back(make_pair(a, d));
}
int q;
cin >> q;
dfs(1, 0);
solve(1, 0);
int p1 = bind[1];
for (int j = 1;j <= n;j++) tup[j] = 0, up[j] = 0, best[j] = 0, bind[j] = 0;
dfs(p1, 0);
ans[2] = tup[p1] + best[p1];
int p2 = bind[p1], cur = p2;
//p1 and p2 are diameter
while (cur != p1) {
done[cur] = 1;
cur = par[cur];
}
if (n <= 2000) {
for (int i = 3;i <= n;i++) {
add = 0, addind = 0;
getweight(p1, 0, 0);
if (add == 0) {
ans[i] = ans[i - 1];
} else {
ans[i] = ans[i - 1] + add;
cur = addind;
while (!done[cur]) {
done[cur] = 1;
cur = par[cur];
}
}
}
}
for (int i = 0;i < q;i++) {
int t;
cin >> t;
cout << tot - ans[t] << "\n";
}
}
/*
4
1 2 1 2
1 3 3 4
1 4 5 6
1
2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
335 ms |
22508 KB |
Output is correct |
3 |
Correct |
373 ms |
38508 KB |
Output is correct |
4 |
Correct |
318 ms |
22380 KB |
Output is correct |
5 |
Correct |
308 ms |
22496 KB |
Output is correct |
6 |
Correct |
336 ms |
24624 KB |
Output is correct |
7 |
Correct |
265 ms |
21852 KB |
Output is correct |
8 |
Correct |
377 ms |
37724 KB |
Output is correct |
9 |
Correct |
192 ms |
21600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
324 ms |
22636 KB |
Output is correct |
3 |
Correct |
374 ms |
38508 KB |
Output is correct |
4 |
Correct |
322 ms |
22380 KB |
Output is correct |
5 |
Correct |
317 ms |
22368 KB |
Output is correct |
6 |
Correct |
359 ms |
25324 KB |
Output is correct |
7 |
Correct |
214 ms |
21596 KB |
Output is correct |
8 |
Correct |
403 ms |
32876 KB |
Output is correct |
9 |
Correct |
198 ms |
21460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
3 ms |
5100 KB |
Output is correct |
13 |
Correct |
87 ms |
5228 KB |
Output is correct |
14 |
Correct |
95 ms |
5632 KB |
Output is correct |
15 |
Correct |
71 ms |
5356 KB |
Output is correct |
16 |
Correct |
72 ms |
5484 KB |
Output is correct |
17 |
Correct |
74 ms |
5356 KB |
Output is correct |
18 |
Correct |
72 ms |
5228 KB |
Output is correct |
19 |
Correct |
82 ms |
5356 KB |
Output is correct |
20 |
Correct |
68 ms |
5228 KB |
Output is correct |
21 |
Correct |
71 ms |
5356 KB |
Output is correct |
22 |
Correct |
77 ms |
5356 KB |
Output is correct |
23 |
Correct |
71 ms |
5228 KB |
Output is correct |
24 |
Correct |
43 ms |
5356 KB |
Output is correct |
25 |
Correct |
85 ms |
5484 KB |
Output is correct |
26 |
Correct |
41 ms |
5356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
335 ms |
22508 KB |
Output is correct |
3 |
Correct |
373 ms |
38508 KB |
Output is correct |
4 |
Correct |
318 ms |
22380 KB |
Output is correct |
5 |
Correct |
308 ms |
22496 KB |
Output is correct |
6 |
Correct |
336 ms |
24624 KB |
Output is correct |
7 |
Correct |
265 ms |
21852 KB |
Output is correct |
8 |
Correct |
377 ms |
37724 KB |
Output is correct |
9 |
Correct |
192 ms |
21600 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
324 ms |
22636 KB |
Output is correct |
12 |
Correct |
374 ms |
38508 KB |
Output is correct |
13 |
Correct |
322 ms |
22380 KB |
Output is correct |
14 |
Correct |
317 ms |
22368 KB |
Output is correct |
15 |
Correct |
359 ms |
25324 KB |
Output is correct |
16 |
Correct |
214 ms |
21596 KB |
Output is correct |
17 |
Correct |
403 ms |
32876 KB |
Output is correct |
18 |
Correct |
198 ms |
21460 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Incorrect |
336 ms |
22508 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
335 ms |
22508 KB |
Output is correct |
14 |
Correct |
373 ms |
38508 KB |
Output is correct |
15 |
Correct |
318 ms |
22380 KB |
Output is correct |
16 |
Correct |
308 ms |
22496 KB |
Output is correct |
17 |
Correct |
336 ms |
24624 KB |
Output is correct |
18 |
Correct |
265 ms |
21852 KB |
Output is correct |
19 |
Correct |
377 ms |
37724 KB |
Output is correct |
20 |
Correct |
192 ms |
21600 KB |
Output is correct |
21 |
Correct |
4 ms |
5248 KB |
Output is correct |
22 |
Correct |
324 ms |
22636 KB |
Output is correct |
23 |
Correct |
374 ms |
38508 KB |
Output is correct |
24 |
Correct |
322 ms |
22380 KB |
Output is correct |
25 |
Correct |
317 ms |
22368 KB |
Output is correct |
26 |
Correct |
359 ms |
25324 KB |
Output is correct |
27 |
Correct |
214 ms |
21596 KB |
Output is correct |
28 |
Correct |
403 ms |
32876 KB |
Output is correct |
29 |
Correct |
198 ms |
21460 KB |
Output is correct |
30 |
Correct |
3 ms |
5100 KB |
Output is correct |
31 |
Correct |
87 ms |
5228 KB |
Output is correct |
32 |
Correct |
95 ms |
5632 KB |
Output is correct |
33 |
Correct |
71 ms |
5356 KB |
Output is correct |
34 |
Correct |
72 ms |
5484 KB |
Output is correct |
35 |
Correct |
74 ms |
5356 KB |
Output is correct |
36 |
Correct |
72 ms |
5228 KB |
Output is correct |
37 |
Correct |
82 ms |
5356 KB |
Output is correct |
38 |
Correct |
68 ms |
5228 KB |
Output is correct |
39 |
Correct |
71 ms |
5356 KB |
Output is correct |
40 |
Correct |
77 ms |
5356 KB |
Output is correct |
41 |
Correct |
71 ms |
5228 KB |
Output is correct |
42 |
Correct |
43 ms |
5356 KB |
Output is correct |
43 |
Correct |
85 ms |
5484 KB |
Output is correct |
44 |
Correct |
41 ms |
5356 KB |
Output is correct |
45 |
Correct |
4 ms |
5100 KB |
Output is correct |
46 |
Incorrect |
336 ms |
22508 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |