提出 #62535460


ソースコード 拡げる

#include <bits/stdc++.h>
#define ll long long
#define endl '\n'

using namespace std;

struct dsu {
    vector<int> parent;
    int comps = 0;

    dsu(int n) {
        parent.resize(n, -1);
        comps = n;
    }

    int find_set(int a) {
        if (parent[a] < 0) return a;
        return parent[a] = find_set(parent[a]);
    }

    int merge(int a, int b) {
        int x = find_set(a), y = find_set(b);
        if (x == y) return x;
        if (-parent[x] < -parent[y]) swap(x, y);
        parent[x] += parent[y];
        parent[y] = x;
        comps--;
        return x;
    }

    bool are_same(int a, int b) {
        return find_set(a) == find_set(b);
    }

    int size(int a) {
        return -parent[find_set(a)];
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int N, M;
    cin >> N >> M;
    vector<pair<int, int>> edges(M);
    for (int i = 0; i < M; i++) {
        cin >> edges[i].first >> edges[i].second;
        edges[i].first--; edges[i].second--;
    }
    dsu DSU(N);
    vector<array<int, 3>> possible_reconnects;
    for (int i = 0; i < M; i++) {
        auto [u, v] = edges[i];
        if (!DSU.are_same(u, v)) {
            DSU.merge(u, v);
        } else {
            possible_reconnects.push_back({
                i, u, v
            });
        }
    }
    int K = DSU.comps - 1;
    cout << K << endl;
    set<int> roots;
    for (int i = 0; i < N; i++) {
        if (DSU.parent[i] < 0) {
            roots.insert(i);
        }
    }
    int count = 0;
    for (int i = 0; i < possible_reconnects.size(); i++) {
        if (count == K) break;
        auto [idx, u, v] = possible_reconnects[i];
        for (auto r : roots) {
            if (!DSU.are_same(v, r)) {
                cout << idx + 1 << " " << v + 1 << " " << r + 1 << endl;
                roots.erase(r);
                DSU.merge(v, r);
                count++;
                break;
            }
        }
    }
    assert(count == K);
    return 0;
}

提出情報

提出日時
問題 E - Cables and Servers
ユーザ Mitscho
言語 C++ 17 (gcc 12.2)
得点 450
コード長 2055 Byte
結果 AC
実行時間 95 ms
メモリ 18520 KiB

コンパイルエラー

Main.cpp: In function ‘int main()’:
Main.cpp:70:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::array<int, 3> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   70 |     for (int i = 0; i < possible_reconnects.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 3
AC × 22
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
random_01.txt AC 14 ms 5232 KiB
random_02.txt AC 14 ms 7124 KiB
random_03.txt AC 3 ms 3656 KiB
random_04.txt AC 1 ms 3512 KiB
random_05.txt AC 20 ms 7756 KiB
random_06.txt AC 15 ms 7716 KiB
random_07.txt AC 4 ms 3768 KiB
random_08.txt AC 1 ms 3488 KiB
random_09.txt AC 17 ms 5400 KiB
random_10.txt AC 9 ms 5508 KiB
random_11.txt AC 30 ms 6464 KiB
random_12.txt AC 1 ms 3556 KiB
random_13.txt AC 25 ms 5384 KiB
random_14.txt AC 24 ms 5392 KiB
random_15.txt AC 25 ms 5364 KiB
random_16.txt AC 24 ms 5424 KiB
random_17.txt AC 65 ms 11284 KiB
random_18.txt AC 95 ms 18520 KiB
random_19.txt AC 2 ms 3504 KiB
sample_01.txt AC 1 ms 3464 KiB
sample_02.txt AC 1 ms 3536 KiB
sample_03.txt AC 1 ms 3444 KiB