結果

問題 No.1605 Matrix Shape
ユーザー snow39snow39
提出日時 2021-07-16 21:55:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 2,690 bytes
コンパイル時間 1,135 ms
コンパイル使用メモリ 112,240 KB
実行使用メモリ 7,140 KB
最終ジャッジ日時 2023-09-20 13:46:47
合計ジャッジ時間 3,672 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,384 KB
testcase_01 AC 5 ms
5,376 KB
testcase_02 AC 4 ms
5,388 KB
testcase_03 AC 5 ms
5,476 KB
testcase_04 AC 4 ms
5,372 KB
testcase_05 AC 5 ms
5,388 KB
testcase_06 AC 5 ms
5,304 KB
testcase_07 AC 4 ms
5,388 KB
testcase_08 AC 5 ms
5,384 KB
testcase_09 AC 5 ms
5,380 KB
testcase_10 AC 5 ms
5,384 KB
testcase_11 AC 5 ms
5,368 KB
testcase_12 AC 5 ms
5,484 KB
testcase_13 AC 5 ms
5,356 KB
testcase_14 AC 5 ms
5,280 KB
testcase_15 AC 5 ms
5,284 KB
testcase_16 AC 5 ms
5,384 KB
testcase_17 AC 5 ms
5,368 KB
testcase_18 AC 4 ms
5,288 KB
testcase_19 AC 52 ms
6,948 KB
testcase_20 AC 47 ms
6,868 KB
testcase_21 AC 46 ms
6,900 KB
testcase_22 AC 46 ms
6,972 KB
testcase_23 AC 52 ms
7,140 KB
testcase_24 AC 50 ms
6,968 KB
testcase_25 AC 35 ms
6,968 KB
testcase_26 AC 35 ms
6,904 KB
testcase_27 AC 35 ms
7,064 KB
testcase_28 AC 33 ms
6,936 KB
testcase_29 AC 34 ms
6,972 KB
testcase_30 AC 46 ms
6,940 KB
testcase_31 AC 46 ms
6,964 KB
testcase_32 AC 42 ms
6,904 KB
testcase_33 AC 41 ms
6,864 KB
testcase_34 AC 38 ms
7,008 KB
testcase_35 AC 41 ms
6,708 KB
testcase_36 AC 25 ms
6,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T>
void chmin(T &a, const T &b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
    if (a < b) a = b;
}

class DisjointSet {
public:
    int N;
    vector<int> rank, p;
    vector<int> sz;

    DisjointSet() {}
    DisjointSet(int n) : N(n), rank(n), p(n), sz(n) {
        for (int i = 0; i < N; i++) makeSet(i);
    }

    void makeSet(int x) {
        p[x] = x;
        rank[x] = 0;
        sz[x] = 1;
    }

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

    void unite(int x, int y) {
        if (same(x, y)) return;
        link(leader(x), leader(y));
    }

    void link(int x, int y) {
        if (rank[x] > rank[y]) swap(x, y);

        p[x] = y;
        sz[y] += sz[x];
        if (rank[x] == rank[y]) {
            ++rank[y];
        }
    }

    int leader(int x) {
        if (x != p[x]) p[x] = leader(p[x]);
        return p[x];
    }

    int size(int x) {
        return sz[leader(x)];
    }
};

const int MAX = 2e5 + 10;

void solve() {
    int N;
    cin >> N;
    vector<int> H(N), W(N);
    rep(i, N) cin >> H[i] >> W[i];

    {
        DisjointSet us(MAX);
        rep(i, N) us.unite(H[i], W[i]);
        int leader = us.leader(H[0]);
        rep(i, N) if (leader != us.leader(H[i])) {
            cout << 0 << endl;
            return;
        }
    }

    vector<int> in(MAX, 0), out(MAX, 0);
    rep(i, N) {
        in[W[i]]++;
        out[H[i]]++;
    }
    int s = -1, t = -1;
    for (int k = 0; k < MAX; k++) {
        if (in[k] - out[k] == 0) continue;
        if (in[k] - out[k] == 1) {
            if (t != -1) {
                cout << 0 << endl;
                return;
            }
            t = k;
        } else if (in[k] - out[k] == -1) {
            if (s != -1) {
                cout << 0 << endl;
                return;
            }
            s = k;
        } else {
            cout << 0 << endl;
            return;
        }
    }

    if (s == -1 && t == -1) {
        int cnt = 0;
        rep(k, MAX) if (in[k] > 0) cnt++;
        cout << cnt << endl;
    } else if (s == -1 || t == -1) {
        cout << 0 << endl;
    } else {
        cout << 1 << endl;
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0