#include <bits/stdc++.h>
#define int long long
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
struct Edge {
int u, v, p;
};
int nbNode, nbEdge, nbReq;
vector<Edge> edges;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> nbNode >> nbEdge;
edges.resize(nbEdge);
for (auto &[u,v,p] : edges) {
cin >> u >> v >> p;
--u, --v;
p *= 2;
}
auto compZero = [&] (Edge a, Edge b) { return a.p < b.p; };
sort(all(edges), compZero);
vector<vi> adjTree(nbNode);
auto add = [&] (int iEdge) {
const auto [u,v,_] = edges[iEdge];
adjTree[u].push_back(iEdge);
adjTree[v].push_back(iEdge);
};
auto remove = [&] (int iEdge) {
const auto [u,v,_] = edges[iEdge];
for (int c : {u, v})
adjTree[c].erase(find(all(adjTree[c]), iEdge));
};
auto Dfs = [&] (auto dfs, int node, int parent, int dest, pii &ans) -> bool {
if (node == dest) {
return true;
}
for (int iEdge : adjTree[node]) {
const auto [u,v,p] = edges[iEdge];
int child = u^v^node;
if (child == parent) continue;
if(dfs(dfs, child, node, dest, ans)) {
chmin(ans, make_pair(p, iEdge));
return true;
}
}
return false;
};
vector<pii> events;
int realAdds = 0;
rep(iCur, 0, nbEdge) {
auto &cur = edges[iCur];
pii ret {1e18, 0};
if (Dfs(Dfs, cur.u, -1, cur.v, ret)) {
auto [minTime, argMin] = ret;
int betterTime = (cur.p + minTime)/2;
remove(argMin);
add(iCur);
events.emplace_back(betterTime, minTime);
events.emplace_back(betterTime, -cur.p);
} else if (realAdds < nbNode-1) {
++realAdds;
add(iCur);
events.emplace_back(0, -cur.p);
}
}
sort(all(events));
cin >> nbReq;
vector<int> curMst;
int ptr = 0;
rep(iReq, 0, nbReq) {
int X; cin >> X; X *= 2;
while (ptr < sz(events) && events[ptr].first <= X) {
int u = events[ptr].second;
if (u < 0)
curMst.push_back(-u);
else
curMst.erase(find(all(curMst), u));
++ptr;
}
int ans = 0;
for (int p : curMst) {
ans += abs(p-X);
}
cout << ans/2 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
521 ms |
8616 KB |
Output is correct |
21 |
Correct |
302 ms |
8536 KB |
Output is correct |
22 |
Correct |
427 ms |
8744 KB |
Output is correct |
23 |
Correct |
443 ms |
8676 KB |
Output is correct |
24 |
Correct |
414 ms |
8728 KB |
Output is correct |
25 |
Correct |
477 ms |
8656 KB |
Output is correct |
26 |
Correct |
488 ms |
8592 KB |
Output is correct |
27 |
Correct |
492 ms |
8636 KB |
Output is correct |
28 |
Correct |
497 ms |
8776 KB |
Output is correct |
29 |
Correct |
538 ms |
8552 KB |
Output is correct |
30 |
Correct |
491 ms |
8612 KB |
Output is correct |
31 |
Correct |
489 ms |
8572 KB |
Output is correct |
32 |
Correct |
489 ms |
8604 KB |
Output is correct |
33 |
Correct |
490 ms |
8724 KB |
Output is correct |
34 |
Correct |
470 ms |
8724 KB |
Output is correct |
35 |
Correct |
481 ms |
8628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
783 ms |
28460 KB |
Output is correct |
5 |
Correct |
788 ms |
28288 KB |
Output is correct |
6 |
Correct |
787 ms |
28328 KB |
Output is correct |
7 |
Correct |
602 ms |
30380 KB |
Output is correct |
8 |
Correct |
641 ms |
30340 KB |
Output is correct |
9 |
Correct |
627 ms |
30320 KB |
Output is correct |
10 |
Correct |
800 ms |
28372 KB |
Output is correct |
11 |
Correct |
602 ms |
30396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
542 ms |
22260 KB |
Output is correct |
21 |
Correct |
573 ms |
22380 KB |
Output is correct |
22 |
Correct |
574 ms |
22288 KB |
Output is correct |
23 |
Correct |
609 ms |
22400 KB |
Output is correct |
24 |
Correct |
630 ms |
22348 KB |
Output is correct |
25 |
Correct |
599 ms |
22328 KB |
Output is correct |
26 |
Correct |
589 ms |
22324 KB |
Output is correct |
27 |
Correct |
556 ms |
22380 KB |
Output is correct |
28 |
Correct |
565 ms |
22220 KB |
Output is correct |
29 |
Correct |
564 ms |
22352 KB |
Output is correct |
30 |
Correct |
537 ms |
22392 KB |
Output is correct |
31 |
Correct |
583 ms |
22120 KB |
Output is correct |
32 |
Correct |
550 ms |
22868 KB |
Output is correct |
33 |
Correct |
562 ms |
22164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
521 ms |
8616 KB |
Output is correct |
21 |
Correct |
302 ms |
8536 KB |
Output is correct |
22 |
Correct |
427 ms |
8744 KB |
Output is correct |
23 |
Correct |
443 ms |
8676 KB |
Output is correct |
24 |
Correct |
414 ms |
8728 KB |
Output is correct |
25 |
Correct |
477 ms |
8656 KB |
Output is correct |
26 |
Correct |
488 ms |
8592 KB |
Output is correct |
27 |
Correct |
492 ms |
8636 KB |
Output is correct |
28 |
Correct |
497 ms |
8776 KB |
Output is correct |
29 |
Correct |
538 ms |
8552 KB |
Output is correct |
30 |
Correct |
491 ms |
8612 KB |
Output is correct |
31 |
Correct |
489 ms |
8572 KB |
Output is correct |
32 |
Correct |
489 ms |
8604 KB |
Output is correct |
33 |
Correct |
490 ms |
8724 KB |
Output is correct |
34 |
Correct |
470 ms |
8724 KB |
Output is correct |
35 |
Correct |
481 ms |
8628 KB |
Output is correct |
36 |
Correct |
507 ms |
8864 KB |
Output is correct |
37 |
Correct |
317 ms |
8828 KB |
Output is correct |
38 |
Correct |
474 ms |
8836 KB |
Output is correct |
39 |
Correct |
464 ms |
8768 KB |
Output is correct |
40 |
Correct |
431 ms |
8848 KB |
Output is correct |
41 |
Correct |
507 ms |
8856 KB |
Output is correct |
42 |
Correct |
522 ms |
8812 KB |
Output is correct |
43 |
Correct |
517 ms |
8768 KB |
Output is correct |
44 |
Correct |
499 ms |
8804 KB |
Output is correct |
45 |
Correct |
559 ms |
8828 KB |
Output is correct |
46 |
Correct |
503 ms |
8804 KB |
Output is correct |
47 |
Correct |
505 ms |
8844 KB |
Output is correct |
48 |
Correct |
506 ms |
8812 KB |
Output is correct |
49 |
Correct |
501 ms |
8816 KB |
Output is correct |
50 |
Correct |
497 ms |
8924 KB |
Output is correct |
51 |
Correct |
499 ms |
8836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
521 ms |
8616 KB |
Output is correct |
21 |
Correct |
302 ms |
8536 KB |
Output is correct |
22 |
Correct |
427 ms |
8744 KB |
Output is correct |
23 |
Correct |
443 ms |
8676 KB |
Output is correct |
24 |
Correct |
414 ms |
8728 KB |
Output is correct |
25 |
Correct |
477 ms |
8656 KB |
Output is correct |
26 |
Correct |
488 ms |
8592 KB |
Output is correct |
27 |
Correct |
492 ms |
8636 KB |
Output is correct |
28 |
Correct |
497 ms |
8776 KB |
Output is correct |
29 |
Correct |
538 ms |
8552 KB |
Output is correct |
30 |
Correct |
491 ms |
8612 KB |
Output is correct |
31 |
Correct |
489 ms |
8572 KB |
Output is correct |
32 |
Correct |
489 ms |
8604 KB |
Output is correct |
33 |
Correct |
490 ms |
8724 KB |
Output is correct |
34 |
Correct |
470 ms |
8724 KB |
Output is correct |
35 |
Correct |
481 ms |
8628 KB |
Output is correct |
36 |
Correct |
1 ms |
320 KB |
Output is correct |
37 |
Correct |
0 ms |
324 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
783 ms |
28460 KB |
Output is correct |
40 |
Correct |
788 ms |
28288 KB |
Output is correct |
41 |
Correct |
787 ms |
28328 KB |
Output is correct |
42 |
Correct |
602 ms |
30380 KB |
Output is correct |
43 |
Correct |
641 ms |
30340 KB |
Output is correct |
44 |
Correct |
627 ms |
30320 KB |
Output is correct |
45 |
Correct |
800 ms |
28372 KB |
Output is correct |
46 |
Correct |
602 ms |
30396 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
542 ms |
22260 KB |
Output is correct |
49 |
Correct |
573 ms |
22380 KB |
Output is correct |
50 |
Correct |
574 ms |
22288 KB |
Output is correct |
51 |
Correct |
609 ms |
22400 KB |
Output is correct |
52 |
Correct |
630 ms |
22348 KB |
Output is correct |
53 |
Correct |
599 ms |
22328 KB |
Output is correct |
54 |
Correct |
589 ms |
22324 KB |
Output is correct |
55 |
Correct |
556 ms |
22380 KB |
Output is correct |
56 |
Correct |
565 ms |
22220 KB |
Output is correct |
57 |
Correct |
564 ms |
22352 KB |
Output is correct |
58 |
Correct |
537 ms |
22392 KB |
Output is correct |
59 |
Correct |
583 ms |
22120 KB |
Output is correct |
60 |
Correct |
550 ms |
22868 KB |
Output is correct |
61 |
Correct |
562 ms |
22164 KB |
Output is correct |
62 |
Correct |
507 ms |
8864 KB |
Output is correct |
63 |
Correct |
317 ms |
8828 KB |
Output is correct |
64 |
Correct |
474 ms |
8836 KB |
Output is correct |
65 |
Correct |
464 ms |
8768 KB |
Output is correct |
66 |
Correct |
431 ms |
8848 KB |
Output is correct |
67 |
Correct |
507 ms |
8856 KB |
Output is correct |
68 |
Correct |
522 ms |
8812 KB |
Output is correct |
69 |
Correct |
517 ms |
8768 KB |
Output is correct |
70 |
Correct |
499 ms |
8804 KB |
Output is correct |
71 |
Correct |
559 ms |
8828 KB |
Output is correct |
72 |
Correct |
503 ms |
8804 KB |
Output is correct |
73 |
Correct |
505 ms |
8844 KB |
Output is correct |
74 |
Correct |
506 ms |
8812 KB |
Output is correct |
75 |
Correct |
501 ms |
8816 KB |
Output is correct |
76 |
Correct |
497 ms |
8924 KB |
Output is correct |
77 |
Correct |
499 ms |
8836 KB |
Output is correct |
78 |
Correct |
1028 ms |
27392 KB |
Output is correct |
79 |
Correct |
813 ms |
29392 KB |
Output is correct |
80 |
Correct |
969 ms |
28388 KB |
Output is correct |
81 |
Correct |
975 ms |
28396 KB |
Output is correct |
82 |
Correct |
949 ms |
27416 KB |
Output is correct |
83 |
Correct |
1025 ms |
27352 KB |
Output is correct |
84 |
Correct |
1016 ms |
27284 KB |
Output is correct |
85 |
Correct |
1031 ms |
27376 KB |
Output is correct |
86 |
Correct |
1014 ms |
27452 KB |
Output is correct |
87 |
Correct |
1080 ms |
28996 KB |
Output is correct |
88 |
Correct |
1028 ms |
27388 KB |
Output is correct |
89 |
Correct |
1014 ms |
27392 KB |
Output is correct |
90 |
Correct |
1039 ms |
27468 KB |
Output is correct |
91 |
Correct |
1063 ms |
27320 KB |
Output is correct |
92 |
Correct |
1027 ms |
30168 KB |
Output is correct |
93 |
Correct |
1037 ms |
28432 KB |
Output is correct |