// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
set<pll> G[N];
ll ans[N];
set<pll> st;
ll mn = Inf;
ll sum = 0;
void DFS(int u, int p, ll in){
mn = min(mn, in);
for(auto [adj, w] : G[u]){
if(adj == p) continue;
int rw = G[adj].lower_bound(pll(u, -1)) -> S;
sum += rw;
DFS(adj, u, in + w - rw);
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
int u, v, c, d;
for(int i = 1; i < n; i++){
cin >> u >> v >> c >> d;
swap(c, d);
G[u].insert({v, c});
G[v].insert({u, d});
}
int cnt = 0;
for(int i = 1; i <= n; i++){
if((int) G[i].size() == 1){
cnt ++;
st.insert({G[i].begin() -> S, i});
// cerr << "! " << G[i].begin() -> S << ' ' << i << '\n';
}
}
DFS(1, -1, 0);
// debug(sum);
ans[1] = sum + mn;
ll sm = 0;
for(int i = n; i >= 2; i--){
while(cnt > i){
pll fr = *st.begin();
// cerr << "### " << fr.F << ' ' << fr.S << '\n';
st.erase(fr);
int adj = G[fr.S].begin() -> F;
G[fr.S].clear();
auto it = G[adj].lower_bound(pll(fr.S, -1));
G[adj].erase(it);
if(G[adj].size() == 1){
st.insert({fr.F + G[adj].begin() -> S, adj});
} else {
cnt --;
sm += fr.F;
}
}
ans[i] = sm;
}
// cout << '\n';
int q;
cin >> q;
for(int i = 1; i <= q; i++){
cin >> v;
// assert(v > 1);
cout << ans[v] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9688 KB |
Output is correct |
2 |
Correct |
6 ms |
9680 KB |
Output is correct |
3 |
Correct |
6 ms |
9652 KB |
Output is correct |
4 |
Correct |
6 ms |
9708 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9672 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
Output is correct |
2 |
Correct |
463 ms |
47732 KB |
Output is correct |
3 |
Correct |
354 ms |
55516 KB |
Output is correct |
4 |
Correct |
418 ms |
46368 KB |
Output is correct |
5 |
Correct |
526 ms |
48456 KB |
Output is correct |
6 |
Correct |
470 ms |
49424 KB |
Output is correct |
7 |
Correct |
588 ms |
50668 KB |
Output is correct |
8 |
Correct |
322 ms |
56300 KB |
Output is correct |
9 |
Correct |
675 ms |
55364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
461 ms |
41380 KB |
Output is correct |
3 |
Correct |
341 ms |
51156 KB |
Output is correct |
4 |
Correct |
409 ms |
41324 KB |
Output is correct |
5 |
Correct |
510 ms |
41936 KB |
Output is correct |
6 |
Correct |
447 ms |
43304 KB |
Output is correct |
7 |
Correct |
643 ms |
47684 KB |
Output is correct |
8 |
Correct |
374 ms |
47800 KB |
Output is correct |
9 |
Correct |
639 ms |
48752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9688 KB |
Output is correct |
2 |
Correct |
6 ms |
9680 KB |
Output is correct |
3 |
Correct |
6 ms |
9652 KB |
Output is correct |
4 |
Correct |
6 ms |
9708 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9672 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9668 KB |
Output is correct |
13 |
Correct |
8 ms |
10112 KB |
Output is correct |
14 |
Correct |
8 ms |
10108 KB |
Output is correct |
15 |
Correct |
8 ms |
10068 KB |
Output is correct |
16 |
Correct |
9 ms |
10088 KB |
Output is correct |
17 |
Correct |
9 ms |
9996 KB |
Output is correct |
18 |
Correct |
9 ms |
10116 KB |
Output is correct |
19 |
Correct |
9 ms |
10060 KB |
Output is correct |
20 |
Correct |
9 ms |
10060 KB |
Output is correct |
21 |
Correct |
9 ms |
10060 KB |
Output is correct |
22 |
Correct |
9 ms |
10056 KB |
Output is correct |
23 |
Correct |
9 ms |
10112 KB |
Output is correct |
24 |
Correct |
9 ms |
10100 KB |
Output is correct |
25 |
Correct |
9 ms |
10112 KB |
Output is correct |
26 |
Correct |
9 ms |
10120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9820 KB |
Output is correct |
2 |
Correct |
463 ms |
47732 KB |
Output is correct |
3 |
Correct |
354 ms |
55516 KB |
Output is correct |
4 |
Correct |
418 ms |
46368 KB |
Output is correct |
5 |
Correct |
526 ms |
48456 KB |
Output is correct |
6 |
Correct |
470 ms |
49424 KB |
Output is correct |
7 |
Correct |
588 ms |
50668 KB |
Output is correct |
8 |
Correct |
322 ms |
56300 KB |
Output is correct |
9 |
Correct |
675 ms |
55364 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
461 ms |
41380 KB |
Output is correct |
12 |
Correct |
341 ms |
51156 KB |
Output is correct |
13 |
Correct |
409 ms |
41324 KB |
Output is correct |
14 |
Correct |
510 ms |
41936 KB |
Output is correct |
15 |
Correct |
447 ms |
43304 KB |
Output is correct |
16 |
Correct |
643 ms |
47684 KB |
Output is correct |
17 |
Correct |
374 ms |
47800 KB |
Output is correct |
18 |
Correct |
639 ms |
48752 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
502 ms |
47736 KB |
Output is correct |
21 |
Correct |
317 ms |
57852 KB |
Output is correct |
22 |
Correct |
416 ms |
46324 KB |
Output is correct |
23 |
Correct |
477 ms |
47796 KB |
Output is correct |
24 |
Correct |
449 ms |
46620 KB |
Output is correct |
25 |
Correct |
468 ms |
47736 KB |
Output is correct |
26 |
Correct |
469 ms |
46740 KB |
Output is correct |
27 |
Correct |
516 ms |
48404 KB |
Output is correct |
28 |
Correct |
455 ms |
49416 KB |
Output is correct |
29 |
Correct |
442 ms |
47296 KB |
Output is correct |
30 |
Correct |
482 ms |
48068 KB |
Output is correct |
31 |
Correct |
614 ms |
51560 KB |
Output is correct |
32 |
Correct |
375 ms |
54636 KB |
Output is correct |
33 |
Correct |
644 ms |
55288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9688 KB |
Output is correct |
2 |
Correct |
6 ms |
9680 KB |
Output is correct |
3 |
Correct |
6 ms |
9652 KB |
Output is correct |
4 |
Correct |
6 ms |
9708 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9672 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9820 KB |
Output is correct |
13 |
Correct |
463 ms |
47732 KB |
Output is correct |
14 |
Correct |
354 ms |
55516 KB |
Output is correct |
15 |
Correct |
418 ms |
46368 KB |
Output is correct |
16 |
Correct |
526 ms |
48456 KB |
Output is correct |
17 |
Correct |
470 ms |
49424 KB |
Output is correct |
18 |
Correct |
588 ms |
50668 KB |
Output is correct |
19 |
Correct |
322 ms |
56300 KB |
Output is correct |
20 |
Correct |
675 ms |
55364 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
461 ms |
41380 KB |
Output is correct |
23 |
Correct |
341 ms |
51156 KB |
Output is correct |
24 |
Correct |
409 ms |
41324 KB |
Output is correct |
25 |
Correct |
510 ms |
41936 KB |
Output is correct |
26 |
Correct |
447 ms |
43304 KB |
Output is correct |
27 |
Correct |
643 ms |
47684 KB |
Output is correct |
28 |
Correct |
374 ms |
47800 KB |
Output is correct |
29 |
Correct |
639 ms |
48752 KB |
Output is correct |
30 |
Correct |
6 ms |
9668 KB |
Output is correct |
31 |
Correct |
8 ms |
10112 KB |
Output is correct |
32 |
Correct |
8 ms |
10108 KB |
Output is correct |
33 |
Correct |
8 ms |
10068 KB |
Output is correct |
34 |
Correct |
9 ms |
10088 KB |
Output is correct |
35 |
Correct |
9 ms |
9996 KB |
Output is correct |
36 |
Correct |
9 ms |
10116 KB |
Output is correct |
37 |
Correct |
9 ms |
10060 KB |
Output is correct |
38 |
Correct |
9 ms |
10060 KB |
Output is correct |
39 |
Correct |
9 ms |
10060 KB |
Output is correct |
40 |
Correct |
9 ms |
10056 KB |
Output is correct |
41 |
Correct |
9 ms |
10112 KB |
Output is correct |
42 |
Correct |
9 ms |
10100 KB |
Output is correct |
43 |
Correct |
9 ms |
10112 KB |
Output is correct |
44 |
Correct |
9 ms |
10120 KB |
Output is correct |
45 |
Correct |
6 ms |
9676 KB |
Output is correct |
46 |
Correct |
502 ms |
47736 KB |
Output is correct |
47 |
Correct |
317 ms |
57852 KB |
Output is correct |
48 |
Correct |
416 ms |
46324 KB |
Output is correct |
49 |
Correct |
477 ms |
47796 KB |
Output is correct |
50 |
Correct |
449 ms |
46620 KB |
Output is correct |
51 |
Correct |
468 ms |
47736 KB |
Output is correct |
52 |
Correct |
469 ms |
46740 KB |
Output is correct |
53 |
Correct |
516 ms |
48404 KB |
Output is correct |
54 |
Correct |
455 ms |
49416 KB |
Output is correct |
55 |
Correct |
442 ms |
47296 KB |
Output is correct |
56 |
Correct |
482 ms |
48068 KB |
Output is correct |
57 |
Correct |
614 ms |
51560 KB |
Output is correct |
58 |
Correct |
375 ms |
54636 KB |
Output is correct |
59 |
Correct |
644 ms |
55288 KB |
Output is correct |
60 |
Correct |
6 ms |
9696 KB |
Output is correct |
61 |
Correct |
558 ms |
50380 KB |
Output is correct |
62 |
Correct |
341 ms |
57168 KB |
Output is correct |
63 |
Correct |
440 ms |
49020 KB |
Output is correct |
64 |
Correct |
499 ms |
50372 KB |
Output is correct |
65 |
Correct |
502 ms |
49092 KB |
Output is correct |
66 |
Correct |
509 ms |
50332 KB |
Output is correct |
67 |
Correct |
491 ms |
49088 KB |
Output is correct |
68 |
Correct |
557 ms |
51060 KB |
Output is correct |
69 |
Correct |
524 ms |
51920 KB |
Output is correct |
70 |
Correct |
470 ms |
49476 KB |
Output is correct |
71 |
Correct |
528 ms |
51016 KB |
Output is correct |
72 |
Correct |
639 ms |
54288 KB |
Output is correct |
73 |
Correct |
447 ms |
55644 KB |
Output is correct |
74 |
Correct |
713 ms |
59544 KB |
Output is correct |