QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#375140 | #7415. Fast Spanning Tree | Xiaohuba | TL | 114ms | 53028kb | C++23 | 4.3kb | 2024-04-02 21:59:07 | 2024-04-02 21:59:09 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
// #define LOCK_GETCHAR
// #define USE_INT_128
#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
template <class T ENABLE_IF_INT>
constexpr T INF = numeric_limits<T>::max() >> 1;
#endif
#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif
#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i, j, k) for (decltype(j - k) i = (j); i <= (k); ++i) // NOLINT
#define ForDown(i, j, k) for (decltype(j - k) i = (j); i >= (k); --i) // NOLINT
#define pb push_back
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename) \
freopen(filename ".in", "r", stdin); \
freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif
// clang-format off
template<typename T> constexpr il T sq(const T & x){ return x * x; }
template<typename T> CONSTEXPR_FUNC il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> CONSTEXPR_FUNC il void cmax(T & x, const T &y){ x = max(x, y);}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y, T mod){T ans = 1; x %= mod; while (y) { if(y & 1)(ans *= x) %= mod;(x *= x) %= mod; y >>= 1;} return ans;}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y){T ans = 1; while (y) {if(y & 1) ans *= x;x *= x;y >>= 1;} return ans;}
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while(!isdigit(c)) {if (c == '-') f = -1;c = getchar();} while(isdigit(c)) {x = x * 10 + c - '0';c = getchar();} x *= f;}
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x); read(y...); }
// clang-format on
// File head end
namespace {
constexpr ll MAXN = 6e5 + 5;
int n, m, f[MAXN];
ll w[MAXN];
set<pair<ll, int>> g[MAXN];
set<pair<ll, int>>::iterator iter[MAXN];
tuple<int, int, ll> A[MAXN];
priority_queue<int, vector<int>, greater<>> pq;
int find(int x) { return f[x] < 0 ? x : f[x] = find(f[x]); }
il bool upd(int x, bool flg) {
int u = find(get<0>(A[x])), v = find(get<1>(A[x]));
if (u == v)
return 0;
ll val = get<2>(A[x]) - w[u] - w[v];
if (flg)
g[u].erase(iter[x << 1]), g[v].erase(iter[x << 1 | 1]);
if (val <= 0)
return pq.emplace(x), 1;
bool _ok;
tie(iter[x << 1], _ok) = g[u].emplace(w[u] + ((val - 1) >> 1), x << 1), assert(_ok);
tie(iter[x << 1 | 1], _ok) = g[v].emplace(w[v] + (val >> 1), x << 1 | 1),
assert(_ok);
return 0;
}
il void Main() {
read(n, m);
For(i, 1, n) read(w[i]), f[i] = -1;
For(i, 1, m) {
int x, y, z;
read(x, y, z), A[i] = {x, y, z};
upd(i, 0);
}
vector<int> Ans;
while (!pq.empty()) {
int u = pq.top();
pq.pop();
int p = find(get<0>(A[u])), q = find(get<1>(A[u]));
if (p == q)
continue;
Ans.eb(u);
if (-f[p] < -f[q])
swap(p, q);
f[p] += f[q], w[p] += w[q], f[q] = p;
for (auto it = g[q].begin(); it != g[q].end();) {
bool _ok;
tie(iter[it->se], _ok) = g[p].emplace(*it), assert(_ok);
it = g[q].erase(it);
}
for (auto it = g[p].begin(); it != g[p].end();) {
auto nxt = next(it);
if (w[p] > it->fi)
upd(it->se >> 1, 1), it = nxt;
else
break;
}
}
cout << Ans.size() << '\n';
for (int x : Ans)
cout << x << ' ';
}
} // namespace
signed main() { return Main(), 0; }
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 38084kb
input:
5 5 1 4 3 4 0 4 5 5 3 1 1 2 5 2 4 3 1 4 1 4
output:
4 2 3 1 4
result:
ok 5 number(s): "4 2 3 1 4"
Test #2:
score: 0
Accepted
time: 10ms
memory: 37984kb
input:
3 5 3 2 2 1 2 6 1 2 6 1 2 3 1 2 6 2 3 6
output:
2 3 5
result:
ok 3 number(s): "2 3 5"
Test #3:
score: 0
Accepted
time: 7ms
memory: 36580kb
input:
10 20 4 6 6 1 7 9 1 8 7 9 5 3 2 1 10 10 5 6 7 9 6 9 3 1 1 6 8 1 5 7 0 9 5 4 10 3 4 8 6 8 3 10 6 5 3 8 3 7 9 1 9 10 10 1 0 5 7 6 6 10 1 6 5 9 5 8 2 9 2 4
output:
8 1 2 3 4 5 6 7 20
result:
ok 9 numbers
Test #4:
score: 0
Accepted
time: 3ms
memory: 36404kb
input:
10 20 0 10 4 6 2 0 2 0 2 8 5 10 8 7 1 6 10 5 0 9 8 0 5 8 4 5 1 8 10 3 6 5 4 3 9 2 6 10 3 6 4 3 1 3 10 3 6 1 3 3 2 5 6 9 2 4 2 5 10 6 5 8 6 3 2 1 0 2 3 6
output:
9 1 4 5 6 2 7 8 9 13
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 5ms
memory: 36640kb
input:
100 200 49 21 70 93 66 51 36 59 56 62 10 4 46 73 22 48 89 17 72 60 29 64 19 56 32 54 55 0 77 86 34 35 56 17 55 2 98 80 73 71 64 37 61 22 89 96 99 28 0 35 56 45 74 81 30 3 66 96 28 16 29 43 60 61 60 95 83 5 73 1 28 47 73 44 8 4 91 34 100 23 4 93 44 87 72 5 13 88 100 52 56 61 100 80 14 30 59 97 51 43 ...
output:
96 1 2 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 9 20 21 22 23 25 26 3 27 28 29 30 31 32 33 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 52 53 34 54 55 56 58 59 60 61 62 63 64 66 67 68 69 70 71 72 74 76 78 80 81 82 87 89 91 94 95 96 100 43 101 24 84 107 109 110 117 119 129 142 144 169 170 195
result:
ok 97 numbers
Test #6:
score: 0
Accepted
time: 10ms
memory: 36412kb
input:
100 200 24 84 94 70 81 51 70 9 39 61 34 89 37 23 21 52 19 65 37 53 85 70 27 74 46 45 49 76 36 51 38 8 91 84 30 21 82 93 54 4 65 44 66 78 42 42 74 11 12 66 24 88 65 75 78 55 61 45 21 83 29 24 85 37 73 25 25 40 96 100 26 28 18 30 56 41 15 11 88 61 42 74 8 97 14 34 5 39 24 94 84 8 3 54 75 62 73 36 58 4...
output:
98 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 11 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 80 81 82 84 85 87 88 89 90 91 92 94 96 98 99 103 104 129 156 163 164 167 185 199
result:
ok 99 numbers
Test #7:
score: 0
Accepted
time: 3ms
memory: 38504kb
input:
1000 2000 919 776 189 249 535 136 885 695 607 561 235 179 233 501 828 472 532 337 721 344 71 885 630 612 867 132 392 624 477 527 529 17 960 726 235 661 407 923 214 882 554 194 68 561 993 393 548 92 269 41 754 647 1 924 510 573 464 911 845 303 624 583 255 861 668 998 648 901 251 766 349 248 504 117 6...
output:
979 2 3 4 5 6 7 8 10 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 42 43 44 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 71 72 73 74 75 76 77 78 79 80 81 82 45 83 84 85 86 87 88 89 90 91 92 93 94 95 97 98 99 100 101 102 103 105 41 106 107 108 1...
result:
ok 980 numbers
Test #8:
score: 0
Accepted
time: 0ms
memory: 36472kb
input:
1000 2000 838 14 274 368 930 991 554 645 860 709 557 447 647 723 384 339 801 423 843 957 649 71 997 106 521 713 275 876 396 541 377 586 934 998 330 221 529 635 515 216 643 742 507 270 902 288 440 622 950 258 295 505 358 540 452 414 888 601 815 386 759 689 742 840 994 786 974 648 379 102 450 754 624 ...
output:
970 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 23 24 25 26 28 29 30 31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 53 27 54 56 52 57 58 59 60 61 62 63 64 65 67 68 69 70 71 72 73 76 77 78 79 80 81 82 83 84 85 86 87 89 90 91 93 95 97 98 99 100 101 102 104 105 106 108 75 109 ...
result:
ok 971 numbers
Test #9:
score: 0
Accepted
time: 6ms
memory: 37412kb
input:
10000 20000 2770 4102 7211 7463 5376 956 7654 6142 7795 4467 5954 3814 401 4217 3088 9 3334 5642 8164 4848 1690 4609 9606 1472 2055 3238 4429 1866 2551 1891 3108 231 8719 5315 3920 9718 2547 9932 2680 15 7872 7302 4641 1096 7732 5134 1348 3489 4436 7903 168 9090 2765 2650 8730 7433 6196 9299 8451 43...
output:
9826 1 3 4 5 6 7 9 10 11 12 15 16 20 21 22 23 24 25 26 29 30 32 33 34 35 36 39 40 41 42 43 45 46 47 48 49 50 51 52 53 55 57 58 59 60 61 62 63 65 66 67 68 69 71 72 73 74 75 76 27 77 79 81 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 106 107 108 109 110 111 113 114 116 117 11...
result:
ok 9827 numbers
Test #10:
score: 0
Accepted
time: 10ms
memory: 37396kb
input:
10000 20000 6023 137 374 4404 7135 4862 3664 1753 2615 5821 3591 1821 1772 8756 6173 3166 8856 8030 994 8260 4267 4477 691 8970 5445 9593 452 7959 3303 6588 3700 4861 1268 9344 2475 2435 7968 1011 9437 5883 4815 2770 9056 4423 2924 1525 4211 4655 9676 8745 429 9269 8489 4940 6051 8532 6732 6831 7866...
output:
9797 1 2 5 6 8 10 11 12 13 14 15 16 17 19 20 21 22 23 24 26 27 29 30 31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 59 60 61 62 63 64 66 67 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 93 94 96 97 99 100 101 103 104 105 106 107 108 109 110 111 112 113...
result:
ok 9798 numbers
Test #11:
score: 0
Accepted
time: 91ms
memory: 53028kb
input:
300000 300000 79589 56209 39109 45927 40583 85355 37235 93800 80252 94753 77793 8508 27123 53965 42678 78268 75590 62814 33653 46238 58818 17465 5587 99163 91597 76787 82053 11436 79434 6262 59411 20917 1182 10838 63928 74239 21886 15024 29599 86110 97887 2230 71925 42987 37897 58688 65784 50117 486...
output:
250270 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 20 21 22 23 25 26 27 28 30 31 32 33 35 36 37 39 40 41 42 44 45 46 47 48 49 51 52 53 54 56 57 58 59 60 61 62 63 67 68 69 70 71 72 74 75 76 78 79 82 83 84 85 86 88 89 90 91 92 93 94 95 96 97 99 100 101 102 103 104 110 111 112 113 115 116 117 118 120 ...
result:
ok 250271 numbers
Test #12:
score: 0
Accepted
time: 114ms
memory: 52972kb
input:
300000 300000 51045 94574 62648 61791 13468 47104 18922 75351 33470 57260 52525 92002 42350 1518 49576 49520 23938 83685 98745 22853 85627 93225 96449 76858 84297 90634 18353 5126 90756 95406 41289 30050 58903 47603 51054 61224 85597 48982 73701 9944 714 60626 62593 56124 53915 69599 35071 442 67000...
output:
250310 1 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 36 37 38 39 40 41 42 43 45 46 47 48 49 51 52 53 54 55 56 57 58 60 61 62 63 64 65 66 67 69 71 72 73 74 75 77 78 79 81 82 83 84 85 86 87 88 89 90 91 92 93 95 97 98 99 100 101 102 103 104 107 108 109 110 111 ...
result:
ok 250311 numbers
Test #13:
score: 0
Accepted
time: 89ms
memory: 52956kb
input:
300000 300000 72519 10569 85101 89272 66593 57128 67893 64457 283 83904 65334 95597 56714 33689 56790 51503 55537 5913 42695 73175 42976 97554 67173 82311 58467 53879 67191 44933 99208 54410 17322 32869 83909 72219 99697 45792 99743 41855 26611 7202 50097 60431 82821 89967 83344 66226 72347 2538 142...
output:
250229 3 4 5 6 7 9 10 11 12 13 15 17 18 19 20 21 22 23 24 25 26 27 29 30 31 32 33 35 36 38 39 40 41 43 44 45 46 47 48 49 51 52 53 54 55 56 57 59 61 62 63 64 65 66 67 69 70 71 72 73 74 75 77 79 80 81 82 83 85 87 88 90 92 93 94 95 96 97 98 102 104 105 106 107 108 109 110 111 113 114 115 116 117 119 12...
result:
ok 250230 numbers
Test #14:
score: 0
Accepted
time: 94ms
memory: 52840kb
input:
300000 300000 53581 86152 7554 68058 60129 2393 16863 88805 7509 10547 18554 47885 60184 65861 80069 88726 27548 3795 86646 47844 11219 85818 2657 47351 16572 17125 80787 36045 42899 48655 33768 35689 24979 32075 13098 19466 13887 99487 28215 4461 12932 35889 27395 23809 77531 22439 33969 15528 2617...
output:
250219 1 3 5 6 7 8 9 10 11 12 13 14 15 16 19 20 21 22 23 24 28 29 30 31 32 33 35 38 39 40 41 42 43 44 47 48 49 50 51 52 53 54 55 56 57 58 59 61 62 63 64 66 67 68 69 70 71 72 73 74 76 77 79 80 81 82 83 84 85 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 112 113 11...
result:
ok 250220 numbers
Test #15:
score: 0
Accepted
time: 100ms
memory: 52932kb
input:
300000 300000 10295 2146 81315 95539 13253 12417 65835 77911 25629 37191 31362 64933 50200 98033 62935 90709 34800 77331 71008 33406 3809 38841 73382 63698 39436 45130 53971 51504 62245 56352 9801 62856 14743 56690 61740 68794 87621 68012 81126 26066 51422 11347 23276 81999 6959 19066 71245 93279 13...
output:
250424 1 2 5 7 10 11 12 13 15 18 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 37 38 40 41 42 43 44 46 47 49 50 51 52 53 54 55 57 58 59 60 61 62 63 64 65 66 67 68 69 70 73 74 75 76 77 78 79 80 81 82 83 84 86 87 88 89 90 91 92 93 94 95 97 98 99 100 101 103 104 106 107 108 109 110 112 113 115 116 117 1...
result:
ok 250425 numbers
Test #16:
score: 0
Accepted
time: 95ms
memory: 52928kb
input:
300000 300000 56116 29036 3768 98673 42031 82029 90459 67017 32855 28595 44170 68528 53670 30203 21455 52278 66400 99560 90612 83728 72053 2758 19760 4391 13607 8376 67567 66964 5936 15356 61488 41329 55815 81306 75142 77710 1765 49991 69277 47672 49498 35499 43504 91495 76801 15693 32867 30615 6100...
output:
250121 1 2 3 4 5 6 7 8 10 11 12 13 16 17 18 20 21 22 23 26 27 29 30 31 32 33 36 37 38 40 41 42 43 44 45 47 49 51 52 53 54 58 59 60 61 62 64 65 66 67 69 70 71 72 73 74 75 76 77 78 79 80 82 84 86 87 88 89 90 91 92 93 95 96 97 98 99 100 101 102 103 104 105 106 107 109 110 111 112 113 114 115 116 117 11...
result:
ok 250122 numbers
Test #17:
score: -100
Time Limit Exceeded
input:
150000 300000 10 6 5 3 9 6 4 10 4 2 6 8 3 2 4 4 9 9 7 7 5 5 4 10 4 2 4 5 3 8 4 10 5 10 1 0 5 10 4 6 0 7 6 4 3 8 0 0 8 0 8 5 7 5 6 7 6 6 0 2 2 7 10 1 0 4 5 8 1 0 3 3 8 0 8 2 2 6 10 10 10 7 10 2 6 4 2 4 7 6 1 8 10 8 0 5 0 1 3 3 10 9 5 6 5 8 7 9 3 2 4 8 1 2 0 8 1 10 8 3 9 0 2 9 8 1 9 0 7 2 10 2 10 1 2 ...