#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(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 |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
107 ms |
19572 KB |
Output is correct |
3 |
Correct |
134 ms |
33032 KB |
Output is correct |
4 |
Correct |
98 ms |
18296 KB |
Output is correct |
5 |
Correct |
103 ms |
19464 KB |
Output is correct |
6 |
Correct |
110 ms |
21584 KB |
Output is correct |
7 |
Correct |
88 ms |
19400 KB |
Output is correct |
8 |
Correct |
113 ms |
33620 KB |
Output is correct |
9 |
Correct |
84 ms |
20228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Incorrect |
275 ms |
43212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
889 ms |
912 KB |
Output is correct |
14 |
Correct |
650 ms |
856 KB |
Output is correct |
15 |
Correct |
842 ms |
860 KB |
Output is correct |
16 |
Correct |
872 ms |
1112 KB |
Output is correct |
17 |
Correct |
906 ms |
908 KB |
Output is correct |
18 |
Correct |
868 ms |
856 KB |
Output is correct |
19 |
Correct |
890 ms |
860 KB |
Output is correct |
20 |
Correct |
827 ms |
860 KB |
Output is correct |
21 |
Correct |
904 ms |
936 KB |
Output is correct |
22 |
Correct |
860 ms |
860 KB |
Output is correct |
23 |
Correct |
863 ms |
912 KB |
Output is correct |
24 |
Correct |
709 ms |
920 KB |
Output is correct |
25 |
Correct |
877 ms |
1064 KB |
Output is correct |
26 |
Correct |
666 ms |
908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
107 ms |
19572 KB |
Output is correct |
3 |
Correct |
134 ms |
33032 KB |
Output is correct |
4 |
Correct |
98 ms |
18296 KB |
Output is correct |
5 |
Correct |
103 ms |
19464 KB |
Output is correct |
6 |
Correct |
110 ms |
21584 KB |
Output is correct |
7 |
Correct |
88 ms |
19400 KB |
Output is correct |
8 |
Correct |
113 ms |
33620 KB |
Output is correct |
9 |
Correct |
84 ms |
20228 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Incorrect |
275 ms |
43212 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
107 ms |
19572 KB |
Output is correct |
14 |
Correct |
134 ms |
33032 KB |
Output is correct |
15 |
Correct |
98 ms |
18296 KB |
Output is correct |
16 |
Correct |
103 ms |
19464 KB |
Output is correct |
17 |
Correct |
110 ms |
21584 KB |
Output is correct |
18 |
Correct |
88 ms |
19400 KB |
Output is correct |
19 |
Correct |
113 ms |
33620 KB |
Output is correct |
20 |
Correct |
84 ms |
20228 KB |
Output is correct |
21 |
Correct |
0 ms |
600 KB |
Output is correct |
22 |
Incorrect |
275 ms |
43212 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |