結果

問題 No.1605 Matrix Shape
ユーザー milanis48663220milanis48663220
提出日時 2021-07-16 21:52:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 2,279 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 124,156 KB
実行使用メモリ 25,684 KB
最終ジャッジ日時 2023-09-20 13:42:09
合計ジャッジ時間 6,483 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,412 KB
testcase_01 AC 4 ms
5,384 KB
testcase_02 AC 3 ms
5,572 KB
testcase_03 AC 4 ms
5,388 KB
testcase_04 AC 3 ms
5,392 KB
testcase_05 AC 4 ms
5,408 KB
testcase_06 AC 3 ms
5,304 KB
testcase_07 AC 4 ms
5,392 KB
testcase_08 AC 4 ms
5,428 KB
testcase_09 AC 4 ms
5,300 KB
testcase_10 AC 4 ms
5,300 KB
testcase_11 AC 3 ms
5,360 KB
testcase_12 AC 4 ms
5,352 KB
testcase_13 AC 3 ms
5,300 KB
testcase_14 AC 4 ms
5,356 KB
testcase_15 AC 4 ms
5,480 KB
testcase_16 AC 4 ms
5,384 KB
testcase_17 AC 3 ms
5,284 KB
testcase_18 AC 4 ms
5,364 KB
testcase_19 AC 295 ms
25,684 KB
testcase_20 AC 186 ms
16,208 KB
testcase_21 AC 137 ms
11,636 KB
testcase_22 AC 209 ms
16,212 KB
testcase_23 AC 301 ms
25,656 KB
testcase_24 AC 213 ms
16,108 KB
testcase_25 AC 60 ms
7,068 KB
testcase_26 AC 60 ms
6,944 KB
testcase_27 AC 50 ms
6,972 KB
testcase_28 AC 50 ms
6,900 KB
testcase_29 AC 50 ms
6,960 KB
testcase_30 AC 189 ms
16,188 KB
testcase_31 AC 144 ms
11,820 KB
testcase_32 AC 139 ms
11,660 KB
testcase_33 AC 101 ms
9,440 KB
testcase_34 AC 38 ms
7,060 KB
testcase_35 AC 139 ms
14,272 KB
testcase_36 AC 70 ms
10,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;
using P = pair<int,int>;


struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<int> in(200000), out(200000);
    UnionFind uf(200000);
    vector<int> h(n), w(n);
    set<int> used;
    for(int i = 0; i < n; i++){
        cin >> h[i] >> w[i]; h[i]--; w[i]--;
        out[w[i]]++;
        in[h[i]]++;
        uf.unionSet(h[i],w[i]);
        used.insert(h[i]);
        used.insert(w[i]);
    }
    if(uf.size(h[0]) != used.size()){
        cout << 0 << endl;
        return 0;
    }
    int cnt_s = 0;
    int cnt_t = 0;
    for(int i = 0; i < 200000; i++){
        if(abs(in[i]-out[i]) > 1){
            cout << 0 << endl;
            return 0;
        }
        if(in[i] > out[i]) cnt_t++;
        if(out[i] > in[i]) cnt_s++;
    }
    if(cnt_t ==  cnt_s){
        if(cnt_t == 0) {
            set<int> st;
            for(int i = 0; i < n; i++) st.insert(h[i]);
            cout << st.size() << endl;
        }
        else if(cnt_t == 1) cout << 1 << endl;
        else cout << 0 << endl;
    }else{
        cout << 0 << endl;
    }
}
0