#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2e5 + 10;
struct edge
{
int v, u;
ll c, d;
edge(int _v = 0, int _u = 0, ll _c = 0, ll _d = 0)
{
v = _v;
u = _u;
c = _c;
d = _d;
}
} edges[maxn];
int n;
vector < pair < int, int > > adj[maxn];
ll sum_all = 0;
void input()
{
cin >> n;
for (int i = 1; i < n; i ++)
{
cin >> edges[i].v >> edges[i].u >> edges[i].c >> edges[i].d;
adj[edges[i].v].push_back({edges[i].u, i});
adj[edges[i].u].push_back({edges[i].v, i});
sum_all += edges[i].c + edges[i].d;
}
}
const ll inf = 1e18;
ll single;
ll sub[maxn];
void calc(int v, int p)
{
sub[v] = 0;
for (pair < int, int > nb : adj[v])
{
int u = nb.first;
if (u == p)
continue;
calc(u, v);
if (v == edges[nb.second].v)
sub[v] += edges[nb.second].d; //cout << "edge " << v << " : " << u << " : " << edges[nb.second].d << endl;
else
sub[v] += edges[nb.second].c;
sub[v] += sub[u];
}
//cout << "calc " << v << " " << sub[v] << endl;
}
void single_dfs(int v, int p, ll val)
{
single = max(single, val);
///cout << v << " : " << val << endl;
for (pair < int, int > nb : adj[v])
{
int u = nb.first;
if (u == p)
continue;
ll cur;
if (v == edges[nb.second].v)
cur = edges[nb.second].c - edges[nb.second].d;
else
cur = edges[nb.second].d - edges[nb.second].c;
single_dfs(u, v, val + cur);
}
}
void solve_single()
{
calc(1, -1);
single_dfs(1, -1, sub[1]);
single = sum_all - single;
}
ll depth[maxn];
int ord[maxn], timer;
int tin[maxn], tout[maxn];
int par[maxn], ridx[maxn], marked[maxn];
void do_math(int v, int p, int id)
{
par[v] = p;
ridx[v] = id;
ord[++ timer] = v;
tin[v] = timer;
for (pair < int, int > nb : adj[v])
{
int u = nb.first, idx = nb.second;
if (u == p)
continue;
ll val;
if (v == edges[idx].v)
val = edges[idx].c;
else
val = edges[idx].d;
if (marked[idx])
val = 0;
depth[u] = depth[v] + val;
do_math(u, v, idx);
}
tout[v] = timer;
}
bool in_subtree(int v, int u)
{
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
void go_mark(int v)
{
while(par[v] != -1)
{
marked[ridx[v]] = 1;
v = par[v];
}
}
int find_root()
{
int root = 1;
while(true)
{
timer = 0;
depth[root] = 0;
for (int i = 1; i <= n; i ++)
marked[i] = 0;
do_math(root, - 1, - 1);
int d = 1;
for (int i = 1; i <= n; i ++)
if (depth[i] > depth[d])
d = i;
go_mark(d);
timer = 0;
depth[root] = 0;
do_math(root, - 1, - 1);
int f = 1;
for (int i = 1; i <= n; i ++)
if (depth[i] > depth[f])
f = i;
while(f != root && !marked[ridx[f]])
{
f = par[f];
}
return f;
}
return root;
}
ll values[maxn];
struct node
{
ll mx;
int pos;
node (int _pos = 0, ll _mx = 0)
{
pos = _pos;
mx = _mx;
}
};
node unite(node lf, node rf)
{
if (lf.mx > rf.mx)
return lf;
return rf;
}
node tree[4 * maxn];
ll lazy[4 * maxn];
void build(int root, int left, int right)
{
if (left == right)
{
tree[root] = node(ord[left], depth[ord[left]]);
return;
}
int mid = (left + right) / 2;
build(root * 2, left, mid);
build(root * 2 + 1, mid + 1, right);
tree[root] = unite(tree[root * 2], tree[root * 2 + 1]);
//cout << "range " << left << " " << right << " " << tree[root].pos << " " << tree[root].mx << endl;
}
void push_lazy(int root, int left, int right)
{
if (lazy[root] != 0)
///cout << "apply lazy " << root << " " << left << " " << right << " " << lazy[root] << endl;
tree[root].mx += lazy[root];
if (left != right)
{
lazy[root * 2] += lazy[root];
lazy[root * 2 + 1] += lazy[root];
}
lazy[root] = 0;
}
void update(int root, int left, int right, int qleft, int qright, ll val)
{
push_lazy(root, left, right);
if (left > qright || right < qleft)
return;
if (left >= qleft && right <= qright)
{
//cout << "update range " << left << " " << right << " " << val << endl;
lazy[root] = val;
push_lazy(root, left, right);
return;
}
int mid = (left + right) / 2;
update(root * 2, left, mid, qleft, qright, val);
update(root * 2 + 1, mid + 1, right, qleft, qright, val);
tree[root] = unite(tree[root * 2], tree[root * 2 + 1]);
}
int get(int root, int left, int right, int pos)
{
push_lazy(root, left, right);
///cout << "get " << root << " " << left << " " << right << " " << tree[root].mx << " " << tree[root].pos << endl;
if (left == right)
return tree[root].mx;
int mid = (left + right) / 2;
if (pos <= mid)
return get(root * 2, left, mid, pos);
return get(root * 2 + 1, mid + 1, right, pos);
}
void clean(int v)
{
while(par[v] != -1)
{
if (marked[ridx[v]])
break;
marked[ridx[v]] = 1;
ll cur;
if (edges[ridx[v]].u == v)
cur = edges[ridx[v]].c;
else
cur = edges[ridx[v]].d;
update(1, 1, n, tin[v], tout[v], - cur);
///for (int i = tin[v]; i <= tout[v]; i ++)
/// depth[ord[i]] -= cur;
v = par[v];
}
}
void simulate(int root)
{
calc(root, -1);
timer = 0;
depth[root] = 0;
for (int i = 1; i <= n; i ++)
marked[i] = 0;
do_math(root, - 1, - 1);
int pivot = 0;
ll so_far = sum_all - sub[root];
///cout << "root " << root << endl;
build(1, 1, n);
while(true)
{
node cur = tree[1];
int d = cur.pos;
// cout << cur.pos << " : " << cur.mx << endl;
/**for (int i = 1; i <= n; i ++)
if (depth[i] > depth[d])
d = i;*/
///cout << "real " << d << " " << depth[d] << " to " << get(1, 1, n, tin[11]) << endl;
if (depth[d] == 0)
break;
pivot ++;
so_far -= cur.mx;
///so_far -= depth[d];
clean(d);
values[pivot] = so_far;
}
int q;
cin >> q;
for (int i = 1; i <= q; i ++)
{
int e;
cin >> e;
if (e == 1)
{
cout << single << endl;
continue;
}
if (e > pivot)
e = pivot;
cout << values[e] << endl;
}
}
void solve()
{
input();
solve_single();
int root = find_root();
simulate(root);
}
int main()
{
speed();
int t = 1;
///cin >> t;
while(t --)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33628 KB |
Output is correct |
3 |
Correct |
6 ms |
33624 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
6 ms |
33628 KB |
Output is correct |
6 |
Correct |
6 ms |
33628 KB |
Output is correct |
7 |
Correct |
6 ms |
33628 KB |
Output is correct |
8 |
Correct |
6 ms |
33628 KB |
Output is correct |
9 |
Correct |
6 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
6 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33628 KB |
Output is correct |
2 |
Correct |
368 ms |
45140 KB |
Output is correct |
3 |
Correct |
547 ms |
57012 KB |
Output is correct |
4 |
Correct |
341 ms |
45248 KB |
Output is correct |
5 |
Correct |
336 ms |
45252 KB |
Output is correct |
6 |
Correct |
424 ms |
46868 KB |
Output is correct |
7 |
Correct |
296 ms |
45216 KB |
Output is correct |
8 |
Correct |
457 ms |
57496 KB |
Output is correct |
9 |
Correct |
265 ms |
45660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33624 KB |
Output is correct |
2 |
Correct |
433 ms |
45144 KB |
Output is correct |
3 |
Correct |
485 ms |
65592 KB |
Output is correct |
4 |
Correct |
334 ms |
50324 KB |
Output is correct |
5 |
Correct |
397 ms |
51744 KB |
Output is correct |
6 |
Correct |
392 ms |
53740 KB |
Output is correct |
7 |
Correct |
273 ms |
51904 KB |
Output is correct |
8 |
Correct |
437 ms |
59428 KB |
Output is correct |
9 |
Correct |
259 ms |
51832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33628 KB |
Output is correct |
3 |
Correct |
6 ms |
33624 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
6 ms |
33628 KB |
Output is correct |
6 |
Correct |
6 ms |
33628 KB |
Output is correct |
7 |
Correct |
6 ms |
33628 KB |
Output is correct |
8 |
Correct |
6 ms |
33628 KB |
Output is correct |
9 |
Correct |
6 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
6 ms |
33628 KB |
Output is correct |
12 |
Correct |
6 ms |
33624 KB |
Output is correct |
13 |
Correct |
8 ms |
33860 KB |
Output is correct |
14 |
Correct |
8 ms |
33884 KB |
Output is correct |
15 |
Correct |
8 ms |
33628 KB |
Output is correct |
16 |
Correct |
8 ms |
33624 KB |
Output is correct |
17 |
Correct |
7 ms |
33880 KB |
Output is correct |
18 |
Correct |
8 ms |
33628 KB |
Output is correct |
19 |
Correct |
8 ms |
33684 KB |
Output is correct |
20 |
Correct |
8 ms |
33628 KB |
Output is correct |
21 |
Correct |
9 ms |
33884 KB |
Output is correct |
22 |
Correct |
8 ms |
33628 KB |
Output is correct |
23 |
Correct |
8 ms |
33720 KB |
Output is correct |
24 |
Correct |
8 ms |
34136 KB |
Output is correct |
25 |
Correct |
9 ms |
33948 KB |
Output is correct |
26 |
Correct |
8 ms |
33880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33628 KB |
Output is correct |
2 |
Correct |
368 ms |
45140 KB |
Output is correct |
3 |
Correct |
547 ms |
57012 KB |
Output is correct |
4 |
Correct |
341 ms |
45248 KB |
Output is correct |
5 |
Correct |
336 ms |
45252 KB |
Output is correct |
6 |
Correct |
424 ms |
46868 KB |
Output is correct |
7 |
Correct |
296 ms |
45216 KB |
Output is correct |
8 |
Correct |
457 ms |
57496 KB |
Output is correct |
9 |
Correct |
265 ms |
45660 KB |
Output is correct |
10 |
Correct |
6 ms |
33624 KB |
Output is correct |
11 |
Correct |
433 ms |
45144 KB |
Output is correct |
12 |
Correct |
485 ms |
65592 KB |
Output is correct |
13 |
Correct |
334 ms |
50324 KB |
Output is correct |
14 |
Correct |
397 ms |
51744 KB |
Output is correct |
15 |
Correct |
392 ms |
53740 KB |
Output is correct |
16 |
Correct |
273 ms |
51904 KB |
Output is correct |
17 |
Correct |
437 ms |
59428 KB |
Output is correct |
18 |
Correct |
259 ms |
51832 KB |
Output is correct |
19 |
Correct |
6 ms |
33628 KB |
Output is correct |
20 |
Correct |
369 ms |
51728 KB |
Output is correct |
21 |
Correct |
597 ms |
65508 KB |
Output is correct |
22 |
Correct |
321 ms |
50308 KB |
Output is correct |
23 |
Correct |
390 ms |
51784 KB |
Output is correct |
24 |
Correct |
373 ms |
50516 KB |
Output is correct |
25 |
Correct |
347 ms |
51796 KB |
Output is correct |
26 |
Correct |
348 ms |
50404 KB |
Output is correct |
27 |
Correct |
339 ms |
51540 KB |
Output is correct |
28 |
Correct |
369 ms |
53412 KB |
Output is correct |
29 |
Correct |
411 ms |
51836 KB |
Output is correct |
30 |
Correct |
337 ms |
50892 KB |
Output is correct |
31 |
Correct |
313 ms |
51752 KB |
Output is correct |
32 |
Correct |
415 ms |
59988 KB |
Output is correct |
33 |
Correct |
290 ms |
52144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33628 KB |
Output is correct |
3 |
Correct |
6 ms |
33624 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
6 ms |
33628 KB |
Output is correct |
6 |
Correct |
6 ms |
33628 KB |
Output is correct |
7 |
Correct |
6 ms |
33628 KB |
Output is correct |
8 |
Correct |
6 ms |
33628 KB |
Output is correct |
9 |
Correct |
6 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
6 ms |
33628 KB |
Output is correct |
12 |
Correct |
6 ms |
33628 KB |
Output is correct |
13 |
Correct |
368 ms |
45140 KB |
Output is correct |
14 |
Correct |
547 ms |
57012 KB |
Output is correct |
15 |
Correct |
341 ms |
45248 KB |
Output is correct |
16 |
Correct |
336 ms |
45252 KB |
Output is correct |
17 |
Correct |
424 ms |
46868 KB |
Output is correct |
18 |
Correct |
296 ms |
45216 KB |
Output is correct |
19 |
Correct |
457 ms |
57496 KB |
Output is correct |
20 |
Correct |
265 ms |
45660 KB |
Output is correct |
21 |
Correct |
6 ms |
33624 KB |
Output is correct |
22 |
Correct |
433 ms |
45144 KB |
Output is correct |
23 |
Correct |
485 ms |
65592 KB |
Output is correct |
24 |
Correct |
334 ms |
50324 KB |
Output is correct |
25 |
Correct |
397 ms |
51744 KB |
Output is correct |
26 |
Correct |
392 ms |
53740 KB |
Output is correct |
27 |
Correct |
273 ms |
51904 KB |
Output is correct |
28 |
Correct |
437 ms |
59428 KB |
Output is correct |
29 |
Correct |
259 ms |
51832 KB |
Output is correct |
30 |
Correct |
6 ms |
33624 KB |
Output is correct |
31 |
Correct |
8 ms |
33860 KB |
Output is correct |
32 |
Correct |
8 ms |
33884 KB |
Output is correct |
33 |
Correct |
8 ms |
33628 KB |
Output is correct |
34 |
Correct |
8 ms |
33624 KB |
Output is correct |
35 |
Correct |
7 ms |
33880 KB |
Output is correct |
36 |
Correct |
8 ms |
33628 KB |
Output is correct |
37 |
Correct |
8 ms |
33684 KB |
Output is correct |
38 |
Correct |
8 ms |
33628 KB |
Output is correct |
39 |
Correct |
9 ms |
33884 KB |
Output is correct |
40 |
Correct |
8 ms |
33628 KB |
Output is correct |
41 |
Correct |
8 ms |
33720 KB |
Output is correct |
42 |
Correct |
8 ms |
34136 KB |
Output is correct |
43 |
Correct |
9 ms |
33948 KB |
Output is correct |
44 |
Correct |
8 ms |
33880 KB |
Output is correct |
45 |
Correct |
6 ms |
33628 KB |
Output is correct |
46 |
Correct |
369 ms |
51728 KB |
Output is correct |
47 |
Correct |
597 ms |
65508 KB |
Output is correct |
48 |
Correct |
321 ms |
50308 KB |
Output is correct |
49 |
Correct |
390 ms |
51784 KB |
Output is correct |
50 |
Correct |
373 ms |
50516 KB |
Output is correct |
51 |
Correct |
347 ms |
51796 KB |
Output is correct |
52 |
Correct |
348 ms |
50404 KB |
Output is correct |
53 |
Correct |
339 ms |
51540 KB |
Output is correct |
54 |
Correct |
369 ms |
53412 KB |
Output is correct |
55 |
Correct |
411 ms |
51836 KB |
Output is correct |
56 |
Correct |
337 ms |
50892 KB |
Output is correct |
57 |
Correct |
313 ms |
51752 KB |
Output is correct |
58 |
Correct |
415 ms |
59988 KB |
Output is correct |
59 |
Correct |
290 ms |
52144 KB |
Output is correct |
60 |
Correct |
6 ms |
33628 KB |
Output is correct |
61 |
Correct |
381 ms |
54396 KB |
Output is correct |
62 |
Correct |
539 ms |
65236 KB |
Output is correct |
63 |
Correct |
349 ms |
52820 KB |
Output is correct |
64 |
Correct |
365 ms |
54284 KB |
Output is correct |
65 |
Correct |
406 ms |
52788 KB |
Output is correct |
66 |
Correct |
440 ms |
54180 KB |
Output is correct |
67 |
Correct |
433 ms |
53056 KB |
Output is correct |
68 |
Correct |
384 ms |
54396 KB |
Output is correct |
69 |
Correct |
384 ms |
55636 KB |
Output is correct |
70 |
Correct |
447 ms |
54244 KB |
Output is correct |
71 |
Correct |
352 ms |
53544 KB |
Output is correct |
72 |
Correct |
339 ms |
55112 KB |
Output is correct |
73 |
Correct |
439 ms |
61088 KB |
Output is correct |
74 |
Correct |
302 ms |
56456 KB |
Output is correct |