#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
struct edge {
int u, f, b;
};
const int MN = 200005;
int n, q, u, v, w, x, cost, ans[MN], dp[MN], node[MN], pre[MN], up[MN], vis[MN];
vector<edge> a[MN];
void dfs1(int cur, int par) {
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.f;
dfs1(nxt.u, cur);
}
}
void dfs2(int cur, int par) {
ans[1] = min(ans[1], cost);
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.b - nxt.f;
dfs2(nxt.u, cur);
cost += nxt.f - nxt.b;
}
}
void dfs3(int cur, int par) {
node[cur] = cur;
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.b - nxt.f;
dfs3(nxt.u, cur);
cost += nxt.f - nxt.b;
int val = dp[cur] + dp[nxt.u] + nxt.f;
if (cost - val < ans[2]) {
ans[2] = cost - val;
u = node[cur], v = node[nxt.u];
}
if (dp[cur] < dp[nxt.u] + nxt.f) {
dp[cur] = dp[nxt.u] + nxt.f;
node[cur] = node[nxt.u];
}
}
}
void dfs4(int cur, int par) {
pre[cur] = par;
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
up[nxt.u] = nxt.f;
dfs4(nxt.u, cur);
}
}
void solve1() {
dfs1(1, 0);
dfs2(1, 0);
}
void solve_other() {
dfs3(1, 0);
dfs4(u, 0);
while (v != u) vis[v] = 1, v = pre[v];
vis[v] = 1;
for (int i = 3; i <= n; i++) {
ans[i] = ans[i - 1];
int best = 0;
for (int j = 1; j <= n; j++) {
if (vis[j] || a[j].size() > 1) continue;
int tmp = 0;
u = j;
while (!vis[u]) tmp += up[u], u = pre[u];
if (tmp > best) best = tmp, v = j;
}
ans[i] -= best;
while (!vis[v]) vis[v] = 1, v = pre[v];
}
}
int32_t main() {
boost();
memset(ans, 0x3f, sizeof(ans));
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v >> w >> x;
a[u].push_back({v, w, x});
a[v].push_back({u, x, w});
}
solve1();
solve_other();
cin >> q;
while (q--) {
cin >> x;
printf("%lld\n", ans[x]);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
3 ms |
6584 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6604 KB |
Output is correct |
6 |
Correct |
3 ms |
6604 KB |
Output is correct |
7 |
Correct |
3 ms |
6604 KB |
Output is correct |
8 |
Correct |
3 ms |
6584 KB |
Output is correct |
9 |
Correct |
3 ms |
6604 KB |
Output is correct |
10 |
Correct |
3 ms |
6604 KB |
Output is correct |
11 |
Correct |
3 ms |
6604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6584 KB |
Output is correct |
2 |
Execution timed out |
2054 ms |
33884 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Execution timed out |
2073 ms |
33852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
3 ms |
6584 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6604 KB |
Output is correct |
6 |
Correct |
3 ms |
6604 KB |
Output is correct |
7 |
Correct |
3 ms |
6604 KB |
Output is correct |
8 |
Correct |
3 ms |
6584 KB |
Output is correct |
9 |
Correct |
3 ms |
6604 KB |
Output is correct |
10 |
Correct |
3 ms |
6604 KB |
Output is correct |
11 |
Correct |
3 ms |
6604 KB |
Output is correct |
12 |
Correct |
3 ms |
6604 KB |
Output is correct |
13 |
Correct |
10 ms |
6860 KB |
Output is correct |
14 |
Correct |
6 ms |
6988 KB |
Output is correct |
15 |
Correct |
18 ms |
6780 KB |
Output is correct |
16 |
Correct |
10 ms |
6860 KB |
Output is correct |
17 |
Correct |
10 ms |
6852 KB |
Output is correct |
18 |
Correct |
10 ms |
6860 KB |
Output is correct |
19 |
Correct |
11 ms |
6852 KB |
Output is correct |
20 |
Correct |
10 ms |
6880 KB |
Output is correct |
21 |
Correct |
9 ms |
6852 KB |
Output is correct |
22 |
Correct |
9 ms |
6900 KB |
Output is correct |
23 |
Correct |
25 ms |
6848 KB |
Output is correct |
24 |
Correct |
13 ms |
6796 KB |
Output is correct |
25 |
Correct |
6 ms |
6988 KB |
Output is correct |
26 |
Correct |
13 ms |
6860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6584 KB |
Output is correct |
2 |
Execution timed out |
2054 ms |
33884 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
3 ms |
6584 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6604 KB |
Output is correct |
6 |
Correct |
3 ms |
6604 KB |
Output is correct |
7 |
Correct |
3 ms |
6604 KB |
Output is correct |
8 |
Correct |
3 ms |
6584 KB |
Output is correct |
9 |
Correct |
3 ms |
6604 KB |
Output is correct |
10 |
Correct |
3 ms |
6604 KB |
Output is correct |
11 |
Correct |
3 ms |
6604 KB |
Output is correct |
12 |
Correct |
3 ms |
6584 KB |
Output is correct |
13 |
Execution timed out |
2054 ms |
33884 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |