#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int MEM = 5e7;
char mem[MEM];
int ptr = 0;
void* operator new(size_t n) {
ptr += n;
return mem + (ptr - n);
}
void operator delete (void*) {}
const int LG = 20, N = 2e5 + 7;
const ll INF = 1e18;
vector <ii> g[N];
int n;
bool used[N], centr[N];
int cnt[N];
void calc(int u, int p) {
cnt[u] = 1;
used[u] = 1;
for (auto e : g[u]) {
int v = e.f, c = e.s;
if (v != p && !centr[v]) {
calc(v, u);
cnt[u] += cnt[v];
}
}
}
int get(int u, int p, int all) {
int mx = -1;
for (auto e : g[u]) {
int v = e.f;
if (v != p && !centr[v]) {
if (mx == -1 || cnt[mx] < cnt[v])
mx = v;
}
}
if (mx == -1 || cnt[mx] * 2 <= all)
return u;
else
return get(mx, u, all);
}
ll out[N], sum[N];
void calc_sum(int u, int p) {
sum[u] = 0;
for (auto e : g[u]) {
int v = e.f, c = e.s;
if (v != p && !centr[v]) {
calc_sum(v, u);
sum[u] += sum[v] + c;
}
}
}
void add_out(int u, int p, ll x) {
out[u] += x;
for (auto e : g[u]) {
int v = e.f, c = e.s;
if (v != p && !centr[v]) {
add_out(v, u, x);
}
}
}
vector <ii> tree[N];
void build(int u, int p) {
tree[u].clear();
for (auto e : g[u]) {
int v = e.f, c = e.s;
if (v != p && !centr[v]) {
tree[u].app(mp(v, c));
build(v, u);
}
}
}
int to[N];
ll h[N];
void ladder(int u, ll cur, ll &sum) {
h[u] = cur;
to[u] = -1;
for (auto e : tree[u]) {
ladder(e.f, cur + e.s, sum);
sum += e.s;
h[u] = max(h[u], h[e.f]);
if (to[u] == -1 || h[to[u]] < h[e.f])
to[u] = e.f;
}
}
void print(int u, int ded, ll cur, vector <pair <ll, int> > &a, bool root) {
if (tree[u].empty())
a.app(mp(cur, ded));
else
a.app(mp(0, ded));
for (auto e : tree[u]) {
int d = ded;
if (root)
d = e.f;
ll c = cur;
if (to[u] != e.f)
c = 0;
c += e.s;
print(e.f, d, c, a, 0);
}
}
ll ans[N];
vector <pair <ll, int> > a;
void solve(int c) {
build(c, c);
ll sum = 0;
ladder(c, 0, sum);
a.clear();
print(c, c, 0, a, 1);
sort(all(a)); reverse(all(a));
ans[1] = min(ans[1], out[c] + sum);
int l = -1;
for (int i = 1; i < a.size(); ++i) {
if (a[i].s != a[0].s) {
l = i;
break;
}
}
sum -= a[0].f;
for (int i = 1; i < a.size(); ++i) {
sum -= a[i].f;
if (l <= i) {
ans[i + 1] = min(ans[i + 1], out[c] + sum);
}
else {
ans[i + 1] = min(ans[i + 1], out[c] + sum + a[i].f - a[l].f);
}
}
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u, v, a, b;
cin >> u >> v >> a >> b;
g[u].app(mp(v, a));
g[v].app(mp(u, b));
}
for (int i = 0; i < N; ++i)
ans[i] = INF;
for (int t = 0; t < LG; ++t) {
memset(used, 0, sizeof used);
for (int i = 1; i <= n; ++i) {
if (!used[i] && !centr[i]) {
calc(i, i);
int c = get(i, i, cnt[i]);
solve(c);
calc_sum(c, c);
for (auto e : g[c]) {
int v = e.f, cost = e.s;
if (!centr[v]) {
int add = 0;
for (auto e : g[v])
if (e.f == c)
add = e.s;
add_out(v, c, sum[c] - sum[v] - cost + add);
}
}
centr[c] = 1;
}
}
}
int q;
cin >> q;
while (q--) {
int k;
cin >> k;
cout << ans[k] << endl;
}
#ifdef HOME
cerr << Time << endl;
#endif
}
Compilation message
designated_cities.cpp: In function 'void calc(int, int)':
designated_cities.cpp:34:22: warning: unused variable 'c' [-Wunused-variable]
int v = e.f, c = e.s;
^
designated_cities.cpp: In function 'void add_out(int, int, long long int)':
designated_cities.cpp:69:22: warning: unused variable 'c' [-Wunused-variable]
int v = e.f, c = e.s;
^
designated_cities.cpp: In function 'void solve(int)':
designated_cities.cpp:133:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < a.size(); ++i) {
~~^~~~~~~~~~
designated_cities.cpp:140:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < a.size(); ++i) {
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11520 KB |
Output is correct |
2 |
Correct |
9 ms |
11520 KB |
Output is correct |
3 |
Correct |
9 ms |
11520 KB |
Output is correct |
4 |
Correct |
9 ms |
11520 KB |
Output is correct |
5 |
Correct |
9 ms |
11520 KB |
Output is correct |
6 |
Correct |
9 ms |
11520 KB |
Output is correct |
7 |
Correct |
9 ms |
11520 KB |
Output is correct |
8 |
Correct |
9 ms |
11520 KB |
Output is correct |
9 |
Correct |
9 ms |
11520 KB |
Output is correct |
10 |
Correct |
9 ms |
11520 KB |
Output is correct |
11 |
Correct |
9 ms |
11520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
11520 KB |
Output is correct |
2 |
Correct |
1209 ms |
34216 KB |
Output is correct |
3 |
Correct |
1929 ms |
43720 KB |
Output is correct |
4 |
Correct |
1288 ms |
39264 KB |
Output is correct |
5 |
Correct |
443 ms |
40700 KB |
Output is correct |
6 |
Correct |
1545 ms |
41824 KB |
Output is correct |
7 |
Correct |
300 ms |
39928 KB |
Output is correct |
8 |
Correct |
1914 ms |
49848 KB |
Output is correct |
9 |
Correct |
198 ms |
40440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11520 KB |
Output is correct |
2 |
Correct |
1251 ms |
34316 KB |
Output is correct |
3 |
Correct |
1935 ms |
44580 KB |
Output is correct |
4 |
Correct |
1276 ms |
39288 KB |
Output is correct |
5 |
Correct |
443 ms |
40824 KB |
Output is correct |
6 |
Correct |
1588 ms |
42404 KB |
Output is correct |
7 |
Correct |
228 ms |
40640 KB |
Output is correct |
8 |
Correct |
1855 ms |
47120 KB |
Output is correct |
9 |
Correct |
203 ms |
40184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11520 KB |
Output is correct |
2 |
Correct |
9 ms |
11520 KB |
Output is correct |
3 |
Correct |
9 ms |
11520 KB |
Output is correct |
4 |
Correct |
9 ms |
11520 KB |
Output is correct |
5 |
Correct |
9 ms |
11520 KB |
Output is correct |
6 |
Correct |
9 ms |
11520 KB |
Output is correct |
7 |
Correct |
9 ms |
11520 KB |
Output is correct |
8 |
Correct |
9 ms |
11520 KB |
Output is correct |
9 |
Correct |
9 ms |
11520 KB |
Output is correct |
10 |
Correct |
9 ms |
11520 KB |
Output is correct |
11 |
Correct |
9 ms |
11520 KB |
Output is correct |
12 |
Correct |
9 ms |
11520 KB |
Output is correct |
13 |
Correct |
13 ms |
11776 KB |
Output is correct |
14 |
Correct |
16 ms |
11904 KB |
Output is correct |
15 |
Correct |
14 ms |
11828 KB |
Output is correct |
16 |
Correct |
14 ms |
11776 KB |
Output is correct |
17 |
Correct |
14 ms |
11752 KB |
Output is correct |
18 |
Correct |
13 ms |
11776 KB |
Output is correct |
19 |
Correct |
13 ms |
11776 KB |
Output is correct |
20 |
Correct |
12 ms |
11776 KB |
Output is correct |
21 |
Correct |
14 ms |
11776 KB |
Output is correct |
22 |
Correct |
14 ms |
11776 KB |
Output is correct |
23 |
Correct |
13 ms |
11776 KB |
Output is correct |
24 |
Correct |
11 ms |
11776 KB |
Output is correct |
25 |
Correct |
14 ms |
11904 KB |
Output is correct |
26 |
Correct |
13 ms |
11776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
11520 KB |
Output is correct |
2 |
Correct |
1209 ms |
34216 KB |
Output is correct |
3 |
Correct |
1929 ms |
43720 KB |
Output is correct |
4 |
Correct |
1288 ms |
39264 KB |
Output is correct |
5 |
Correct |
443 ms |
40700 KB |
Output is correct |
6 |
Correct |
1545 ms |
41824 KB |
Output is correct |
7 |
Correct |
300 ms |
39928 KB |
Output is correct |
8 |
Correct |
1914 ms |
49848 KB |
Output is correct |
9 |
Correct |
198 ms |
40440 KB |
Output is correct |
10 |
Correct |
9 ms |
11520 KB |
Output is correct |
11 |
Correct |
1251 ms |
34316 KB |
Output is correct |
12 |
Correct |
1935 ms |
44580 KB |
Output is correct |
13 |
Correct |
1276 ms |
39288 KB |
Output is correct |
14 |
Correct |
443 ms |
40824 KB |
Output is correct |
15 |
Correct |
1588 ms |
42404 KB |
Output is correct |
16 |
Correct |
228 ms |
40640 KB |
Output is correct |
17 |
Correct |
1855 ms |
47120 KB |
Output is correct |
18 |
Correct |
203 ms |
40184 KB |
Output is correct |
19 |
Correct |
9 ms |
11520 KB |
Output is correct |
20 |
Correct |
1275 ms |
40680 KB |
Output is correct |
21 |
Correct |
1950 ms |
51176 KB |
Output is correct |
22 |
Correct |
1226 ms |
39288 KB |
Output is correct |
23 |
Correct |
1259 ms |
40644 KB |
Output is correct |
24 |
Correct |
1280 ms |
39544 KB |
Output is correct |
25 |
Correct |
1257 ms |
40472 KB |
Output is correct |
26 |
Correct |
1245 ms |
39584 KB |
Output is correct |
27 |
Correct |
434 ms |
40184 KB |
Output is correct |
28 |
Correct |
1567 ms |
41996 KB |
Output is correct |
29 |
Correct |
1251 ms |
40756 KB |
Output is correct |
30 |
Correct |
1043 ms |
39908 KB |
Output is correct |
31 |
Correct |
272 ms |
39672 KB |
Output is correct |
32 |
Correct |
1862 ms |
47480 KB |
Output is correct |
33 |
Correct |
192 ms |
40440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11520 KB |
Output is correct |
2 |
Correct |
9 ms |
11520 KB |
Output is correct |
3 |
Correct |
9 ms |
11520 KB |
Output is correct |
4 |
Correct |
9 ms |
11520 KB |
Output is correct |
5 |
Correct |
9 ms |
11520 KB |
Output is correct |
6 |
Correct |
9 ms |
11520 KB |
Output is correct |
7 |
Correct |
9 ms |
11520 KB |
Output is correct |
8 |
Correct |
9 ms |
11520 KB |
Output is correct |
9 |
Correct |
9 ms |
11520 KB |
Output is correct |
10 |
Correct |
9 ms |
11520 KB |
Output is correct |
11 |
Correct |
9 ms |
11520 KB |
Output is correct |
12 |
Correct |
10 ms |
11520 KB |
Output is correct |
13 |
Correct |
1209 ms |
34216 KB |
Output is correct |
14 |
Correct |
1929 ms |
43720 KB |
Output is correct |
15 |
Correct |
1288 ms |
39264 KB |
Output is correct |
16 |
Correct |
443 ms |
40700 KB |
Output is correct |
17 |
Correct |
1545 ms |
41824 KB |
Output is correct |
18 |
Correct |
300 ms |
39928 KB |
Output is correct |
19 |
Correct |
1914 ms |
49848 KB |
Output is correct |
20 |
Correct |
198 ms |
40440 KB |
Output is correct |
21 |
Correct |
9 ms |
11520 KB |
Output is correct |
22 |
Correct |
1251 ms |
34316 KB |
Output is correct |
23 |
Correct |
1935 ms |
44580 KB |
Output is correct |
24 |
Correct |
1276 ms |
39288 KB |
Output is correct |
25 |
Correct |
443 ms |
40824 KB |
Output is correct |
26 |
Correct |
1588 ms |
42404 KB |
Output is correct |
27 |
Correct |
228 ms |
40640 KB |
Output is correct |
28 |
Correct |
1855 ms |
47120 KB |
Output is correct |
29 |
Correct |
203 ms |
40184 KB |
Output is correct |
30 |
Correct |
9 ms |
11520 KB |
Output is correct |
31 |
Correct |
13 ms |
11776 KB |
Output is correct |
32 |
Correct |
16 ms |
11904 KB |
Output is correct |
33 |
Correct |
14 ms |
11828 KB |
Output is correct |
34 |
Correct |
14 ms |
11776 KB |
Output is correct |
35 |
Correct |
14 ms |
11752 KB |
Output is correct |
36 |
Correct |
13 ms |
11776 KB |
Output is correct |
37 |
Correct |
13 ms |
11776 KB |
Output is correct |
38 |
Correct |
12 ms |
11776 KB |
Output is correct |
39 |
Correct |
14 ms |
11776 KB |
Output is correct |
40 |
Correct |
14 ms |
11776 KB |
Output is correct |
41 |
Correct |
13 ms |
11776 KB |
Output is correct |
42 |
Correct |
11 ms |
11776 KB |
Output is correct |
43 |
Correct |
14 ms |
11904 KB |
Output is correct |
44 |
Correct |
13 ms |
11776 KB |
Output is correct |
45 |
Correct |
9 ms |
11520 KB |
Output is correct |
46 |
Correct |
1275 ms |
40680 KB |
Output is correct |
47 |
Correct |
1950 ms |
51176 KB |
Output is correct |
48 |
Correct |
1226 ms |
39288 KB |
Output is correct |
49 |
Correct |
1259 ms |
40644 KB |
Output is correct |
50 |
Correct |
1280 ms |
39544 KB |
Output is correct |
51 |
Correct |
1257 ms |
40472 KB |
Output is correct |
52 |
Correct |
1245 ms |
39584 KB |
Output is correct |
53 |
Correct |
434 ms |
40184 KB |
Output is correct |
54 |
Correct |
1567 ms |
41996 KB |
Output is correct |
55 |
Correct |
1251 ms |
40756 KB |
Output is correct |
56 |
Correct |
1043 ms |
39908 KB |
Output is correct |
57 |
Correct |
272 ms |
39672 KB |
Output is correct |
58 |
Correct |
1862 ms |
47480 KB |
Output is correct |
59 |
Correct |
192 ms |
40440 KB |
Output is correct |
60 |
Correct |
10 ms |
11520 KB |
Output is correct |
61 |
Correct |
1294 ms |
43260 KB |
Output is correct |
62 |
Correct |
1935 ms |
51600 KB |
Output is correct |
63 |
Correct |
1257 ms |
42088 KB |
Output is correct |
64 |
Correct |
1228 ms |
43384 KB |
Output is correct |
65 |
Correct |
1245 ms |
41848 KB |
Output is correct |
66 |
Correct |
1313 ms |
43148 KB |
Output is correct |
67 |
Correct |
1281 ms |
42104 KB |
Output is correct |
68 |
Correct |
496 ms |
43128 KB |
Output is correct |
69 |
Correct |
1547 ms |
44792 KB |
Output is correct |
70 |
Correct |
1310 ms |
43260 KB |
Output is correct |
71 |
Correct |
1051 ms |
42616 KB |
Output is correct |
72 |
Correct |
333 ms |
43128 KB |
Output is correct |
73 |
Correct |
1822 ms |
49744 KB |
Output is correct |
74 |
Correct |
235 ms |
44664 KB |
Output is correct |