QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#646128#6402. MEXimum Spanning Treecrimsonsunset#RE 1039ms3972kbC++205.0kb2024-10-16 21:23:082024-10-16 21:23:08

Judging History

你现在查看的是最新测评结果

  • [2024-10-16 21:23:08]
  • 评测
  • 测评结果:RE
  • 用时:1039ms
  • 内存:3972kb
  • [2024-10-16 21:23:08]
  • 提交

answer

#pragma GCC optimize("O3,unroll-loops,inline")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
//#define int ll
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

constexpr int INF = 1e9;

struct edge {
    int u, v, c;
};

int idi_naxui(vector<edge> &e, int n) {
    int m = e.size();
    vector<int> j;
    while (true) {
        vector<int> x1, x2, color(m, 0);
        for (int i : j)
            ++color[e[i].c];
        vector<bool> used(m, false);
        for (int i : j)
            used[i] = true;
        // vector<vector<int>> gr(m);
        for (int k = 0; k < m; ++k)
            if (!used[k])
                if (color[e[k].c] == 0)
                    x2.push_back(k);
        vector<int> comp(n, -1);
        int cnt = 0;
        vector<vector<int>> trees(n);
        auto dfs = [&](auto dfs, int v, int p, vector<int>& c) -> void {
            c[v] = cnt;
            for (int u : trees[v])
                if (u != p)
                    dfs(dfs, u, v, c);
        };
        for (int q = 0; q < j.size(); ++q) {
            trees[e[j[q]].u].push_back(e[j[q]].v);
            trees[e[j[q]].v].push_back(e[j[q]].u);
        }
        for (int i = 0; i < n; ++i)
            if (comp[i] == -1) {
                dfs(dfs, i, -1, comp);
                ++cnt;
            }
        for (int k = 0; k < m; ++k)
            if (!used[k])
                if (comp[e[k].u] != comp[e[k].v])
                    x1.push_back(k);
        auto calc = [&](int i) -> vector<int> {
            vector<int> res;
            if (used[i]) {
                for (int q = 0; q < n; ++q)
                    trees[q].clear();
                for (int q = 0; q < j.size(); ++q)
                    if (j[q] != i) {
                        trees[e[j[q]].u].push_back(e[j[q]].v);
                        trees[e[j[q]].v].push_back(e[j[q]].u);
                    }
                cnt = 0;
                vector<int> cd(n, -1);
                for (int k = 0; k < n; ++k)
                    if (cd[k] == -1) {
                        dfs(dfs, k, -1, cd);
                        ++cnt;
                    }
                for (int k = 0; k < m; ++k)
                    if (!used[k]) {
                        if (comp[e[k].u] != comp[e[k].v])
                            res.push_back(k);
                        else if (cd[e[k].u] != cd[e[k].v])
                            res.push_back(k);
                    }
            } else {
                for (int k = 0; k < j.size(); ++k)
                    if (color[e[i].c] == 0)
                        res.push_back(j[k]);
                    else if (e[i].c == e[j[k]].c)
                        res.push_back(j[k]);
            }
            return res;
        };
        if (x1.empty() || x2.empty()) {
            break;
        }
        vector<bool> inx1(m, false), inx2(m, false);
        for (int i = 0; i < x1.size(); ++i)
            inx1[x1[i]] = true;
        for (int i = 0; i < x2.size(); ++i)
            inx2[x2[i]] = true;
        bool fl = false;
        for (int i = 0; i < m; ++i)
            if (inx1[i] && inx2[i]) {
                j.push_back(i);
                fl = true;
                break;
            }
        if (fl)
            continue;
        // vector<bool> cgr(m, false);
        vector<int> dist(m, INF), prev(m, -1);
        deque<int> bfs;
        for (int i = 0; i < x1.size(); ++i)
            bfs.push_back(x1[i]), dist[x1[i]] = 0;
        while (!bfs.empty()) {
            int v = bfs.front();
            bfs.pop_front();
            for (int u : calc(v))
                if (dist[v] + 1 < dist[u]) {
                    dist[u] = dist[v] + 1;
                    bfs.push_back(u);
                    prev[u] = v;
                }
        }
        int ind = x2[0];
        for (int i = 1; i < x2.size(); ++i)
            if (dist[x2[i]] < dist[ind])
                ind = x2[i];
        if (dist[ind] == INF)
            break;
        vector<int> rebuild(m, 0);
        for (int i : j)
            rebuild[i] ^= 1;
        int last = ind;
        while (last != -1) {
            rebuild[last] ^= 1;
            last = prev[last];
        }
        vector<int> new_j;
        for (int i = 0; i < m; ++i)
            if (rebuild[i])
                new_j.push_back(i);
        j = new_j;
    }
    return j.size();
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<edge> e(m);
    for (auto &i: e)
        cin >> i.u >> i.v >> i.c, --i.u, --i.v;
    int l = 0, r = m + 1;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        vector<edge> ee;
        for (auto i : e)
            if (i.c < mid)
                ee.push_back(i);
        if (idi_naxui(ee, n) == mid)
            l = mid;
        else
            r = mid;
    }
    cout << l;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t = 1;
//    cin >> t;
    while (t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3788kb

input:

4 4
1 2 0
2 3 1
1 3 1
3 4 2

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 137ms
memory: 3768kb

input:

1000 1000
647 790 6
91 461 435
90 72 74
403 81 240
893 925 395
817 345 136
88 71 821
831 962 53
164 270 298
14 550 317
99 580 81
26 477 488
977 474 861
413 483 167
872 675 17
819 327 449
594 242 68
381 983 319
867 582 358
869 225 669
274 352 392
40 388 998
246 477 44
508 979 286
483 776 71
580 438 6...

output:

502

result:

ok 1 number(s): "502"

Test #3:

score: 0
Accepted
time: 1039ms
memory: 3916kb

input:

900 1000
232 890 107
425 399 19
5 74 753
105 333 163
779 42 582
359 647 524
767 409 48
239 780 443
484 489 546
97 634 562
627 866 714
500 357 590
60 728 591
407 686 210
547 32 370
76 772 500
407 584 772
73 699 69
332 847 516
829 754 727
562 756 678
819 303 128
781 667 263
535 672 767
89 762 216
878 ...

output:

801

result:

ok 1 number(s): "801"

Test #4:

score: 0
Accepted
time: 554ms
memory: 3944kb

input:

500 1000
381 118 230
258 331 21
403 71 207
170 2 125
467 99 6
369 100 492
70 187 352
99 163 123
135 51 352
461 175 486
275 194 236
299 14 19
16 1 68
7 229 316
235 433 320
411 179 463
112 329 326
464 169 52
377 93 51
84 336 335
42 240 379
182 496 344
377 481 195
88 286 491
199 425 165
37 292 44
197 2...

output:

403

result:

ok 1 number(s): "403"

Test #5:

score: 0
Accepted
time: 955ms
memory: 3972kb

input:

900 1000
698 454 775
6 762 755
585 346 86
220 245 253
54 634 184
634 249 234
454 363 546
520 799 501
103 346 134
381 346 792
835 782 614
359 220 485
634 68 54
411 220 439
701 364 791
220 876 15
70 346 317
220 461 769
577 431 117
488 107 706
160 39 864
220 172 721
431 400 556
801 364 716
37 845 83
99...

output:

801

result:

ok 1 number(s): "801"

Test #6:

score: 0
Accepted
time: 479ms
memory: 3648kb

input:

500 1000
7 326 457
269 363 262
414 164 422
416 321 391
37 180 315
304 212 80
483 439 185
221 55 236
456 428 164
401 142 133
42 197 327
366 389 181
125 492 347
220 277 308
316 426 192
29 95 268
50 87 366
62 394 136
480 255 287
363 39 366
489 103 302
477 370 418
16 82 259
374 398 298
77 224 64
402 474...

output:

404

result:

ok 1 number(s): "404"

Test #7:

score: -100
Runtime Error

input:

1000 1000
404 572 946
963 590 625
691 563 394
297 369 699
379 667 345
285 219 775
860 851 693
393 962 118
78 19 178
986 466 284
14 709 603
897 698 491
432 875 675
268 886 372
599 1000 151
938 435 978
554 22 577
53 804 686
932 195 192
974 100 267
195 219 790
545 242 669
720 623 317
181 514 48
75 373 ...

output:


result: