#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
const ll inf = numeric_limits<ll> :: max();
struct ST{
vector<pair<ll, int>> t;
vector<ll> a, p;
int n;
ST(int ns){
n = ns;
a.resize(n + 1);
t.resize(4 * n);
p.resize(4 * n);
}
void upd(int p, ll x){
a[p] = x;
}
void build(int v, int tl, int tr){
if (tl == tr){
t[v] = {a[tl], tl};
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
build(vv, tl, tm);
build(vv + 1, tm + 1, tr);
t[v] = max(t[vv], t[vv + 1]);
}
void build(){
fill(p.begin(), p.end(), 0);
build(1, 1, n);
}
pair<ll, int> get(){
return t[1];
}
void push(int& v){
if (!p[v]) return;
int vv = 2 * v;
t[vv].ff += p[v]; p[vv] += p[v];
t[vv + 1].ff += p[v]; p[vv + 1] += p[v];
p[v] = 0;
}
void add(int v, int tl, int tr, int& l, int& r, int& x){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
t[v].ff += x;
p[v] += x;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v);
add(vv, tl, tm, l, r, x);
add(vv + 1, tm + 1, tr, l, r, x);
t[v] = max(t[vv], t[vv + 1]);
}
void add(int l, int r, int x){
if (l > r) return;
add(1, 1, n, l, r, x);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<arr3> g[n + 1];
ll tot = 0;
for (int i = 1; i < n; i++){
int a, b, c, d; cin>>a>>b>>c>>d;
tot += c; tot += d;
g[a].pb({b, c, d});
g[b].pb({a, d, c});
}
int q; cin>>q;
vector<int> qq(q + 1);
for (int i = 1; i <= q; i++){
cin>>qq[i];
}
if (q == 1 && qq[1] == 1){
ll S = 0;
function<void(int, int)> dfs = [&](int v, int pr){
for (auto [i, w1, w2]: g[v]){
if (i == pr) continue;
S += w2;
dfs(i, v);
}
};
dfs(1, 0);
ll out = inf;
function<void(int, int)> solve = [&](int v, int pr){
out = min(out, tot - S);
for (auto [i, w1, w2]: g[v]){
if (i == pr) continue;
S += (w1 - w2);
solve(i, v);
S -= (w1 - w2);
}
};
solve(1, 0);
cout<<out<<"\n";
return 0;
}
if (q == 1 && qq[1] == 2){
vector<ll> d(n + 1);
vector<int> tin(n + 1), tout(n + 1);
ll S = 0;
int timer = 0;
function<void(int, int)> dfs = [&](int v, int pr){
tin[v] = ++timer;
for (auto [i, w1, w2]: g[v]){
if (i == pr) continue;
S += w2;
d[i] = d[v] + w1;
dfs(i, v);
}
tout[v] = timer;
};
dfs(1, 0);
ST T(n);
for (int i = 1; i <= n; i++) T.upd(tin[i], d[i]);
T.build();
ll out = inf;
function<void(int, int)> solve = [&](int v, int pr){
out = min(out, tot - (S + T.get().ff));
for (auto [i, w1, w2]: g[v]){
if (i == pr) continue;
S += (w1 - w2);
T.add(1, n, w2);
T.add(tin[i], tout[i], -(w1 + w2));
solve(i, v);
S -= (w1 - w2);
T.add(1, n, -w2);
T.add(tin[i], tout[i], (w1 + w2));
}
};
solve(1, 0);
cout<<out<<"\n";
return 0;
}
vector<int> tin(n + 1), tout(n + 1), inv(n + 1), p(n + 1), dd(n + 1);
vector<ll> d(n + 1);
int timer = 0; ll S = 0;
function<void(int, int)> dfs = [&](int v, int pr){
p[v] = pr;
tin[v] = ++timer;
for (auto [i, w1, w2]: g[v]){
if (i == pr) continue;
S += w2;
dd[i] = w1;
d[i] = d[v] + w1;
dfs(i, v);
}
tout[v] = timer;
};
vector<ll> out(n + 1, inf);
vector<bool> used(n + 1);
ST T(n);
for (int i = 1; i <= n; i++){
S = d[i] = dd[i] = timer = 0;
dfs(i, 0);
out[1] = min(out[1], tot - S);
for (int j = 1; j <= n; j++){
T.upd(tin[j], d[j]);
inv[tin[j]] = j;
}
T.build();
fill(used.begin(), used.end(), 0);
used[i] = 1;
for (int j = 2; j <= n; j++){
auto [x, y] = T.get();
if (!x) continue;
int v = inv[y];
while (!used[v]){
T.add(tin[v], tout[v], -dd[v]);
used[v] = 1;
v = p[v];
}
S += x;
out[j] = min(out[j], tot - S);
}
}
for (int i = 1; i <= n; i++) out[i] = min(out[i], out[i - 1]);
for (int i = 1; i <= q; i++){
cout<<out[qq[i]]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
114 ms |
19648 KB |
Output is correct |
3 |
Correct |
134 ms |
32852 KB |
Output is correct |
4 |
Correct |
121 ms |
18220 KB |
Output is correct |
5 |
Correct |
104 ms |
19636 KB |
Output is correct |
6 |
Correct |
113 ms |
21588 KB |
Output is correct |
7 |
Correct |
93 ms |
19456 KB |
Output is correct |
8 |
Correct |
135 ms |
33620 KB |
Output is correct |
9 |
Correct |
79 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
269 ms |
43056 KB |
Output is correct |
3 |
Correct |
260 ms |
62016 KB |
Output is correct |
4 |
Correct |
314 ms |
41812 KB |
Output is correct |
5 |
Correct |
261 ms |
43128 KB |
Output is correct |
6 |
Correct |
245 ms |
45908 KB |
Output is correct |
7 |
Correct |
207 ms |
43352 KB |
Output is correct |
8 |
Correct |
302 ms |
53840 KB |
Output is correct |
9 |
Correct |
228 ms |
43320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
919 ms |
940 KB |
Output is correct |
14 |
Correct |
660 ms |
1072 KB |
Output is correct |
15 |
Correct |
844 ms |
860 KB |
Output is correct |
16 |
Correct |
888 ms |
860 KB |
Output is correct |
17 |
Correct |
905 ms |
856 KB |
Output is correct |
18 |
Correct |
927 ms |
912 KB |
Output is correct |
19 |
Correct |
917 ms |
860 KB |
Output is correct |
20 |
Correct |
848 ms |
856 KB |
Output is correct |
21 |
Correct |
937 ms |
928 KB |
Output is correct |
22 |
Correct |
878 ms |
912 KB |
Output is correct |
23 |
Correct |
901 ms |
912 KB |
Output is correct |
24 |
Correct |
731 ms |
860 KB |
Output is correct |
25 |
Correct |
866 ms |
856 KB |
Output is correct |
26 |
Correct |
699 ms |
912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
114 ms |
19648 KB |
Output is correct |
3 |
Correct |
134 ms |
32852 KB |
Output is correct |
4 |
Correct |
121 ms |
18220 KB |
Output is correct |
5 |
Correct |
104 ms |
19636 KB |
Output is correct |
6 |
Correct |
113 ms |
21588 KB |
Output is correct |
7 |
Correct |
93 ms |
19456 KB |
Output is correct |
8 |
Correct |
135 ms |
33620 KB |
Output is correct |
9 |
Correct |
79 ms |
20052 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
269 ms |
43056 KB |
Output is correct |
12 |
Correct |
260 ms |
62016 KB |
Output is correct |
13 |
Correct |
314 ms |
41812 KB |
Output is correct |
14 |
Correct |
261 ms |
43128 KB |
Output is correct |
15 |
Correct |
245 ms |
45908 KB |
Output is correct |
16 |
Correct |
207 ms |
43352 KB |
Output is correct |
17 |
Correct |
302 ms |
53840 KB |
Output is correct |
18 |
Correct |
228 ms |
43320 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
2089 ms |
47216 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
114 ms |
19648 KB |
Output is correct |
14 |
Correct |
134 ms |
32852 KB |
Output is correct |
15 |
Correct |
121 ms |
18220 KB |
Output is correct |
16 |
Correct |
104 ms |
19636 KB |
Output is correct |
17 |
Correct |
113 ms |
21588 KB |
Output is correct |
18 |
Correct |
93 ms |
19456 KB |
Output is correct |
19 |
Correct |
135 ms |
33620 KB |
Output is correct |
20 |
Correct |
79 ms |
20052 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
269 ms |
43056 KB |
Output is correct |
23 |
Correct |
260 ms |
62016 KB |
Output is correct |
24 |
Correct |
314 ms |
41812 KB |
Output is correct |
25 |
Correct |
261 ms |
43128 KB |
Output is correct |
26 |
Correct |
245 ms |
45908 KB |
Output is correct |
27 |
Correct |
207 ms |
43352 KB |
Output is correct |
28 |
Correct |
302 ms |
53840 KB |
Output is correct |
29 |
Correct |
228 ms |
43320 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
919 ms |
940 KB |
Output is correct |
32 |
Correct |
660 ms |
1072 KB |
Output is correct |
33 |
Correct |
844 ms |
860 KB |
Output is correct |
34 |
Correct |
888 ms |
860 KB |
Output is correct |
35 |
Correct |
905 ms |
856 KB |
Output is correct |
36 |
Correct |
927 ms |
912 KB |
Output is correct |
37 |
Correct |
917 ms |
860 KB |
Output is correct |
38 |
Correct |
848 ms |
856 KB |
Output is correct |
39 |
Correct |
937 ms |
928 KB |
Output is correct |
40 |
Correct |
878 ms |
912 KB |
Output is correct |
41 |
Correct |
901 ms |
912 KB |
Output is correct |
42 |
Correct |
731 ms |
860 KB |
Output is correct |
43 |
Correct |
866 ms |
856 KB |
Output is correct |
44 |
Correct |
699 ms |
912 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Execution timed out |
2089 ms |
47216 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |