#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;
template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }
mt19937 rng( (uint32_t)chrono::steady_clock::now().time_since_epoch().count() );
int n;
vector<vector<pair<int, int> > > gr;
vector<int> eCost;
LL sum;
vector<LL> ans;
vector<LL> inCost;
LL getUp(int u, int pr) {
LL ret = 0;
for (auto e : gr[u]) if (e.first != pr) ret += getUp(e.first, u) + eCost[e.second ^ 1];
return ret;
}
void calInCost(int u, int pr) {
for (auto e : gr[u]) {
int v = e.first, id = e.second;
if (v == pr) continue ;
inCost[v] = inCost[u] - eCost[id ^ 1] + eCost[id];
calInCost(v, u);
}
}
void calAns1() {
inCost.assign(n, 0);
inCost[0] = getUp(0, -1);
calInCost(0, -1);
ans = { 0, *max_element(all(inCost) ) };
}
pair<LL, int> dfs(int u, int pr, tuple<LL, int, int> &mx) {
pair<LL, int> prv = { inCost[u], u };
for (auto e : gr[u]) {
int v = e.first, id = e.second;
if (v == pr) continue ;
auto cur = dfs(v, u, mx); cur.first += eCost[id] + eCost[id ^ 1];
asMx(mx, { prv.first + cur.first, prv.second, cur.second } );
asMx(prv, cur);
}
return prv;
}
int getRt() {
tuple<LL, int, int> mx{ 0, 0, 0 };
dfs(0, -1, mx);
return get<1>(mx);
}
int rt;
vector<pair<LL, int> > mxDep;
vector<int> par;
void calMxDep(int u, int pr, int upCost = 0) {
par[u] = pr;
mxDep[u] = { 0, u };
for (auto e : gr[u]) {
int v = e.first, id = e.second;
if (v == pr) continue ;
calMxDep(v, u, eCost[id]);
asMx(mxDep[u], mxDep[v]);
}
mxDep[u].first += upCost;
}
void prep() {
rt = getRt();
mxDep.assign(n, { -1, -1 } );
par.assign(n, -1);
calMxDep(rt, -1);
}
void calAns() {
calAns1();
prep();
LL curAns = inCost[rt];
priority_queue<pair<LL, int> > pq; pq.emplace(mxDep[rt]);
vector<bool> done(n, false); done[rt] = true;
for (int i = 2; i <= n; ++i) {
if (pq.empty() ) {
ans.emplace_back(curAns);
continue ;
}
LL bonus = pq.top().first; int u = pq.top().second; pq.pop();
ans.emplace_back(curAns += bonus);
vector<int> path;
for (; !done[u]; u = par[u]) {
path.emplace_back(u);
done[u] = true;
}
reverse(all(path) );
for (int j = 0; j + 1 < sz(path); ++j) {
u = path[j]; int v = path[j + 1];
for (auto e : gr[u]) {
int w = e.first;
if (w == par[u] || w == v) continue ;
pq.emplace(mxDep[w]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef FourLeafClover
freopen("input", "r", stdin);
#endif // FourLeafCLover
cin >> n;
gr.assign(n, {} );
for (int i = 0; i < n - 1; ++i) {
int u, v, c, d; cin >> u >> v >> c >> d; --u; --v;
gr[u].emplace_back(v, sz(eCost) ); eCost.emplace_back(c);
gr[v].emplace_back(u, sz(eCost) ); eCost.emplace_back(d);
sum += c + d;
}
calAns();
int q; cin >> q;
while (q--) {
int e; cin >> e;
cout << sum - ans[e] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
252 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
394 ms |
24040 KB |
Output is correct |
3 |
Correct |
443 ms |
37428 KB |
Output is correct |
4 |
Correct |
395 ms |
22768 KB |
Output is correct |
5 |
Correct |
375 ms |
24040 KB |
Output is correct |
6 |
Correct |
417 ms |
26056 KB |
Output is correct |
7 |
Correct |
331 ms |
24280 KB |
Output is correct |
8 |
Correct |
448 ms |
37224 KB |
Output is correct |
9 |
Correct |
246 ms |
26704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
407 ms |
24084 KB |
Output is correct |
3 |
Correct |
437 ms |
37052 KB |
Output is correct |
4 |
Correct |
387 ms |
23144 KB |
Output is correct |
5 |
Correct |
384 ms |
24040 KB |
Output is correct |
6 |
Correct |
406 ms |
26744 KB |
Output is correct |
7 |
Correct |
264 ms |
26320 KB |
Output is correct |
8 |
Correct |
438 ms |
33952 KB |
Output is correct |
9 |
Correct |
241 ms |
26704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
252 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
7 ms |
632 KB |
Output is correct |
14 |
Correct |
7 ms |
760 KB |
Output is correct |
15 |
Correct |
7 ms |
632 KB |
Output is correct |
16 |
Correct |
7 ms |
632 KB |
Output is correct |
17 |
Correct |
7 ms |
632 KB |
Output is correct |
18 |
Correct |
7 ms |
632 KB |
Output is correct |
19 |
Correct |
7 ms |
632 KB |
Output is correct |
20 |
Correct |
7 ms |
632 KB |
Output is correct |
21 |
Correct |
7 ms |
632 KB |
Output is correct |
22 |
Correct |
8 ms |
632 KB |
Output is correct |
23 |
Correct |
7 ms |
628 KB |
Output is correct |
24 |
Correct |
7 ms |
632 KB |
Output is correct |
25 |
Correct |
7 ms |
760 KB |
Output is correct |
26 |
Correct |
8 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
394 ms |
24040 KB |
Output is correct |
3 |
Correct |
443 ms |
37428 KB |
Output is correct |
4 |
Correct |
395 ms |
22768 KB |
Output is correct |
5 |
Correct |
375 ms |
24040 KB |
Output is correct |
6 |
Correct |
417 ms |
26056 KB |
Output is correct |
7 |
Correct |
331 ms |
24280 KB |
Output is correct |
8 |
Correct |
448 ms |
37224 KB |
Output is correct |
9 |
Correct |
246 ms |
26704 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
407 ms |
24084 KB |
Output is correct |
12 |
Correct |
437 ms |
37052 KB |
Output is correct |
13 |
Correct |
387 ms |
23144 KB |
Output is correct |
14 |
Correct |
384 ms |
24040 KB |
Output is correct |
15 |
Correct |
406 ms |
26744 KB |
Output is correct |
16 |
Correct |
264 ms |
26320 KB |
Output is correct |
17 |
Correct |
438 ms |
33952 KB |
Output is correct |
18 |
Correct |
241 ms |
26704 KB |
Output is correct |
19 |
Correct |
4 ms |
376 KB |
Output is correct |
20 |
Correct |
399 ms |
24040 KB |
Output is correct |
21 |
Correct |
448 ms |
37076 KB |
Output is correct |
22 |
Correct |
386 ms |
22620 KB |
Output is correct |
23 |
Correct |
399 ms |
24044 KB |
Output is correct |
24 |
Correct |
381 ms |
24120 KB |
Output is correct |
25 |
Correct |
392 ms |
24168 KB |
Output is correct |
26 |
Correct |
384 ms |
23912 KB |
Output is correct |
27 |
Correct |
382 ms |
24048 KB |
Output is correct |
28 |
Correct |
392 ms |
26216 KB |
Output is correct |
29 |
Correct |
401 ms |
24084 KB |
Output is correct |
30 |
Correct |
357 ms |
24044 KB |
Output is correct |
31 |
Correct |
303 ms |
24800 KB |
Output is correct |
32 |
Correct |
442 ms |
34280 KB |
Output is correct |
33 |
Correct |
238 ms |
26772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
252 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
380 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
394 ms |
24040 KB |
Output is correct |
14 |
Correct |
443 ms |
37428 KB |
Output is correct |
15 |
Correct |
395 ms |
22768 KB |
Output is correct |
16 |
Correct |
375 ms |
24040 KB |
Output is correct |
17 |
Correct |
417 ms |
26056 KB |
Output is correct |
18 |
Correct |
331 ms |
24280 KB |
Output is correct |
19 |
Correct |
448 ms |
37224 KB |
Output is correct |
20 |
Correct |
246 ms |
26704 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
407 ms |
24084 KB |
Output is correct |
23 |
Correct |
437 ms |
37052 KB |
Output is correct |
24 |
Correct |
387 ms |
23144 KB |
Output is correct |
25 |
Correct |
384 ms |
24040 KB |
Output is correct |
26 |
Correct |
406 ms |
26744 KB |
Output is correct |
27 |
Correct |
264 ms |
26320 KB |
Output is correct |
28 |
Correct |
438 ms |
33952 KB |
Output is correct |
29 |
Correct |
241 ms |
26704 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
7 ms |
632 KB |
Output is correct |
32 |
Correct |
7 ms |
760 KB |
Output is correct |
33 |
Correct |
7 ms |
632 KB |
Output is correct |
34 |
Correct |
7 ms |
632 KB |
Output is correct |
35 |
Correct |
7 ms |
632 KB |
Output is correct |
36 |
Correct |
7 ms |
632 KB |
Output is correct |
37 |
Correct |
7 ms |
632 KB |
Output is correct |
38 |
Correct |
7 ms |
632 KB |
Output is correct |
39 |
Correct |
7 ms |
632 KB |
Output is correct |
40 |
Correct |
8 ms |
632 KB |
Output is correct |
41 |
Correct |
7 ms |
628 KB |
Output is correct |
42 |
Correct |
7 ms |
632 KB |
Output is correct |
43 |
Correct |
7 ms |
760 KB |
Output is correct |
44 |
Correct |
8 ms |
632 KB |
Output is correct |
45 |
Correct |
4 ms |
376 KB |
Output is correct |
46 |
Correct |
399 ms |
24040 KB |
Output is correct |
47 |
Correct |
448 ms |
37076 KB |
Output is correct |
48 |
Correct |
386 ms |
22620 KB |
Output is correct |
49 |
Correct |
399 ms |
24044 KB |
Output is correct |
50 |
Correct |
381 ms |
24120 KB |
Output is correct |
51 |
Correct |
392 ms |
24168 KB |
Output is correct |
52 |
Correct |
384 ms |
23912 KB |
Output is correct |
53 |
Correct |
382 ms |
24048 KB |
Output is correct |
54 |
Correct |
392 ms |
26216 KB |
Output is correct |
55 |
Correct |
401 ms |
24084 KB |
Output is correct |
56 |
Correct |
357 ms |
24044 KB |
Output is correct |
57 |
Correct |
303 ms |
24800 KB |
Output is correct |
58 |
Correct |
442 ms |
34280 KB |
Output is correct |
59 |
Correct |
238 ms |
26772 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
458 ms |
26868 KB |
Output is correct |
62 |
Correct |
479 ms |
40020 KB |
Output is correct |
63 |
Correct |
431 ms |
26088 KB |
Output is correct |
64 |
Correct |
451 ms |
27024 KB |
Output is correct |
65 |
Correct |
434 ms |
27068 KB |
Output is correct |
66 |
Correct |
445 ms |
26984 KB |
Output is correct |
67 |
Correct |
425 ms |
26984 KB |
Output is correct |
68 |
Correct |
413 ms |
26984 KB |
Output is correct |
69 |
Correct |
445 ms |
29296 KB |
Output is correct |
70 |
Correct |
446 ms |
26848 KB |
Output is correct |
71 |
Correct |
418 ms |
26856 KB |
Output is correct |
72 |
Correct |
375 ms |
27608 KB |
Output is correct |
73 |
Correct |
487 ms |
37096 KB |
Output is correct |
74 |
Correct |
287 ms |
29648 KB |
Output is correct |