結果

問題 No.1605 Matrix Shape
ユーザー mamimume____momamimume____mo
提出日時 2021-07-17 00:19:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 493 ms / 2,000 ms
コード長 2,210 bytes
コンパイル時間 1,710 ms
コンパイル使用メモリ 176,232 KB
実行使用メモリ 17,040 KB
最終ジャッジ日時 2023-09-20 16:49:06
合計ジャッジ時間 9,712 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 477 ms
17,040 KB
testcase_20 AC 344 ms
11,028 KB
testcase_21 AC 342 ms
11,020 KB
testcase_22 AC 454 ms
17,036 KB
testcase_23 AC 493 ms
16,984 KB
testcase_24 AC 449 ms
16,984 KB
testcase_25 AC 128 ms
4,576 KB
testcase_26 AC 129 ms
4,576 KB
testcase_27 AC 130 ms
4,572 KB
testcase_28 AC 128 ms
4,684 KB
testcase_29 AC 127 ms
4,644 KB
testcase_30 AC 335 ms
11,028 KB
testcase_31 AC 374 ms
11,020 KB
testcase_32 AC 281 ms
7,928 KB
testcase_33 AC 264 ms
7,740 KB
testcase_34 AC 103 ms
4,588 KB
testcase_35 AC 272 ms
14,344 KB
testcase_36 AC 152 ms
10,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1000000000;
const ll INFL = 2e18;
template <class T>bool chmin(T &a, T b){ if (a > b) { a = b; return true;} else return false;}
template <class T>bool chmax(T &a, T b){ if (a < b) { a = b; return true;} else return false;}

class UnionFind {
public:
    vector<int> par;
    vector<int> siz;

    UnionFind() {
    }

    UnionFind(int sz_): par(sz_), siz(sz_, 1) {
        for(int i = 0;i < sz_;i++)par[i] = i;
    }

    void init(int sz_) {
        par.resize(sz_);
        siz.resize(sz_,1);
        for(int i = 0;i < sz_;i++)par[i] = i;
    }

    int root(int x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }

    bool merge(int x,int y) {
        x = root(x);
        y = root(y);
        if(x == y)return false;
        if(siz[x] < siz[y])swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return siz[root(x)];
    }
};
int main() {
    int N;
    cin >> N;

    vector<int> H(N);
    vector<int> W(N);
    map<int,int> mp;
    for (int i = 0;i < N;i++){
        cin >> H[i] >> W[i];
        mp[H[i]] = mp[W[i]] = 1;
    }

    int idx = 0;
    for (auto& p:mp) {
        p.second = idx;
        idx++;
    }

    UnionFind uf(mp.size());
    vector<int> in_deg(mp.size(), 0);
    vector<int> out_deg(mp.size(), 0);
    for (int i = 0;i < N;i++) {
        H[i] = mp[H[i]], W[i] = mp[W[i]];
        uf.merge(H[i], W[i]);
        in_deg[W[i]]++;
        out_deg[H[i]]++;
    }

    if (uf.size(0) != mp.size()) {
        cout << 0 << endl;
        return 0;
    }

    int cnt_0 = 0;
    int cnt_p1 = 0;
    int cnt_m1 = 0;
    for (int i = 0;i < mp.size();i++) {
        int deg = in_deg[i] - out_deg[i];
        if (deg == 0)cnt_0++;
        if (deg == -1)cnt_m1++;
        if (deg == 1)cnt_p1++;
    }

    if (cnt_0 == mp.size()) {
        cout << mp.size() << endl;
    }
    else if(cnt_m1 == 1 && cnt_p1 == 1 && cnt_0 + cnt_m1 + cnt_p1 == mp.size()) {
        cout << 1 << endl;
    }
    else cout << 0 << endl;
}
0