結果

問題 No.1639 最小通信路
ユーザー magstamagsta
提出日時 2021-07-09 15:47:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,924 bytes
コンパイル時間 6,890 ms
コンパイル使用メモリ 282,488 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-17 02:26:06
合計ジャッジ時間 8,660 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "testlib.h"
using namespace std;

struct UnionFind {
    vector<int> size, parents;
    UnionFind() {}
    UnionFind(int n) {
        size.resize(n, 0);
        parents.resize(n, 0);
        for (int i = 0; i < n; i++) {
            makeTree(i);
        }
    }
    void makeTree(int x) {
        parents[x] = x;
        size[x] = 1;
    }
    bool isSame(int x, int y) {
        return findRoot(x) == findRoot(y);
    }
    bool unite(int x, int y) {
        x = findRoot(x);
        y = findRoot(y);
        if (x == y) return false;
        if (size[x] > size[y]) {
            parents[y] = x;
            size[x] += size[y];
        }
        else {
            parents[x] = y;
            size[y] += size[x];
        }
        return true;
    }
    int findRoot(int x) {
        if (x != parents[x]) {
            parents[x] = findRoot(parents[x]);
        }
        return parents[x];
    }
    int treeSize(int x) {
        return size[findRoot(x)];
    }
};

int main() {
    registerValidation();
    int N = inf.readInt(2, 100000, "N");
    inf.readEoln();
    UnionFind uni(N);
    int count = 0;
    map<pair<int, int>, int> Map;
    for (int i = 0; i < N * (N - 1) / 2; i++) {
        int a = inf.readInt(1, N, "a");
        inf.readSpace();
        int b = inf.readInt(1, N, "a");
        inf.readSpace();
        string S = inf.readToken("[0-9]{1, 101}", "S");
        inf.readEoln();
        if (Map[make_pair(a, b)] == 1) cout << -1 << endl;
        Map[make_pair(a, b)]++;
        a--; b--;
        if (S.size() == 101) {
            for (int i = 2; i < 101; i++) {
                if (S[i] != '0') cout << -1 << endl;
            }
        }
        if (!uni.isSame(a, b)) {
            uni.unite(a, b);
            count++;
            if (count == N - 1) {
                cout << S << endl;
                return 0;
            }
        }
    }
    inf.readEof();
}
0