#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 200010;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, pll>
#define pli pair<ll, int>
#define mp make_pair
int n, q;
//we want the seg-tree to go in euler tour order
int qus[maxn];
vector<pil> adj[maxn];
ll ans[maxn]; //store ans for each value
ll dep[maxn]; //distance from root to me
int par[maxn];
ll plink[maxn];
ll pplink[maxn];
int rt; //some global root variable
ll esum = 0LL; //total edge sum
//barebones dfs
void dfs(int u, int p = -1) {
if (p == -1) {
dep[u] = 0LL;
par[u] = -1;
plink[u] = 0LL;
}
for (pil vp : adj[u]) {
if (vp.first == p) continue;
dep[vp.first] = dep[u] + vp.second.first; //dep is length to bot
par[vp.first] = u;
plink[vp.first] = vp.second.second;
pplink[vp.first] = vp.second.first;
dfs(vp.first, u);
}
}
void dfs1(int u, ll msum = 0LL) {
ans[1] = min(ans[1], esum - msum);
for (pil vp : adj[u]) {
if (vp.first != par[u]) {
dfs1(vp.first, msum - vp.second.second +
vp.second.first);
}
}
}
void go1() {
//finds the answer for 1 by itself
//when I go to a child, I reverse that edge
//start with all going down
ans[1] = esum;
ll csum = 0LL;
for (int i = 1; i <= n; i++) {
if (i != rt) csum += plink[i];
}
dfs1(rt, csum);
}
pli mdepth[maxn]; //want maxdepth
pii cg;
void dfs2(int u, ll msum) {
//msum is the sum of everything in
//consider me to be an lca
mdepth[u] = {dep[u], u};
vector<pli> ops;
for (pil vp : adj[u]) {
if (vp.first == par[u]) continue;
dfs2(vp.first, msum - vp.second.second);
ops.push_back(mdepth[vp.first]);
}
sort(ops.begin(), ops.end());
reverse(ops.begin(), ops.end());
if (ops.size()) mdepth[u] = ops[0];
if (ops.size() < 2) return;
msum += ops[0].first + ops[1].first - dep[u];
if (esum - msum < ans[2]) {
ans[2] = esum - msum;
cg = mp(ops[0].second, ops[1].second);
}
}
pii go2() {
//we consider everything as the lca
//then we sacrifice a certain amount that "goes down"
//start with all of the par-links in (par points up)
ans[2] = esum;
// we lose some of the par-links
ll csum = 0LL;
for (int i = 1; i <= n; i++) {
if (i != rt) csum += plink[i];
}
dfs2(rt, csum);
return cg;
}
int st[maxn];
int en[maxn];
vector<int> stuff;
void etour(int u) {
st[u] = stuff.size();
stuff.push_back(u);
for (pil vp : adj[u]) {
if (vp.first != par[u]) {
etour(vp.first);
}
}
en[u] = stuff.size()-1;
}
pli seg[maxn*4]; //a max seg tree
ll lazy[maxn*4];
void delaze(int ss, int se, int si) {
seg[si] = mp(seg[si].first + lazy[si], seg[si].second);
if (lazy[si] && ss != se) {
lazy[si*2+1] += lazy[si];
lazy[si*2+2] += lazy[si];
}
lazy[si] = 0;
}
pli query() {
//get the maximum
delaze(0, n-1, 0);
return seg[0];
}
void upd(int us, int ue, ll diff, int ss = 0, int se = n-1,
int si = 0) {
delaze(ss, se, si);
if (us > ue || ss > se || us > se || ue < ss) return;
if (us <= ss && se <= ue) {
lazy[si] += diff;
delaze(ss, se, si);
return;
}
int mid = (ss+se)/2;
upd(us, ue, diff, ss, mid, si*2+1);
upd(us, ue, diff, mid+1, se, si*2+2);
seg[si] = max(seg[si*2+1], seg[si*2+2]);
}
bool isrem[maxn];
void buildtree(int ss = 0, int se = n-1, int si = 0) {
if (ss == se) {
seg[si] = {dep[stuff[ss]], stuff[ss]};
return;
}
int mid = (ss+se)/2;
buildtree(ss, mid, si*2+1);
buildtree(mid+1, se, si*2+2);
seg[si] = max(seg[si*2+1], seg[si*2+2]);
}
void proc(int u) {
//remove this node
//go up the parents list
//all children of me lose going up (keep doing this)
while (!isrem[u]) {
// cout << " ----- " << u << endl;
isrem[u] = true;
upd(st[u], en[u], 0-pplink[u]);
u = par[u];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int a, b;
ll c, d;
for (int i = 0; i < n-1; i++) {
cin >> a >> b >> c >> d;
adj[a].emplace_back(b, mp(c, d));
adj[b].emplace_back(a, mp(d, c));
esum += c;
esum += d;
}
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> qus[i];
}
if (n == 2) {
//just bash
for (int i = 1; i <= q; i++) {
if (qus[i] == 2) {
cout << 0 << endl;
}
else {
cout << min(adj[1][0].second.first,
adj[1][0].second.second) << endl;
}
}
return 0;
}
//now we want to root at a non-leaf
rt = 1;
for (int i = 2; i <= n; i++) {
if (adj[i].size() != 1) rt = i;
}
dfs(rt);
go1();
pii vp = go2();
// cout << "got 2 : " << vp.first << " " << vp.second << endl;
//we get the two guys
rt = par[vp.first];
dfs(rt); //reset everything (yea)
etour(rt);
// cout << "done the dfs" << endl;
ll cans = ans[2]; //starting answer (will increase)
//now we do the greedy thing
buildtree(); //just start all as it is
// cout << "built the tree" << endl;
isrem[rt] = true; //basically is removed
proc(vp.first);
proc(vp.second);
for (int i = 3; i <= n; i++) {
pli tmp = query();
if (tmp.first != 0) {
cans -= tmp.first;
proc(tmp.second);
// cout << "removing " << tmp.second << " "
// << tmp.first << endl;
}
else {
assert(cans == 0LL);
}
ans[i] = cans;
}
//now we are just printing out answer (for now - 1/2)
for (int i = 1; i <= q; i++) {
cout << ans[qus[i]] << '\n';
}
cout.flush();
}
//calculate the answer for one
//use a dp to calculate the answer for two (root at non-leaf)
//greedily add nodes until we get to each val (if none left - do nothing)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5092 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5092 KB |
Output is correct |
2 |
Correct |
807 ms |
43384 KB |
Output is correct |
3 |
Correct |
844 ms |
52940 KB |
Output is correct |
4 |
Correct |
709 ms |
43436 KB |
Output is correct |
5 |
Correct |
773 ms |
44152 KB |
Output is correct |
6 |
Correct |
792 ms |
45260 KB |
Output is correct |
7 |
Correct |
690 ms |
43408 KB |
Output is correct |
8 |
Correct |
885 ms |
54200 KB |
Output is correct |
9 |
Correct |
564 ms |
44512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Correct |
831 ms |
43328 KB |
Output is correct |
3 |
Correct |
933 ms |
52972 KB |
Output is correct |
4 |
Correct |
674 ms |
43500 KB |
Output is correct |
5 |
Correct |
741 ms |
44076 KB |
Output is correct |
6 |
Correct |
786 ms |
45144 KB |
Output is correct |
7 |
Correct |
571 ms |
44616 KB |
Output is correct |
8 |
Correct |
863 ms |
52732 KB |
Output is correct |
9 |
Correct |
586 ms |
44500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5092 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
10 ms |
5504 KB |
Output is correct |
14 |
Correct |
11 ms |
5632 KB |
Output is correct |
15 |
Correct |
11 ms |
5504 KB |
Output is correct |
16 |
Correct |
9 ms |
5552 KB |
Output is correct |
17 |
Correct |
11 ms |
5504 KB |
Output is correct |
18 |
Correct |
11 ms |
5540 KB |
Output is correct |
19 |
Correct |
11 ms |
5632 KB |
Output is correct |
20 |
Correct |
12 ms |
5476 KB |
Output is correct |
21 |
Correct |
10 ms |
5504 KB |
Output is correct |
22 |
Correct |
12 ms |
5504 KB |
Output is correct |
23 |
Correct |
10 ms |
5504 KB |
Output is correct |
24 |
Correct |
9 ms |
5632 KB |
Output is correct |
25 |
Correct |
10 ms |
5632 KB |
Output is correct |
26 |
Correct |
11 ms |
5504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5092 KB |
Output is correct |
2 |
Correct |
807 ms |
43384 KB |
Output is correct |
3 |
Correct |
844 ms |
52940 KB |
Output is correct |
4 |
Correct |
709 ms |
43436 KB |
Output is correct |
5 |
Correct |
773 ms |
44152 KB |
Output is correct |
6 |
Correct |
792 ms |
45260 KB |
Output is correct |
7 |
Correct |
690 ms |
43408 KB |
Output is correct |
8 |
Correct |
885 ms |
54200 KB |
Output is correct |
9 |
Correct |
564 ms |
44512 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
831 ms |
43328 KB |
Output is correct |
12 |
Correct |
933 ms |
52972 KB |
Output is correct |
13 |
Correct |
674 ms |
43500 KB |
Output is correct |
14 |
Correct |
741 ms |
44076 KB |
Output is correct |
15 |
Correct |
786 ms |
45144 KB |
Output is correct |
16 |
Correct |
571 ms |
44616 KB |
Output is correct |
17 |
Correct |
863 ms |
52732 KB |
Output is correct |
18 |
Correct |
586 ms |
44500 KB |
Output is correct |
19 |
Correct |
6 ms |
5092 KB |
Output is correct |
20 |
Correct |
754 ms |
43460 KB |
Output is correct |
21 |
Correct |
774 ms |
52764 KB |
Output is correct |
22 |
Correct |
807 ms |
43372 KB |
Output is correct |
23 |
Correct |
782 ms |
43652 KB |
Output is correct |
24 |
Correct |
796 ms |
43560 KB |
Output is correct |
25 |
Correct |
756 ms |
43632 KB |
Output is correct |
26 |
Correct |
739 ms |
43624 KB |
Output is correct |
27 |
Correct |
680 ms |
43308 KB |
Output is correct |
28 |
Correct |
803 ms |
45264 KB |
Output is correct |
29 |
Correct |
764 ms |
43936 KB |
Output is correct |
30 |
Correct |
725 ms |
43808 KB |
Output is correct |
31 |
Correct |
726 ms |
42948 KB |
Output is correct |
32 |
Correct |
894 ms |
51968 KB |
Output is correct |
33 |
Correct |
529 ms |
44664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5092 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5092 KB |
Output is correct |
13 |
Correct |
807 ms |
43384 KB |
Output is correct |
14 |
Correct |
844 ms |
52940 KB |
Output is correct |
15 |
Correct |
709 ms |
43436 KB |
Output is correct |
16 |
Correct |
773 ms |
44152 KB |
Output is correct |
17 |
Correct |
792 ms |
45260 KB |
Output is correct |
18 |
Correct |
690 ms |
43408 KB |
Output is correct |
19 |
Correct |
885 ms |
54200 KB |
Output is correct |
20 |
Correct |
564 ms |
44512 KB |
Output is correct |
21 |
Correct |
6 ms |
4992 KB |
Output is correct |
22 |
Correct |
831 ms |
43328 KB |
Output is correct |
23 |
Correct |
933 ms |
52972 KB |
Output is correct |
24 |
Correct |
674 ms |
43500 KB |
Output is correct |
25 |
Correct |
741 ms |
44076 KB |
Output is correct |
26 |
Correct |
786 ms |
45144 KB |
Output is correct |
27 |
Correct |
571 ms |
44616 KB |
Output is correct |
28 |
Correct |
863 ms |
52732 KB |
Output is correct |
29 |
Correct |
586 ms |
44500 KB |
Output is correct |
30 |
Correct |
7 ms |
5120 KB |
Output is correct |
31 |
Correct |
10 ms |
5504 KB |
Output is correct |
32 |
Correct |
11 ms |
5632 KB |
Output is correct |
33 |
Correct |
11 ms |
5504 KB |
Output is correct |
34 |
Correct |
9 ms |
5552 KB |
Output is correct |
35 |
Correct |
11 ms |
5504 KB |
Output is correct |
36 |
Correct |
11 ms |
5540 KB |
Output is correct |
37 |
Correct |
11 ms |
5632 KB |
Output is correct |
38 |
Correct |
12 ms |
5476 KB |
Output is correct |
39 |
Correct |
10 ms |
5504 KB |
Output is correct |
40 |
Correct |
12 ms |
5504 KB |
Output is correct |
41 |
Correct |
10 ms |
5504 KB |
Output is correct |
42 |
Correct |
9 ms |
5632 KB |
Output is correct |
43 |
Correct |
10 ms |
5632 KB |
Output is correct |
44 |
Correct |
11 ms |
5504 KB |
Output is correct |
45 |
Correct |
6 ms |
5092 KB |
Output is correct |
46 |
Correct |
754 ms |
43460 KB |
Output is correct |
47 |
Correct |
774 ms |
52764 KB |
Output is correct |
48 |
Correct |
807 ms |
43372 KB |
Output is correct |
49 |
Correct |
782 ms |
43652 KB |
Output is correct |
50 |
Correct |
796 ms |
43560 KB |
Output is correct |
51 |
Correct |
756 ms |
43632 KB |
Output is correct |
52 |
Correct |
739 ms |
43624 KB |
Output is correct |
53 |
Correct |
680 ms |
43308 KB |
Output is correct |
54 |
Correct |
803 ms |
45264 KB |
Output is correct |
55 |
Correct |
764 ms |
43936 KB |
Output is correct |
56 |
Correct |
725 ms |
43808 KB |
Output is correct |
57 |
Correct |
726 ms |
42948 KB |
Output is correct |
58 |
Correct |
894 ms |
51968 KB |
Output is correct |
59 |
Correct |
529 ms |
44664 KB |
Output is correct |
60 |
Correct |
7 ms |
5120 KB |
Output is correct |
61 |
Correct |
864 ms |
53240 KB |
Output is correct |
62 |
Correct |
820 ms |
61800 KB |
Output is correct |
63 |
Correct |
844 ms |
51820 KB |
Output is correct |
64 |
Correct |
778 ms |
53488 KB |
Output is correct |
65 |
Correct |
867 ms |
52048 KB |
Output is correct |
66 |
Correct |
913 ms |
53484 KB |
Output is correct |
67 |
Correct |
790 ms |
52180 KB |
Output is correct |
68 |
Correct |
872 ms |
53132 KB |
Output is correct |
69 |
Correct |
938 ms |
54756 KB |
Output is correct |
70 |
Correct |
838 ms |
53832 KB |
Output is correct |
71 |
Correct |
817 ms |
52816 KB |
Output is correct |
72 |
Correct |
782 ms |
53816 KB |
Output is correct |
73 |
Correct |
952 ms |
62480 KB |
Output is correct |
74 |
Correct |
696 ms |
55984 KB |
Output is correct |