結果

問題 No.1605 Matrix Shape
ユーザー ktr216ktr216
提出日時 2021-07-16 22:20:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 169,764 KB
実行使用メモリ 8,708 KB
最終ジャッジ日時 2023-09-20 14:32:20
合計ジャッジ時間 5,308 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,852 KB
testcase_01 AC 5 ms
6,932 KB
testcase_02 AC 4 ms
7,128 KB
testcase_03 AC 5 ms
6,960 KB
testcase_04 AC 4 ms
6,988 KB
testcase_05 AC 5 ms
6,884 KB
testcase_06 AC 5 ms
6,852 KB
testcase_07 AC 4 ms
7,068 KB
testcase_08 AC 5 ms
6,872 KB
testcase_09 AC 5 ms
6,884 KB
testcase_10 AC 5 ms
6,884 KB
testcase_11 AC 5 ms
6,984 KB
testcase_12 AC 5 ms
6,984 KB
testcase_13 AC 5 ms
6,936 KB
testcase_14 AC 5 ms
6,824 KB
testcase_15 AC 5 ms
6,996 KB
testcase_16 AC 5 ms
6,992 KB
testcase_17 AC 5 ms
6,884 KB
testcase_18 AC 5 ms
6,888 KB
testcase_19 AC 141 ms
8,616 KB
testcase_20 AC 122 ms
8,632 KB
testcase_21 AC 119 ms
8,568 KB
testcase_22 AC 134 ms
8,656 KB
testcase_23 AC 136 ms
8,616 KB
testcase_24 AC 133 ms
8,456 KB
testcase_25 AC 79 ms
8,412 KB
testcase_26 AC 79 ms
8,708 KB
testcase_27 AC 79 ms
8,456 KB
testcase_28 AC 79 ms
8,412 KB
testcase_29 AC 79 ms
8,504 KB
testcase_30 AC 119 ms
8,460 KB
testcase_31 AC 119 ms
8,652 KB
testcase_32 AC 112 ms
8,412 KB
testcase_33 AC 108 ms
8,512 KB
testcase_34 AC 105 ms
8,568 KB
testcase_35 AC 108 ms
7,936 KB
testcase_36 AC 63 ms
7,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i < (r); i++)
using namespace std;

typedef long long ll;

const int MAX_N = 200010;
int par[MAX_N];
int rnk[MAX_N];
int siz[MAX_N];

void init(int n) {
    rep(i,0,n) {
        par[i] = i;
        rnk[i] = 0;
        siz[i] = 1;
    }
}

int find(int x) {
    if (par[x] == x) {
        return x;
    } else {
        return par[x] = find(par[x]);
    }
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    int s = siz[x] + siz[y];
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
    siz[find(x)] = s;
}

bool same(int x, int y) {
    return find(x) == find(y);
}

int size(int x) {
    return siz[find(x)];
}

int main() {
    int N, D = 0;
    cin >> N;
    init(200000);
    vector<int> A(200000, 0), B(200000, 0), H(N), W(N);
    rep(i, 0, N) {
        cin >> H[i] >> W[i];
        A[H[i] - 1]++;
        B[W[i] - 1]++;
        unite(H[i] - 1, W[i] - 1);
    }
    rep(i, 0, 200000) {
        if ((A[i] > 0 || B[i] > 0) && !same(i, H[0] - 1)) {
            cout << 0 << endl;
            return 0;
        }
        D += abs(A[i] - B[i]);
    }
    if (D > 2) {
        cout << 0 << endl;
    } else if (D == 2) {
        cout << 1 << endl;
    } else {
        int ans = 0;
        rep(i, 0, 200000) {
            ans += (A[i] > 0);
        }
        cout << ans << endl;
    }
}
0