結果

問題 No.1605 Matrix Shape
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-01-17 15:14:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,327 bytes
コンパイル時間 2,847 ms
コンパイル使用メモリ 216,892 KB
実行使用メモリ 19,664 KB
最終ジャッジ日時 2024-01-17 15:14:37
合計ジャッジ時間 7,294 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
18,172 KB
testcase_01 AC 20 ms
18,172 KB
testcase_02 AC 20 ms
18,172 KB
testcase_03 AC 20 ms
18,172 KB
testcase_04 AC 19 ms
18,172 KB
testcase_05 AC 20 ms
18,172 KB
testcase_06 AC 20 ms
18,172 KB
testcase_07 AC 20 ms
18,172 KB
testcase_08 AC 20 ms
18,172 KB
testcase_09 AC 20 ms
18,172 KB
testcase_10 AC 20 ms
18,172 KB
testcase_11 AC 20 ms
18,172 KB
testcase_12 AC 20 ms
18,172 KB
testcase_13 AC 20 ms
18,172 KB
testcase_14 AC 20 ms
18,172 KB
testcase_15 AC 21 ms
18,172 KB
testcase_16 AC 20 ms
18,172 KB
testcase_17 AC 19 ms
18,172 KB
testcase_18 AC 20 ms
18,172 KB
testcase_19 AC 88 ms
14,272 KB
testcase_20 AC 75 ms
16,960 KB
testcase_21 AC 58 ms
16,960 KB
testcase_22 AC 56 ms
14,272 KB
testcase_23 AC 75 ms
14,272 KB
testcase_24 AC 57 ms
14,272 KB
testcase_25 AC 63 ms
19,616 KB
testcase_26 AC 64 ms
19,648 KB
testcase_27 AC 53 ms
19,648 KB
testcase_28 AC 51 ms
19,632 KB
testcase_29 AC 51 ms
19,632 KB
testcase_30 AC 76 ms
16,960 KB
testcase_31 AC 59 ms
16,960 KB
testcase_32 AC 73 ms
18,320 KB
testcase_33 AC 58 ms
18,320 KB
testcase_34 AC 59 ms
19,664 KB
testcase_35 AC 58 ms
15,056 KB
testcase_36 AC 40 ms
16,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/dsu>
using namespace std;
using namespace atcoder;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

const int M = 2e5 + 1;
int main() {
    fast_io();
    int n;
    cin >> n;

    vector<int> h(n), w(n);
    vector<int> in_deg(M), out_deg(M);
    dsu uf(M);
    vector<bool> vis(M);
    for (int i = 0; i < n; i++) {
        cin >> h[i] >> w[i];
        in_deg[h[i]]++;
        out_deg[w[i]]++;
        uf.merge(h[i], w[i]);
        vis[h[i]] = vis[w[i]] = true;
    }
    int cnt = 0;
    for (int i = 0; i < M; i++) {
        cnt += !vis[i];
    }
    if (uf.groups().size() > cnt + 1) {
        cout << 0 << endl;
        return 0;
    }
    int in_odd = 0, out_odd = 0;
    for (int i = 0; i < M; i++) {
        if (in_deg[i] == out_deg[i] + 1) {
            in_odd++;
        } else if (in_deg[i] + 1 == out_deg[i]) {
            out_odd++;
        } else if (in_deg[i] != out_deg[i]) {
            cout << 0 << endl;
            return 0;
        }
    }
    if (in_odd > 1 || out_odd > 1) {
        cout << 0 << endl;
        return 0;
    }
    if (in_odd == 1 && out_odd == 1) {
        cout << 1 << endl;
        return 0;
    }
    sort(h.begin(), h.end());
    h.erase(unique(h.begin(), h.end()), h.end());
    cout << h.size() << endl;
}
0