#include<bits/stdc++.h>
#define ll long long
#define int long long
const int nmax = 2e5 + 5, N = 1e5;
const ll oo = 1e18 + 1, base = 311;
const int lg = 19, M = 10;
const ll mod = 998244353, mod2 = 1e9 + 5277;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
int n, q;
map<int, int> mp[nmax];
ll f[nmax], rev[nmax];
vector<int> adj[nmax], leaf;
int par[nmax];
void dfs_1(int u, int p){
bool ok = 1;
for(auto v : adj[u]){
if(v == p) continue;
rev[v] = rev[u] + mp[u][v];
dfs_1(v, u);
f[u] += f[v] + mp[u][v];
par[v] = u;
ok = 0;
}
if(ok) leaf.push_back(u);
}
ll pre[nmax], suf[nmax], g[nmax];
struct node{
int u,v, x, y;
}a[nmax];
void dfs_2(int u, int p){
int k = adj[u].size();
pre[0] = suf[k + 1] = 0;
for(int i = 1; i <= k; ++i){
int v = adj[u][i -1];
pre[i] = pre[i - 1];
if(v == p) continue;
pre[i] += mp[u][v] + f[v];
}
for(int i = k; i >= 1; --i){
int v = adj[u][i - 1];
suf[i] = suf[i + 1];
if(v == p) continue;
suf[i] += mp[u][v] + f[v];
}
for(int i = 1; i <= k; ++i){
int v = adj[u][i - 1];
if(v == p) continue;
g[v] = g[u] + mp[v][u] + pre[i - 1] + suf[i + 1];
}
for(auto v: adj[u]){
if(v == p) continue;
dfs_2(v, u);
}
}
ll sum = 0;
int h[nmax], one[nmax];
ll dp[nmax], nchain = 1;
void dfs_3(int u, int p){
h[u] = 0;
for(auto v : adj[u]){
if(v == p) continue;
dfs_3(v, u);
h[u] = max(h[v] + mp[v][u], h[u]);
}
}
void dfs_4(int u, int p){
for(auto v : adj[u]){
one[v] = h[v] + mp[v][u];
}
sort(adj[u].begin(), adj[u].end(), [](int a, int b){
return one[a] > one[b];
});
int idx = -1;
for(auto v : adj[u]){
if(v == p) continue;
idx = v;
break;
}
if(idx != -1){
dp[nchain] += mp[idx][u];
dfs_4(idx, u);
}
for(auto v : adj[u]){
if(v == p || idx == v) continue;
++nchain;
dp[nchain] += mp[v][u];
dfs_4(v, u);
}
}
ll S, T, dcu[nmax], ma = -oo, two[nmax];
void dfs(int u, int p){
dcu[u] = h[u] = u;
for(auto v : adj[u]){
if(v == p) continue;
rev[v] = rev[u] + mp[v][u];
two[v] = two[u] + mp[u][v];
dfs(v, u);
int x = dcu[v];
int y = dcu[u];
if(g[x] + two[x] > g[y] + two[y]) dcu[u] = x;
x = h[u];
y = h[v];
if(rev[x] < rev[y]) h[u] = y;
}
for(auto v : adj[u]){
if(v == p) continue;
// cout << dcu[v] << ' ';
}
int k = adj[u].size();
pre[0] = suf[k + 1] = 0;
for(int i = 1; i <= k; ++i){
int v = adj[u][i - 1];
pre[i] = pre[i - 1];
if(v == p) continue;
if(pre[i] == 0) pre[i] = h[v];
else{
int x = pre[i];
if(rev[x] < rev[h[v]]) pre[i] = h[v];
}
}
for(int i = k; i >= 1; --i){
int v = adj[u][i - 1];
suf[i] = suf[i + 1];
if(v == p) continue;
if(suf[i] == 0) suf[i] = h[v];
else{
int x = suf[i];
if(rev[x] < rev[h[v]]) suf[i] = h[v];
}
}
for(int i = 1; i <= k; ++i){
int v = adj[u][i - 1];
if(v == p) continue;
int x = dcu[v];
int y = pre[i - 1];
if(y > 0){
int cost = g[x] + two[x] - two[u] + rev[y] -rev[u];
// if(u== 1)cout << v << ' ' << x << ' ' << y << ' ' << cost << endl;
if(cost > ma){
ma = cost;
S = x;
T = y;
}
}
y = suf[i + 1];
if(y > 0){
int cost = g[x] + two[x] - two[u] + rev[y] -rev[u];
// if(u == 1)cout << v << ' ' << x << ' ' << y << ' ' << cost << endl;
if(cost > ma){
ma = cost;
S = x;
T = y;
}
}
}
}
ll ans[nmax];
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n;
sum = 0;
for(int i = 1, u, v, x, y; i < n; ++i){
cin >> u >> v >> x >> y;
// cout << u << ' ' << v << endl;
adj[u].push_back(v);
adj[v].push_back(u);
mp[u][v] = y;
mp[v][u] = x;
sum += x + y;
a[i] = {u, v, x, y};
}
dfs_1(1, -1);
dfs_2(1, -1);
ans[1] = oo;
for(int i = 1; i <= n; ++i){
ans[1] = min(ans[1], sum - f[i] - g[i]);
}
sort(leaf.begin(), leaf.end(), [](int a, int b){
return g[a] + rev[a] > g[b] + rev[b];
});
S = 1, T = leaf[0], ma = g[T];
int x = T;
while(x > 1){
ma += mp[par[x]][x];
x = par[x];
}
dfs(1, -1);
// cout << S << ' ' << T<< endl;
dfs_3(S, -1);
dfs_4(S, -1);
sort(dp + 1, dp + 1 + nchain, greater<ll>());
ans[2] = sum - ma;
for(int i = 3; i <= n; ++i){
ans[i] = ans[i - 1] - dp[i - 1];
}
cin >> q;
while(q--){
int x;
cin >> x;
cout << ans[x] << endl;
}
}
/*
5
2 4 8 4
3 4 2 2
1 2 7 6
5 4 10 10
1
2
*/
Compilation message
designated_cities.cpp:170:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
170 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15404 KB |
Output is correct |
2 |
Correct |
4 ms |
15448 KB |
Output is correct |
3 |
Correct |
5 ms |
15488 KB |
Output is correct |
4 |
Correct |
4 ms |
15700 KB |
Output is correct |
5 |
Correct |
6 ms |
15452 KB |
Output is correct |
6 |
Correct |
4 ms |
15452 KB |
Output is correct |
7 |
Correct |
4 ms |
15452 KB |
Output is correct |
8 |
Correct |
4 ms |
15408 KB |
Output is correct |
9 |
Correct |
4 ms |
15452 KB |
Output is correct |
10 |
Correct |
4 ms |
15452 KB |
Output is correct |
11 |
Correct |
4 ms |
15452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15448 KB |
Output is correct |
2 |
Correct |
402 ms |
74964 KB |
Output is correct |
3 |
Correct |
480 ms |
95316 KB |
Output is correct |
4 |
Correct |
363 ms |
73668 KB |
Output is correct |
5 |
Correct |
470 ms |
75560 KB |
Output is correct |
6 |
Correct |
424 ms |
78100 KB |
Output is correct |
7 |
Correct |
617 ms |
77384 KB |
Output is correct |
8 |
Correct |
492 ms |
96292 KB |
Output is correct |
9 |
Correct |
806 ms |
80004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
15448 KB |
Output is correct |
2 |
Correct |
391 ms |
75204 KB |
Output is correct |
3 |
Correct |
478 ms |
99304 KB |
Output is correct |
4 |
Correct |
412 ms |
73668 KB |
Output is correct |
5 |
Correct |
453 ms |
75564 KB |
Output is correct |
6 |
Correct |
406 ms |
78536 KB |
Output is correct |
7 |
Correct |
709 ms |
79676 KB |
Output is correct |
8 |
Correct |
454 ms |
88604 KB |
Output is correct |
9 |
Correct |
778 ms |
79420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15404 KB |
Output is correct |
2 |
Correct |
4 ms |
15448 KB |
Output is correct |
3 |
Correct |
5 ms |
15488 KB |
Output is correct |
4 |
Correct |
4 ms |
15700 KB |
Output is correct |
5 |
Correct |
6 ms |
15452 KB |
Output is correct |
6 |
Correct |
4 ms |
15452 KB |
Output is correct |
7 |
Correct |
4 ms |
15452 KB |
Output is correct |
8 |
Correct |
4 ms |
15408 KB |
Output is correct |
9 |
Correct |
4 ms |
15452 KB |
Output is correct |
10 |
Correct |
4 ms |
15452 KB |
Output is correct |
11 |
Correct |
4 ms |
15452 KB |
Output is correct |
12 |
Correct |
4 ms |
15452 KB |
Output is correct |
13 |
Correct |
9 ms |
16040 KB |
Output is correct |
14 |
Correct |
6 ms |
16220 KB |
Output is correct |
15 |
Correct |
6 ms |
16040 KB |
Output is correct |
16 |
Correct |
7 ms |
15964 KB |
Output is correct |
17 |
Correct |
6 ms |
15964 KB |
Output is correct |
18 |
Correct |
6 ms |
15964 KB |
Output is correct |
19 |
Correct |
6 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
16128 KB |
Output is correct |
21 |
Correct |
7 ms |
15980 KB |
Output is correct |
22 |
Correct |
6 ms |
16104 KB |
Output is correct |
23 |
Correct |
7 ms |
15964 KB |
Output is correct |
24 |
Correct |
7 ms |
15964 KB |
Output is correct |
25 |
Correct |
6 ms |
16220 KB |
Output is correct |
26 |
Correct |
6 ms |
15936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15448 KB |
Output is correct |
2 |
Correct |
402 ms |
74964 KB |
Output is correct |
3 |
Correct |
480 ms |
95316 KB |
Output is correct |
4 |
Correct |
363 ms |
73668 KB |
Output is correct |
5 |
Correct |
470 ms |
75560 KB |
Output is correct |
6 |
Correct |
424 ms |
78100 KB |
Output is correct |
7 |
Correct |
617 ms |
77384 KB |
Output is correct |
8 |
Correct |
492 ms |
96292 KB |
Output is correct |
9 |
Correct |
806 ms |
80004 KB |
Output is correct |
10 |
Correct |
5 ms |
15448 KB |
Output is correct |
11 |
Correct |
391 ms |
75204 KB |
Output is correct |
12 |
Correct |
478 ms |
99304 KB |
Output is correct |
13 |
Correct |
412 ms |
73668 KB |
Output is correct |
14 |
Correct |
453 ms |
75564 KB |
Output is correct |
15 |
Correct |
406 ms |
78536 KB |
Output is correct |
16 |
Correct |
709 ms |
79676 KB |
Output is correct |
17 |
Correct |
454 ms |
88604 KB |
Output is correct |
18 |
Correct |
778 ms |
79420 KB |
Output is correct |
19 |
Correct |
4 ms |
15452 KB |
Output is correct |
20 |
Correct |
392 ms |
75056 KB |
Output is correct |
21 |
Correct |
485 ms |
99792 KB |
Output is correct |
22 |
Correct |
382 ms |
73672 KB |
Output is correct |
23 |
Correct |
406 ms |
75120 KB |
Output is correct |
24 |
Correct |
398 ms |
73928 KB |
Output is correct |
25 |
Correct |
401 ms |
74944 KB |
Output is correct |
26 |
Correct |
390 ms |
73924 KB |
Output is correct |
27 |
Correct |
450 ms |
75536 KB |
Output is correct |
28 |
Correct |
417 ms |
77936 KB |
Output is correct |
29 |
Correct |
401 ms |
74952 KB |
Output is correct |
30 |
Correct |
455 ms |
75080 KB |
Output is correct |
31 |
Correct |
662 ms |
78068 KB |
Output is correct |
32 |
Correct |
482 ms |
89664 KB |
Output is correct |
33 |
Correct |
753 ms |
79868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15404 KB |
Output is correct |
2 |
Correct |
4 ms |
15448 KB |
Output is correct |
3 |
Correct |
5 ms |
15488 KB |
Output is correct |
4 |
Correct |
4 ms |
15700 KB |
Output is correct |
5 |
Correct |
6 ms |
15452 KB |
Output is correct |
6 |
Correct |
4 ms |
15452 KB |
Output is correct |
7 |
Correct |
4 ms |
15452 KB |
Output is correct |
8 |
Correct |
4 ms |
15408 KB |
Output is correct |
9 |
Correct |
4 ms |
15452 KB |
Output is correct |
10 |
Correct |
4 ms |
15452 KB |
Output is correct |
11 |
Correct |
4 ms |
15452 KB |
Output is correct |
12 |
Correct |
4 ms |
15448 KB |
Output is correct |
13 |
Correct |
402 ms |
74964 KB |
Output is correct |
14 |
Correct |
480 ms |
95316 KB |
Output is correct |
15 |
Correct |
363 ms |
73668 KB |
Output is correct |
16 |
Correct |
470 ms |
75560 KB |
Output is correct |
17 |
Correct |
424 ms |
78100 KB |
Output is correct |
18 |
Correct |
617 ms |
77384 KB |
Output is correct |
19 |
Correct |
492 ms |
96292 KB |
Output is correct |
20 |
Correct |
806 ms |
80004 KB |
Output is correct |
21 |
Correct |
5 ms |
15448 KB |
Output is correct |
22 |
Correct |
391 ms |
75204 KB |
Output is correct |
23 |
Correct |
478 ms |
99304 KB |
Output is correct |
24 |
Correct |
412 ms |
73668 KB |
Output is correct |
25 |
Correct |
453 ms |
75564 KB |
Output is correct |
26 |
Correct |
406 ms |
78536 KB |
Output is correct |
27 |
Correct |
709 ms |
79676 KB |
Output is correct |
28 |
Correct |
454 ms |
88604 KB |
Output is correct |
29 |
Correct |
778 ms |
79420 KB |
Output is correct |
30 |
Correct |
4 ms |
15452 KB |
Output is correct |
31 |
Correct |
9 ms |
16040 KB |
Output is correct |
32 |
Correct |
6 ms |
16220 KB |
Output is correct |
33 |
Correct |
6 ms |
16040 KB |
Output is correct |
34 |
Correct |
7 ms |
15964 KB |
Output is correct |
35 |
Correct |
6 ms |
15964 KB |
Output is correct |
36 |
Correct |
6 ms |
15964 KB |
Output is correct |
37 |
Correct |
6 ms |
15964 KB |
Output is correct |
38 |
Correct |
7 ms |
16128 KB |
Output is correct |
39 |
Correct |
7 ms |
15980 KB |
Output is correct |
40 |
Correct |
6 ms |
16104 KB |
Output is correct |
41 |
Correct |
7 ms |
15964 KB |
Output is correct |
42 |
Correct |
7 ms |
15964 KB |
Output is correct |
43 |
Correct |
6 ms |
16220 KB |
Output is correct |
44 |
Correct |
6 ms |
15936 KB |
Output is correct |
45 |
Correct |
4 ms |
15452 KB |
Output is correct |
46 |
Correct |
392 ms |
75056 KB |
Output is correct |
47 |
Correct |
485 ms |
99792 KB |
Output is correct |
48 |
Correct |
382 ms |
73672 KB |
Output is correct |
49 |
Correct |
406 ms |
75120 KB |
Output is correct |
50 |
Correct |
398 ms |
73928 KB |
Output is correct |
51 |
Correct |
401 ms |
74944 KB |
Output is correct |
52 |
Correct |
390 ms |
73924 KB |
Output is correct |
53 |
Correct |
450 ms |
75536 KB |
Output is correct |
54 |
Correct |
417 ms |
77936 KB |
Output is correct |
55 |
Correct |
401 ms |
74952 KB |
Output is correct |
56 |
Correct |
455 ms |
75080 KB |
Output is correct |
57 |
Correct |
662 ms |
78068 KB |
Output is correct |
58 |
Correct |
482 ms |
89664 KB |
Output is correct |
59 |
Correct |
753 ms |
79868 KB |
Output is correct |
60 |
Correct |
4 ms |
15272 KB |
Output is correct |
61 |
Correct |
426 ms |
77740 KB |
Output is correct |
62 |
Correct |
466 ms |
97104 KB |
Output is correct |
63 |
Correct |
388 ms |
76348 KB |
Output is correct |
64 |
Correct |
415 ms |
77768 KB |
Output is correct |
65 |
Correct |
419 ms |
76228 KB |
Output is correct |
66 |
Correct |
426 ms |
77760 KB |
Output is correct |
67 |
Correct |
397 ms |
76232 KB |
Output is correct |
68 |
Correct |
465 ms |
78288 KB |
Output is correct |
69 |
Correct |
467 ms |
80400 KB |
Output is correct |
70 |
Correct |
436 ms |
77324 KB |
Output is correct |
71 |
Correct |
439 ms |
77888 KB |
Output is correct |
72 |
Correct |
605 ms |
81020 KB |
Output is correct |
73 |
Correct |
483 ms |
90576 KB |
Output is correct |
74 |
Correct |
768 ms |
84112 KB |
Output is correct |