#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int N, Q, sz[200005], pre[200005], vis[200005];
ll tot, sum, dp[200005], val[200005], ans[200005], sum1[200005];
vector<pair<int, pll>> E[200005];
vector<int> cur;
vector<pll> leaf;
void calcSz(int u)
{
vis[u] = sz[u] = 1;
cur.emplace_back(u);
for (auto [v, p] : E[u])
if (!vis[v])
{
pre[v] = u;
calcSz(v);
sz[u] += sz[v];
}
}
void calcDP(int u, int r)
{
vis[u] = 1;
int mx = 0;
for (auto [v, p] : E[u])
if (!vis[v])
{
pre[v] = (r == 0 ? v : r);
calcDP(v, (r == 0 ? v : r));
val[dp[v]] += p.F;
if (val[dp[v]] > val[mx])
mx = dp[v];
}
dp[u] = (mx == 0 ? u : mx);
}
void centroid(int r)
{
cur.clear();
pre[r] = 0;
calcSz(r);
int c = 0;
for (auto i : cur)
{
vis[i] = 0;
bool b = (sz[r] - sz[i]) <= sz[r] / 2;
for (auto [j, p] : E[i])
if (j != pre[i] && !vis[j])
b &= sz[j] <= sz[r] / 2;
if (b)
c = i;
}
// cerr << "centroid dec " << c << '\n';
// for (auto i : cur)
// cout << i << ' ';
// cout << '\n';
tot = sum1[c];
calcDP(c, 0);
leaf.clear();
for (auto i : cur)
{
vis[i] = 0;
if (dp[i] == i)
{
leaf.emplace_back(pll(val[dp[i]], pre[i]));
val[dp[i]] = 0;
}
pre[i] = 0;
dp[i] = 0;
}
if (leaf.size() < 2)
return;
sort(leaf.begin(), leaf.end(), greater<>());
bool f = 0;
for (int i = 1; i < (int)leaf.size(); i++)
{
if (f)
leaf[i - 1] = leaf[i];
else if (leaf[i].S != leaf[0].S)
{
f = 1;
tot += leaf[i].F;
}
}
leaf.pop_back();
for (int i = 0; i < (int)leaf.size(); i++)
{
tot += leaf[i].F;
ans[i + 2] = max(ans[i + 2], tot);
}
vis[c] = 1;
for (auto [i, p] : E[c])
if (!vis[i])
centroid(i);
}
void calc1(int u)
{
dp[u] = 0;
for (auto [v, p] : E[u])
if (v != pre[u])
{
pre[v] = u;
calc1(v);
dp[u] += dp[v] + p.S;
}
}
void calc1root(int u)
{
ans[1] = max(ans[1], sum1[u] = dp[u]);
for (auto [v, p] : E[u])
if (v != pre[u])
{
dp[v] += dp[u] - dp[v] + p.F - p.S;
calc1root(v);
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N;
if(N == 2)
{
int A, B, C, D;
cin >> A >> B >> C >> D;
sum += C + D;
ans[1] = max(C, D);
ans[2] = C + D;
goto ono;
}
for (int i = 1; i < N; i++)
{
int A, B, C, D;
cin >> A >> B >> C >> D;
sum += C + D;
E[A].emplace_back(make_pair(B, pll(C, D)));
E[B].emplace_back(make_pair(A, pll(D, C)));
}
calc1(1);
calc1root(1);
for (int i = 1; i <= N; i++)
dp[i] = 0;
centroid(1);
for (int i = 1; i <= N; i++)
ans[i] = max(ans[i], ans[i - 1]);
ono:
cin >> Q;
for (int i = 1; i <= Q; i++)
{
int E;
cin >> E;
cout << sum - ans[E] << '\n';
}
}
/*
10
1 2 1 1
2 3 1 1
3 4 1 1
4 5 1 1
5 6 1 1
6 7 1 1
7 8 1 1
8 9 1 1
9 10 1 1
4
1
2
3
4
*/
Compilation message
designated_cities.cpp: In function 'void calcSz(int)':
designated_cities.cpp:20:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [v, p] : E[u])
| ^
designated_cities.cpp: In function 'void calcDP(int, int)':
designated_cities.cpp:33:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for (auto [v, p] : E[u])
| ^
designated_cities.cpp: In function 'void centroid(int)':
designated_cities.cpp:55:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto [j, p] : E[i])
| ^
designated_cities.cpp:101:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
101 | for (auto [i, p] : E[c])
| ^
designated_cities.cpp: In function 'void calc1(int)':
designated_cities.cpp:109:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
109 | for (auto [v, p] : E[u])
| ^
designated_cities.cpp: In function 'void calc1root(int)':
designated_cities.cpp:121:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
121 | for (auto [v, p] : E[u])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
879 ms |
28912 KB |
Output is correct |
3 |
Correct |
1057 ms |
40392 KB |
Output is correct |
4 |
Correct |
771 ms |
28784 KB |
Output is correct |
5 |
Correct |
401 ms |
29692 KB |
Output is correct |
6 |
Correct |
1010 ms |
30860 KB |
Output is correct |
7 |
Correct |
302 ms |
30276 KB |
Output is correct |
8 |
Correct |
1045 ms |
41184 KB |
Output is correct |
9 |
Correct |
245 ms |
32520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
870 ms |
29212 KB |
Output is correct |
3 |
Correct |
1109 ms |
45668 KB |
Output is correct |
4 |
Correct |
863 ms |
32028 KB |
Output is correct |
5 |
Correct |
407 ms |
32844 KB |
Output is correct |
6 |
Correct |
1035 ms |
34288 KB |
Output is correct |
7 |
Correct |
267 ms |
35548 KB |
Output is correct |
8 |
Correct |
1151 ms |
40104 KB |
Output is correct |
9 |
Correct |
209 ms |
35740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
4952 KB |
Output is correct |
13 |
Correct |
7 ms |
5332 KB |
Output is correct |
14 |
Correct |
6 ms |
5460 KB |
Output is correct |
15 |
Correct |
7 ms |
5292 KB |
Output is correct |
16 |
Correct |
8 ms |
5332 KB |
Output is correct |
17 |
Correct |
7 ms |
5332 KB |
Output is correct |
18 |
Correct |
7 ms |
5332 KB |
Output is correct |
19 |
Correct |
6 ms |
5332 KB |
Output is correct |
20 |
Correct |
5 ms |
5372 KB |
Output is correct |
21 |
Correct |
6 ms |
5332 KB |
Output is correct |
22 |
Correct |
6 ms |
5292 KB |
Output is correct |
23 |
Correct |
6 ms |
5332 KB |
Output is correct |
24 |
Correct |
5 ms |
5428 KB |
Output is correct |
25 |
Correct |
6 ms |
5428 KB |
Output is correct |
26 |
Correct |
5 ms |
5332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
879 ms |
28912 KB |
Output is correct |
3 |
Correct |
1057 ms |
40392 KB |
Output is correct |
4 |
Correct |
771 ms |
28784 KB |
Output is correct |
5 |
Correct |
401 ms |
29692 KB |
Output is correct |
6 |
Correct |
1010 ms |
30860 KB |
Output is correct |
7 |
Correct |
302 ms |
30276 KB |
Output is correct |
8 |
Correct |
1045 ms |
41184 KB |
Output is correct |
9 |
Correct |
245 ms |
32520 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
870 ms |
29212 KB |
Output is correct |
12 |
Correct |
1109 ms |
45668 KB |
Output is correct |
13 |
Correct |
863 ms |
32028 KB |
Output is correct |
14 |
Correct |
407 ms |
32844 KB |
Output is correct |
15 |
Correct |
1035 ms |
34288 KB |
Output is correct |
16 |
Correct |
267 ms |
35548 KB |
Output is correct |
17 |
Correct |
1151 ms |
40104 KB |
Output is correct |
18 |
Correct |
209 ms |
35740 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
792 ms |
31940 KB |
Output is correct |
21 |
Correct |
1032 ms |
45824 KB |
Output is correct |
22 |
Correct |
878 ms |
32040 KB |
Output is correct |
23 |
Correct |
888 ms |
32288 KB |
Output is correct |
24 |
Correct |
891 ms |
32412 KB |
Output is correct |
25 |
Correct |
899 ms |
32300 KB |
Output is correct |
26 |
Correct |
851 ms |
32300 KB |
Output is correct |
27 |
Correct |
367 ms |
31956 KB |
Output is correct |
28 |
Correct |
984 ms |
33948 KB |
Output is correct |
29 |
Correct |
847 ms |
32364 KB |
Output is correct |
30 |
Correct |
711 ms |
32908 KB |
Output is correct |
31 |
Correct |
276 ms |
33676 KB |
Output is correct |
32 |
Correct |
1066 ms |
43184 KB |
Output is correct |
33 |
Correct |
214 ms |
35820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
3 ms |
5032 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
879 ms |
28912 KB |
Output is correct |
14 |
Correct |
1057 ms |
40392 KB |
Output is correct |
15 |
Correct |
771 ms |
28784 KB |
Output is correct |
16 |
Correct |
401 ms |
29692 KB |
Output is correct |
17 |
Correct |
1010 ms |
30860 KB |
Output is correct |
18 |
Correct |
302 ms |
30276 KB |
Output is correct |
19 |
Correct |
1045 ms |
41184 KB |
Output is correct |
20 |
Correct |
245 ms |
32520 KB |
Output is correct |
21 |
Correct |
4 ms |
4948 KB |
Output is correct |
22 |
Correct |
870 ms |
29212 KB |
Output is correct |
23 |
Correct |
1109 ms |
45668 KB |
Output is correct |
24 |
Correct |
863 ms |
32028 KB |
Output is correct |
25 |
Correct |
407 ms |
32844 KB |
Output is correct |
26 |
Correct |
1035 ms |
34288 KB |
Output is correct |
27 |
Correct |
267 ms |
35548 KB |
Output is correct |
28 |
Correct |
1151 ms |
40104 KB |
Output is correct |
29 |
Correct |
209 ms |
35740 KB |
Output is correct |
30 |
Correct |
3 ms |
4952 KB |
Output is correct |
31 |
Correct |
7 ms |
5332 KB |
Output is correct |
32 |
Correct |
6 ms |
5460 KB |
Output is correct |
33 |
Correct |
7 ms |
5292 KB |
Output is correct |
34 |
Correct |
8 ms |
5332 KB |
Output is correct |
35 |
Correct |
7 ms |
5332 KB |
Output is correct |
36 |
Correct |
7 ms |
5332 KB |
Output is correct |
37 |
Correct |
6 ms |
5332 KB |
Output is correct |
38 |
Correct |
5 ms |
5372 KB |
Output is correct |
39 |
Correct |
6 ms |
5332 KB |
Output is correct |
40 |
Correct |
6 ms |
5292 KB |
Output is correct |
41 |
Correct |
6 ms |
5332 KB |
Output is correct |
42 |
Correct |
5 ms |
5428 KB |
Output is correct |
43 |
Correct |
6 ms |
5428 KB |
Output is correct |
44 |
Correct |
5 ms |
5332 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
792 ms |
31940 KB |
Output is correct |
47 |
Correct |
1032 ms |
45824 KB |
Output is correct |
48 |
Correct |
878 ms |
32040 KB |
Output is correct |
49 |
Correct |
888 ms |
32288 KB |
Output is correct |
50 |
Correct |
891 ms |
32412 KB |
Output is correct |
51 |
Correct |
899 ms |
32300 KB |
Output is correct |
52 |
Correct |
851 ms |
32300 KB |
Output is correct |
53 |
Correct |
367 ms |
31956 KB |
Output is correct |
54 |
Correct |
984 ms |
33948 KB |
Output is correct |
55 |
Correct |
847 ms |
32364 KB |
Output is correct |
56 |
Correct |
711 ms |
32908 KB |
Output is correct |
57 |
Correct |
276 ms |
33676 KB |
Output is correct |
58 |
Correct |
1066 ms |
43184 KB |
Output is correct |
59 |
Correct |
214 ms |
35820 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
875 ms |
35772 KB |
Output is correct |
62 |
Correct |
1091 ms |
46416 KB |
Output is correct |
63 |
Correct |
796 ms |
35880 KB |
Output is correct |
64 |
Correct |
775 ms |
36136 KB |
Output is correct |
65 |
Correct |
758 ms |
35908 KB |
Output is correct |
66 |
Correct |
885 ms |
36128 KB |
Output is correct |
67 |
Correct |
946 ms |
35868 KB |
Output is correct |
68 |
Correct |
417 ms |
35996 KB |
Output is correct |
69 |
Correct |
989 ms |
37592 KB |
Output is correct |
70 |
Correct |
850 ms |
36128 KB |
Output is correct |
71 |
Correct |
690 ms |
36780 KB |
Output is correct |
72 |
Correct |
337 ms |
37952 KB |
Output is correct |
73 |
Correct |
1115 ms |
42940 KB |
Output is correct |
74 |
Correct |
251 ms |
41348 KB |
Output is correct |