結果

問題 No.1605 Matrix Shape
ユーザー FukucchiFukucchi
提出日時 2021-07-17 11:01:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 9,790 bytes
コンパイル時間 4,635 ms
コンパイル使用メモリ 199,216 KB
実行使用メモリ 38,364 KB
最終ジャッジ日時 2023-09-21 03:57:43
合計ジャッジ時間 7,863 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,620 KB
testcase_01 AC 6 ms
13,860 KB
testcase_02 AC 2 ms
4,464 KB
testcase_03 AC 7 ms
13,764 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 7 ms
13,604 KB
testcase_06 AC 6 ms
13,640 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 7 ms
13,856 KB
testcase_09 AC 6 ms
13,620 KB
testcase_10 AC 6 ms
13,652 KB
testcase_11 AC 7 ms
13,248 KB
testcase_12 AC 6 ms
13,580 KB
testcase_13 AC 6 ms
13,636 KB
testcase_14 AC 6 ms
13,648 KB
testcase_15 AC 7 ms
13,604 KB
testcase_16 AC 6 ms
13,624 KB
testcase_17 AC 7 ms
13,176 KB
testcase_18 AC 8 ms
13,488 KB
testcase_19 AC 230 ms
38,364 KB
testcase_20 AC 176 ms
27,500 KB
testcase_21 AC 93 ms
22,812 KB
testcase_22 AC 54 ms
7,112 KB
testcase_23 AC 229 ms
38,260 KB
testcase_24 AC 105 ms
28,820 KB
testcase_25 AC 67 ms
20,492 KB
testcase_26 AC 66 ms
20,424 KB
testcase_27 AC 47 ms
20,300 KB
testcase_28 AC 39 ms
6,984 KB
testcase_29 AC 47 ms
20,356 KB
testcase_30 AC 176 ms
28,552 KB
testcase_31 AC 95 ms
23,740 KB
testcase_32 AC 146 ms
24,764 KB
testcase_33 AC 45 ms
7,156 KB
testcase_34 AC 52 ms
19,620 KB
testcase_35 AC 77 ms
25,968 KB
testcase_36 AC 54 ms
21,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* #region  header */
#pragma GCC optimize("O3") //コンパイラ最適化用

#ifdef LOCAL
#define _GLIBCXX_DEBUG //配列に[]でアクセス時のエラー表示
#endif
#define _USE_MATH_DEFINES
#include <algorithm>   //sort,二分探索,など
#include <atcoder/all> // CodeForcesの場合ファイルごとに入れる
#include <bitset>      //固定長bit集合
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <cassert> //assert(p)で,not pのときにエラー
#include <cctype>
#include <chrono> //実行時間計測
#include <climits>
#include <cmath>   //pow,logなど
#include <complex> //複素数
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional> //sortのgreater
#include <iomanip>    //setprecision(浮動小数点の出力の誤差)
#include <ios>        // std::left, std::right
#include <iostream>   //入出力
#include <iterator>   //集合演算(積集合,和集合,差集合など)
#include <map>
#include <numeric> //iota(整数列の生成),gcdとlcm,accumulate
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility> //pair
#include <vector>
using namespace std;
using namespace atcoder;
typedef long long LL;
typedef long double LD;

#define ALL(x) x.begin(), x.end()
const long long INF = numeric_limits<long long>::max() / 4;
const int MOD = 1e9 + 7;
// const int MOD=998244353;
//略記
#define FF first
#define SS second
#define int long long
#define stoi stoll
#define LD long double
#define PII pair<int, int>
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(x) (int)((x).size())
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define VI vector<int>
#define VVI vector<vector<int>>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n) - (int)1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(b) - (int)1; i >= (int)a; i--)

const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

int in() {
    int x;
    cin >> x;
    return x;
}
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed#c%E3%81%A7%E3%81%AE%E5%AE%9F%E8%A3%85
void print() { cout << "\n"; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    cout << head;
    if (sizeof...(tail) != 0)
        cout << " ";
    print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
    for (auto &a : vec) {
        cout << a;
        if (&a != &vec.back())
            cout << " ";
    }
    cout << "\n";
}
template <class T> void print(set<T> &set) {
    for (auto &a : set) {
        cout << a << " ";
    }
    cout << "\n";
}
template <class T> void print(vector<vector<T>> &df) {
    for (auto &vec : df) {
        print(vec);
    }
}

// debug macro
// https://atcoder.jp/contests/abc202/submissions/22815715
namespace debugger {
template <class T> void view(const std::vector<T> &a) {
    std::cerr << "{ ";
    for (const auto &v : a) {
        std::cerr << v << ", ";
    }
    std::cerr << "\b\b }";
}
template <class T> void view(const std::vector<std::vector<T>> &a) {
    std::cerr << "{\n";
    for (const auto &v : a) {
        std::cerr << "\t";
        view(v);
        std::cerr << "\n";
    }
    std::cerr << "}";
}
template <class T, class U> void view(const std::vector<std::pair<T, U>> &a) {
    std::cerr << "{\n";
    for (const auto &p : a)
        std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
    std::cerr << "}";
}
template <class T, class U> void view(const std::map<T, U> &m) {
    std::cerr << "{\n";
    for (const auto &p : m)
        std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
    std::cerr << "}";
}
template <class T, class U> void view(const std::pair<T, U> &p) {
    std::cerr << "(" << p.first << ", " << p.second << ")";
}
template <class T> void view(const std::set<T> &s) {
    std::cerr << "{ ";
    for (auto &v : s) {
        view(v);
        std::cerr << ", ";
    }
    std::cerr << "\b\b }";
}

template <class T> void view(const T &e) { std::cerr << e; }
} // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
    debugger::view(H);
    std::cerr << ", ";
    debug_out(T...);
}
#define debug(...)                                                             \
    do {                                                                       \
        std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : [";              \
        debug_out(__VA_ARGS__);                                                \
        std::cerr << "\b\b]\n";                                                \
    } while (false)
#else
#define debug(...) (void(0))
#endif

long long power(long long x, long long n) {
    // O(logn)
    // https://algo-logic.info/calc-pow/#toc_id_1_2
    long long ret = 1;
    while (n > 0) {
        if (n & 1)
            ret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける
        x *= x;
        n >>= 1; // n を1bit 左にずらす
    }
    return ret;
}
// @brief nCr. O(r log n)。あるいは前処理 O(n), 本処理 O(1)で求められる modint
// の bc を検討。
int comb(int n, int r) {
    // https://www.geeksforgeeks.org/program-to-calculate-the-value-of-ncr-efficiently/
    if (n < r)
        return 0;

    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    long long p = 1, k = 1;

    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;

            // gcd of p, k
            long long m = __gcd(p, k);

            // dividing by gcd, to simplify
            // product division by their gcd
            // saves from the overflow
            p /= m;
            k /= m;

            n--;
            r--;
        }

        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }

    else
        p = 1;

    // if our approach is correct p = ans and k =1
    return p;
}

// MOD
void add(long long &a, long long b) {
    a += b;
    if (a >= MOD)
        a -= MOD;
}
template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

// 負数も含む丸め
long long ceildiv(long long a, long long b) {
    if (b < 0)
        a = -a, b = -b;
    if (a >= 0)
        return (a + b - 1) / b;
    else
        return a / b;
}
long long floordiv(long long a, long long b) {
    if (b < 0)
        a = -a, b = -b;
    if (a >= 0)
        return a / b;
    else
        return (a - b + 1) / b;
}
long long floorsqrt(long long x) {
    assert(x >= 0);
    long long ok = 0;
    long long ng = 1;
    while (ng * ng <= x)
        ng <<= 1;
    while (ng - ok > 1) {
        long long mid = (ng + ok) >> 1;
        if (mid * mid <= x)
            ok = mid;
        else
            ng = mid;
    }
    return ok;
}

// @brief a^n mod mod
long long modpower(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// @brief s が c を含むか
template <class T> bool contain(const std::string &s, const T &c) {
    return s.find(c) != std::string::npos;
}

__attribute__((constructor)) void faster_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cerr.tie(nullptr);
}
/* #endregion */

int n;
VI H, W;
int euler_closed_path(VVI &G1, VVI &G2) {
    REP(ni, n) {
        if (SZ(G1[H[ni]]) != SZ(G2[H[ni]])) {
            return -1;
        }
        if (SZ(G1[W[ni]]) != SZ(G2[W[ni]])) {
            return -1;
        }
    }
    set<int> sz;
    REP(ni, n) {
        sz.insert(H[ni]);
        sz.insert(W[ni]);
    }
    return SZ(sz);
}

int euler_path(VVI &G1, VVI &G2) {
    set<int> st, ed;

    REP(ni, n) {
        if (SZ(G1[H[ni]]) + 1 == SZ(G2[H[ni]])) {
            st.insert(H[ni]);
        } else if (SZ(G1[H[ni]]) == SZ(G2[H[ni]]) + 1) {
            ed.insert(H[ni]);
        } else if (SZ(G1[H[ni]]) == SZ(G2[H[ni]])) {
            ;
        } else {
            return -1;
        }

        if (SZ(G1[W[ni]]) + 1 == SZ(G2[W[ni]])) {
            st.insert(W[ni]);
        } else if (SZ(G1[W[ni]]) == SZ(G2[W[ni]]) + 1) {
            ed.insert(W[ni]);
        } else if (SZ(G1[W[ni]]) == SZ(G2[W[ni]])) {
            ;
        } else {
            return -1;
        }
    }
    if (SZ(st) == 1 and SZ(ed) == 1) {
        return 1;
    }
    return 0;
}
signed main() {
    cin >> n;
    H.assign(n, -1);
    W.assign(n, -1);
    REP(ni, n) { cin >> H[ni] >> W[ni]; }
    int MAX = 2e5 + 10;
    dsu d(MAX);
    REP(ni, n) { d.merge(H[ni], W[ni]); }
    set<int> cnt;
    REP(ni, n) {
        cnt.insert(d.leader(H[ni]));
        cnt.insert(d.leader(W[ni]));
    }
    if (SZ(cnt) != 1) {
        print(0);
        exit(0);
    }

    VVI G1(MAX), G2(MAX);
    REP(ni, n) {
        G1[H[ni]].PB(W[ni]);
        G2[W[ni]].PB(H[ni]);
    }
    int ret1 = euler_closed_path(G1, G2);
    debug(ret1);
    if (ret1 > -1) {
        print(ret1);
        exit(0);
    }
    int ret2 = euler_path(G1, G2);
    debug(ret2);

    if (ret2 > -1) {
        print(ret2);
        exit(0);
    }
    print(0);
    return 0;
}
0