結果

問題 No.1605 Matrix Shape
ユーザー sten_sansten_san
提出日時 2021-07-16 23:26:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 2,133 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 212,948 KB
実行使用メモリ 38,504 KB
最終ジャッジ日時 2023-09-20 15:58:01
合計ジャッジ時間 7,092 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
14,780 KB
testcase_01 AC 10 ms
14,756 KB
testcase_02 AC 10 ms
14,828 KB
testcase_03 AC 10 ms
14,656 KB
testcase_04 AC 10 ms
14,816 KB
testcase_05 AC 10 ms
14,700 KB
testcase_06 AC 11 ms
14,772 KB
testcase_07 AC 9 ms
14,656 KB
testcase_08 AC 10 ms
14,952 KB
testcase_09 AC 11 ms
14,792 KB
testcase_10 AC 10 ms
14,964 KB
testcase_11 AC 10 ms
14,880 KB
testcase_12 AC 10 ms
14,692 KB
testcase_13 AC 10 ms
14,724 KB
testcase_14 AC 11 ms
14,760 KB
testcase_15 AC 11 ms
14,664 KB
testcase_16 AC 10 ms
14,696 KB
testcase_17 AC 10 ms
14,688 KB
testcase_18 AC 11 ms
14,876 KB
testcase_19 AC 340 ms
38,212 KB
testcase_20 AC 227 ms
27,172 KB
testcase_21 AC 135 ms
22,692 KB
testcase_22 AC 162 ms
28,852 KB
testcase_23 AC 330 ms
38,504 KB
testcase_24 AC 170 ms
28,748 KB
testcase_25 AC 66 ms
18,408 KB
testcase_26 AC 67 ms
18,440 KB
testcase_27 AC 48 ms
18,516 KB
testcase_28 AC 45 ms
18,476 KB
testcase_29 AC 48 ms
18,480 KB
testcase_30 AC 226 ms
27,400 KB
testcase_31 AC 135 ms
22,800 KB
testcase_32 AC 173 ms
23,220 KB
testcase_33 AC 110 ms
20,592 KB
testcase_34 AC 50 ms
17,884 KB
testcase_35 AC 128 ms
26,208 KB
testcase_36 AC 74 ms
21,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

#include <atcoder/dsu>

int main() {
    constexpr int lim = 200000;

    int n; cin >> n;

    auto g = vec<int>(uns, lim, 0);
    auto r = vec<int>(uns, lim, 0);
    auto h = vec<int>(uns, n);
    auto w = vec<int>(uns, n);
    for (int i = 0; i < n; ++i) {
        cin >> h[i] >> w[i]; --h[i]; --w[i];
        g[h[i]].push_back(w[i]);
        r[w[i]].push_back(h[i]);
    }

    atcoder::dsu dsu(lim);
    auto in = vec<int>(0, lim);
    auto out = vec<int>(0, lim);
    for (int i = 0; i < lim; ++i) {
        for (auto v : g[i]) {
            ++out[i]; --out[v];
            ++in[v]; --in[i];
            dsu.merge(i, v);
        }
    }

    for (int i = 0; i < n; ++i) {
        if (!dsu.same(h[0], h[i]) || !dsu.same(w[0], w[i])) {
            cout << 0 << endl;
            return 0;
        }
    }

    if (count(begin(out), end(out), 0) == lim) {
        set<int> ans;
        for (int i = 0; i < n; ++i) {
            ans.insert(h[i]);
            ans.insert(w[i]);
        }

        cout << size(ans) << endl;
        return 0;
    }

    int p = 0;
    int m = 0;
    int o = 0;

    for (auto v : in) {
        p += v == +1;
        m += v == -1;
        o += v == 0;
    }

    if (!(p == 1 && m == 1 && o == lim - 2)) {
        cout << 0 << endl;
        return 0;
    }

    cout << 1 << endl;
}

0