#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 200010;
int mxp[maxn*4], fa[maxn], oval[maxn], dfn[maxn], rdfn[maxn], l[maxn], sz[maxn], vis[maxn], tval[maxn], tag[maxn], mx[maxn], val[maxn], del[maxn], e, n, q;
ll ans[maxn], dis[maxn];
int tot = 0;
vector<int> lst;
struct Edge {
int v, w, x;
} E[maxn*2];
struct dat {
ll mx;
int mxp;
} tree[maxn*4];
ll add[maxn*4];
dat operator+(const dat &x, const dat &y) {
dat ret;
if (x.mx < y.mx) {
ret.mx = y.mx;
ret.mxp = y.mxp;
} else {
ret.mx = x.mx;
ret.mxp = x.mxp;
}
return ret;
}
inline void addEdge(int u, int v, int w) {
E[e].v = v; E[e].x = l[u]; E[e].w = w; l[u] = e++;
}
void dfs_init(int u, int f) {
sz[u] = 1; mx[u] = 0;
lst.push_back(u);
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (v != f && !vis[v]) {
dfs_init(v, u);
sz[u] += sz[v];
mx[u] = max(mx[u], sz[v]);
}
}
}
void pushUp(int rt) {
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}
void build(int l, int r, int rt) {
add[rt] = 0;
if (l == r) {
tree[rt].mx = dis[rdfn[l]];
tree[rt].mxp = l;
return;
}
int m = (l + r) >> 1;
build(l, m, rt<<1);
build(m+1, r, rt<<1|1);
pushUp(rt);
}
void modify(int rt, ll v) {
tree[rt].mx += v;
add[rt] += v;
}
void pushDown(int rt) {
if (add[rt]) {
modify(rt<<1, add[rt]);
modify(rt<<1|1, add[rt]);
add[rt] = 0;
}
}
void update(int L, int R, ll v, int l, int r, int rt) {
if (L <= l && r <= R) {
modify(rt, v);
return;
}
pushDown(rt);
int m = (l + r) >> 1;
if (L <= m) update(L, R, v, l, m, rt<<1);
if (R > m) update(L, R, v, m+1, r, rt<<1|1);
pushUp(rt);
}
dat ask(int p, int l, int r, int rt) {
if (l == r) return tree[rt];
pushDown(rt);
int m = (l + r) >> 1;
if (p <= m) return ask(p, l, m, rt<<1);
else return ask(p, m+1, r, rt<<1|1);
}
void dfs_cal(int u, int f, int t) {
fa[u] = f;
dfn[u] = ++tot; rdfn[tot] = u;
sz[u] = 1; tag[u] = t;
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (v != f && !vis[v]) {
val[v] = E[p].w;
oval[v] = E[p^1].w;
dis[v] = dis[u] + val[v];
dfs_cal(v, u, t);
sz[u] += sz[v];
}
}
}
ll cur_ans = 0;
void choose(int u) {
while (u && !del[u]) {
del[u] = 1;
update(dfn[u], dfn[u] + sz[u] - 1, -val[u], 1, tot, 1);
cur_ans += val[u];
u = fa[u];
}
}
void calans(int c) {
int t = tag[rdfn[tree[1].mxp]];
dat T; T.mx = -1; T.mxp = 0;
for (int i = 0; i < lst.size(); i++) {
int u = lst[i];
if (u != c && tag[u] != tag[t]) {
T = T + ask(dfn[u], 1, tot, 1);
}
}
int u = rdfn[T.mxp];
choose(u);
for (int i = 2; i <= int(lst.size()); i++) {
choose(rdfn[tree[1].mxp]);
ans[i] = max(ans[i], cur_ans);
}
}
namespace DP {
ll sum[maxn*2];
void dfs1(int u, int f, int fa_e) {
if (fa_e != -1) sum[fa_e] = E[fa_e].w;
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (v == f) continue;
dfs1(v, u, p^1);
if (fa_e != -1) {
sum[fa_e] += sum[p^1];
}
}
}
void dfs2(int u, int f) {
ll s = 0;
for (int p = l[u]; p >= 0; p = E[p].x) {
s += sum[p^1];
}
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (v == f) continue;
sum[p] = s - sum[p^1] + E[p].w;
}
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (v != f) {
dfs2(v, u);
}
}
}
void solve() {
dfs1(1, 0, -1);
dfs2(1, 0);
}
}
void solve(int u) {
lst.clear();
dfs_init(u, 0);
int c = 0, total = int(lst.size());
for (int i = 0; i < lst.size(); i++) {
int t = lst[i];
tval[t] = max(total-sz[t], mx[t]);
if (!c || tval[t] < tval[c]) c = t;
}
//处理过 c 的连通块
dis[c] = 0;
fa[c] = 0;
tot = 1;
dfn[c] = 1; rdfn[1] = c;
val[c] = oval[c] = 0;
int scnt = 0;
for (int p = l[c]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v]) {
++ scnt;
val[v] = E[p].w;
oval[v] = E[p^1].w;
dis[v] = val[v];
fa[v] = c;
dfs_cal(v, c, v);
}
}
build(1, tot, 1);
ll sum = 0;
for (int i = 0; i < lst.size(); i++) {
int t = lst[i];
sum += oval[t];
del[t] = 0;
for (int p = l[t]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (vis[v]) sum += DP::sum[p^1];
}
}
ans[1] = max(ans[1], sum);
if (scnt == 1) {
if (total == 2) {
for (int p = l[c]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v]) {
sum += val[v];
}
}
ans[2] = max(ans[2], sum);
}
} else {
cur_ans = sum;
calans(c);
}
vis[c] = 1;
for (int p = l[c]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!vis[v]) {
solve(v);
}
}
}
int main() {
ll total_sum = 0;
memset(l, -1, sizeof(l));
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int A, B, C, D;
scanf("%d%d%d%d", &A, &B, &C, &D);
addEdge(A, B, C);
addEdge(B, A, D);
total_sum += C;
total_sum += D;
}
DP::solve();
solve(1);
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int k = 0;
scanf("%d", &k);
printf("%lld\n", total_sum-ans[k]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void calans(int)':
designated_cities.cpp:139:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < lst.size(); i++) {
~~^~~~~~~~~~~~
designated_cities.cpp: In function 'void solve(int)':
designated_cities.cpp:193:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < lst.size(); i++) {
~~^~~~~~~~~~~~
designated_cities.cpp:218:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < lst.size(); i++) {
~~^~~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:254:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
designated_cities.cpp:257:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &A, &B, &C, &D);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:265:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
designated_cities.cpp:268:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &k);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
2 ms |
1152 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1280 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
33904 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1280 KB |
Output is correct |
2 |
Execution timed out |
2065 ms |
34044 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
2 ms |
1152 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
3 ms |
1280 KB |
Output is correct |
13 |
Correct |
10 ms |
1536 KB |
Output is correct |
14 |
Correct |
9 ms |
1664 KB |
Output is correct |
15 |
Correct |
10 ms |
1536 KB |
Output is correct |
16 |
Correct |
10 ms |
1536 KB |
Output is correct |
17 |
Correct |
9 ms |
1536 KB |
Output is correct |
18 |
Correct |
8 ms |
1536 KB |
Output is correct |
19 |
Correct |
9 ms |
1536 KB |
Output is correct |
20 |
Correct |
7 ms |
1536 KB |
Output is correct |
21 |
Correct |
10 ms |
1536 KB |
Output is correct |
22 |
Correct |
9 ms |
1536 KB |
Output is correct |
23 |
Correct |
8 ms |
1536 KB |
Output is correct |
24 |
Correct |
7 ms |
1536 KB |
Output is correct |
25 |
Correct |
11 ms |
1664 KB |
Output is correct |
26 |
Correct |
6 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1280 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
33904 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
2 ms |
1152 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
4 ms |
1280 KB |
Output is correct |
13 |
Execution timed out |
2053 ms |
33904 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |