結果

問題 No.1605 Matrix Shape
ユーザー snow39snow39
提出日時 2021-07-16 21:50:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,625 bytes
コンパイル時間 1,092 ms
コンパイル使用メモリ 112,956 KB
実行使用メモリ 7,248 KB
最終ジャッジ日時 2023-09-20 13:37:30
合計ジャッジ時間 3,839 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,384 KB
testcase_01 AC 5 ms
5,428 KB
testcase_02 AC 4 ms
5,444 KB
testcase_03 AC 5 ms
5,428 KB
testcase_04 AC 4 ms
5,628 KB
testcase_05 AC 5 ms
5,396 KB
testcase_06 AC 5 ms
5,424 KB
testcase_07 AC 3 ms
5,392 KB
testcase_08 AC 5 ms
5,380 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 5 ms
5,388 KB
testcase_11 WA -
testcase_12 AC 5 ms
5,432 KB
testcase_13 AC 5 ms
5,384 KB
testcase_14 AC 5 ms
5,468 KB
testcase_15 AC 5 ms
5,372 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 52 ms
6,960 KB
testcase_20 AC 47 ms
6,952 KB
testcase_21 AC 46 ms
6,968 KB
testcase_22 AC 46 ms
7,248 KB
testcase_23 AC 51 ms
7,076 KB
testcase_24 AC 51 ms
7,012 KB
testcase_25 AC 35 ms
7,008 KB
testcase_26 AC 35 ms
7,008 KB
testcase_27 AC 35 ms
7,012 KB
testcase_28 AC 33 ms
7,080 KB
testcase_29 AC 34 ms
7,052 KB
testcase_30 AC 46 ms
6,968 KB
testcase_31 AC 46 ms
6,956 KB
testcase_32 AC 43 ms
7,060 KB
testcase_33 AC 40 ms
6,956 KB
testcase_34 AC 38 ms
6,980 KB
testcase_35 AC 40 ms
6,752 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
        }
        if (in[k] - out[k] == -1) {
            if (s != -1) {
                cout << 0 << endl;
                return;
            }
            s = k;
        }
    }

    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