#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
struct dsu {
vector<int> p, sze;
int comps;
dsu(int n) : p(n+1), sze(n+1), comps(n) {
for (int i = 0; i <= n; i++) {
p[i] = i, sze[i] = 1;
}
}
int get(int v) {
return v==p[v]?v:p[v]=get(p[v]);
}
int size(int v) {
return sze[get(v)];
}
bool merge(int a, int b) {
if ((a=get(a)) == (b=get(b))) return 0;
if (sze[a]<sze[b]) swap(a, b);
sze[a] += sze[b], p[b] = a;
comps--;
return 1;
}
};
void test_case() {
int n, m;
cin >> n >> m;
vector<ar<int, 3>> edges(m);
for (auto &x : edges) cin >> x[0] >> x[1] >> x[2];
vector<int> lst;
vector<int> order(m);
auto cmp = [&](int x, int y) {
return edges[x][2] != edges[y][2] ? edges[x][2] < edges[y][2] : x < y;
};
iota(all(order), 0);
sort(all(order), cmp);
vector<vector<int>> suff(m), pref(m);
{
dsu d(n);
vector<int> cur;
for (int i = 0; i < m; i++) {
auto [x, y, w] = edges[order[i]];
if (d.merge(x, y)) {
cur.emplace_back(order[i]);
}
}
suff[0] = cur;
for (int i = 1; i < m; i++) {
auto mn = min_element(all(cur), cmp);
if (*mn == order[i-1]) {
d = dsu(n);
cur.erase(mn);
for (int idx : cur) {
d.merge(edges[idx][0], edges[idx][1]);
}
for (int j = i; j < m; j++) {
int pos = order[j];
if (d.merge(edges[pos][0], edges[pos][1])) {
cur.insert(upper_bound(all(cur), pos, cmp), pos);
break;
}
}
}
suff[i] = cur;
}
}
{
dsu d(n);
vector<int> cur;
for (int i = m-1; i >= 0; i--) {
auto [x, y, w] = edges[order[i]];
if (d.merge(x, y)) {
cur.emplace_back(order[i]);
}
}
reverse(all(cur));
assert(is_sorted(all(cur), cmp));
pref[m-1] = cur;
for (int i = m-2; i >= 0; i--) {
auto mx = max_element(all(cur), cmp);
if (*mx == order[i+1]) {
d = dsu(n);
cur.erase(mx);
for (int idx : cur) {
d.merge(edges[idx][0], edges[idx][1]);
}
for (int j = i; j >= 0; j--) {
int pos = order[j];
if (d.merge(edges[pos][0], edges[pos][1])) {
cur.insert(upper_bound(all(cur), pos, cmp), pos);
break;
}
}
}
pref[i] = cur;
}
}
int q; cin >> q;
const int INF = 2e9;
while (q--) {
int x; cin >> x;
int l = 0, r = m-1, ans = -1;
while (l <= r) {
int mid = (l+r)/2;
if (x >= edges[order[mid]][2]) ans = mid, l = mid+1;
else r = mid-1;
}
int a = ans >= 0 ? sz(pref[ans]) - 1 : 0, b = 0, added = 0;
dsu d(n);
ll ans2 = 0;
while (added < n-1) {
int costA = ans >= 0 && a >= 0 ? x - edges[pref[ans][a]][2] : INF;
int costB = ans + 1 < m && b < sz(suff[ans+1]) ? edges[suff[ans+1][b]][2] - x : INF;
assert(costA >= 0);
assert(costB >= 0);
if (costA < costB) {
auto [u, v, w] = edges[pref[ans][a]];
if (d.merge(u, v)) added++, ans2 += costA;
a--;
}
else {
auto [u, v, w] = edges[suff[ans+1][b]];
if (d.merge(u, v)) added++, ans2 += costB;
b++;
}
}
cout << ans2 << '\n';
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
820 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
820 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
2608 ms |
401540 KB |
Output is correct |
21 |
Correct |
2349 ms |
389208 KB |
Output is correct |
22 |
Correct |
2508 ms |
397936 KB |
Output is correct |
23 |
Correct |
2453 ms |
400848 KB |
Output is correct |
24 |
Correct |
2482 ms |
401320 KB |
Output is correct |
25 |
Correct |
2630 ms |
401524 KB |
Output is correct |
26 |
Correct |
2603 ms |
401816 KB |
Output is correct |
27 |
Correct |
2548 ms |
401572 KB |
Output is correct |
28 |
Correct |
2469 ms |
401740 KB |
Output is correct |
29 |
Correct |
2279 ms |
401584 KB |
Output is correct |
30 |
Correct |
2614 ms |
401548 KB |
Output is correct |
31 |
Correct |
2609 ms |
401684 KB |
Output is correct |
32 |
Correct |
2575 ms |
401688 KB |
Output is correct |
33 |
Correct |
2474 ms |
401764 KB |
Output is correct |
34 |
Correct |
2239 ms |
401624 KB |
Output is correct |
35 |
Correct |
2547 ms |
401592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5062 ms |
403784 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
820 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Execution timed out |
5022 ms |
16160 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
820 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
2608 ms |
401540 KB |
Output is correct |
21 |
Correct |
2349 ms |
389208 KB |
Output is correct |
22 |
Correct |
2508 ms |
397936 KB |
Output is correct |
23 |
Correct |
2453 ms |
400848 KB |
Output is correct |
24 |
Correct |
2482 ms |
401320 KB |
Output is correct |
25 |
Correct |
2630 ms |
401524 KB |
Output is correct |
26 |
Correct |
2603 ms |
401816 KB |
Output is correct |
27 |
Correct |
2548 ms |
401572 KB |
Output is correct |
28 |
Correct |
2469 ms |
401740 KB |
Output is correct |
29 |
Correct |
2279 ms |
401584 KB |
Output is correct |
30 |
Correct |
2614 ms |
401548 KB |
Output is correct |
31 |
Correct |
2609 ms |
401684 KB |
Output is correct |
32 |
Correct |
2575 ms |
401688 KB |
Output is correct |
33 |
Correct |
2474 ms |
401764 KB |
Output is correct |
34 |
Correct |
2239 ms |
401624 KB |
Output is correct |
35 |
Correct |
2547 ms |
401592 KB |
Output is correct |
36 |
Correct |
3107 ms |
402132 KB |
Output is correct |
37 |
Correct |
2816 ms |
390140 KB |
Output is correct |
38 |
Correct |
3130 ms |
398060 KB |
Output is correct |
39 |
Correct |
3093 ms |
401288 KB |
Output is correct |
40 |
Correct |
3114 ms |
401808 KB |
Output is correct |
41 |
Correct |
3054 ms |
401972 KB |
Output is correct |
42 |
Correct |
3165 ms |
402412 KB |
Output is correct |
43 |
Correct |
3127 ms |
402052 KB |
Output is correct |
44 |
Correct |
2867 ms |
402116 KB |
Output is correct |
45 |
Correct |
2460 ms |
402188 KB |
Output is correct |
46 |
Correct |
3112 ms |
402464 KB |
Output is correct |
47 |
Correct |
3063 ms |
402072 KB |
Output is correct |
48 |
Correct |
3044 ms |
402004 KB |
Output is correct |
49 |
Correct |
3047 ms |
402120 KB |
Output is correct |
50 |
Correct |
2317 ms |
402240 KB |
Output is correct |
51 |
Correct |
2775 ms |
402080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
820 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
2608 ms |
401540 KB |
Output is correct |
21 |
Correct |
2349 ms |
389208 KB |
Output is correct |
22 |
Correct |
2508 ms |
397936 KB |
Output is correct |
23 |
Correct |
2453 ms |
400848 KB |
Output is correct |
24 |
Correct |
2482 ms |
401320 KB |
Output is correct |
25 |
Correct |
2630 ms |
401524 KB |
Output is correct |
26 |
Correct |
2603 ms |
401816 KB |
Output is correct |
27 |
Correct |
2548 ms |
401572 KB |
Output is correct |
28 |
Correct |
2469 ms |
401740 KB |
Output is correct |
29 |
Correct |
2279 ms |
401584 KB |
Output is correct |
30 |
Correct |
2614 ms |
401548 KB |
Output is correct |
31 |
Correct |
2609 ms |
401684 KB |
Output is correct |
32 |
Correct |
2575 ms |
401688 KB |
Output is correct |
33 |
Correct |
2474 ms |
401764 KB |
Output is correct |
34 |
Correct |
2239 ms |
401624 KB |
Output is correct |
35 |
Correct |
2547 ms |
401592 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5062 ms |
403784 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |