結果

問題 No.1605 Matrix Shape
ユーザー merom686merom686
提出日時 2021-07-17 00:26:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 1,152 bytes
コンパイル時間 2,805 ms
コンパイル使用メモリ 206,568 KB
実行使用メモリ 13,408 KB
最終ジャッジ日時 2023-09-20 16:55:54
合計ジャッジ時間 6,772 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 181 ms
13,176 KB
testcase_20 AC 142 ms
8,448 KB
testcase_21 AC 144 ms
8,496 KB
testcase_22 AC 176 ms
13,196 KB
testcase_23 AC 187 ms
13,172 KB
testcase_24 AC 177 ms
13,408 KB
testcase_25 AC 79 ms
4,380 KB
testcase_26 AC 80 ms
4,376 KB
testcase_27 AC 80 ms
4,376 KB
testcase_28 AC 79 ms
4,380 KB
testcase_29 AC 79 ms
4,376 KB
testcase_30 AC 145 ms
8,520 KB
testcase_31 AC 143 ms
8,600 KB
testcase_32 AC 121 ms
6,328 KB
testcase_33 AC 121 ms
6,388 KB
testcase_34 AC 104 ms
4,376 KB
testcase_35 AC 130 ms
10,084 KB
testcase_36 AC 75 ms
8,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct UnionFind {
    UnionFind(int n) : p(n, -1) {}
    int root(int u) {
        return p[u] < 0 ? u : p[u] = root(p[u]);
    }
    int size(int u) {
        return -p[root(u)];
    }
    bool same(int u, int v) {
        return root(u) == root(v);
    }
    bool unite(int u, int v) {
        u = root(u);
        v = root(v);
        if (u == v) return false;
        if (p[u] > p[v]) swap(u, v);
        p[u] += p[v];
        p[v] = u;
        return true;
    }
    vector<int> p;
};

int main() {
    int n;
    cin >> n;

    UnionFind uf((int)2e5 + 1);
    unordered_map<int, int> mp;
    for (int i = 0; i < n; i++) {
        int h, w;
        cin >> h >> w;

        uf.unite(h, w);
        mp[h]--;
        mp[w]++;
    }

    if (uf.size(mp.begin()->first) != mp.size()) {
        cout << 0 << endl;
        exit(0);
    }

    int x = 0;
    for (const auto &[i, d] : mp) {
        x += abs(d);
    }
    if (x == 0) {
        cout << mp.size() << endl;
    } else if (x == 2) {
        cout << 1 << endl;
    } else {
        cout << 0 << endl;
    }

    return 0;
}
0