結果

問題 No.1605 Matrix Shape
ユーザー momoyuumomoyuu
提出日時 2023-11-18 04:25:36
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,046 bytes
コンパイル時間 3,779 ms
コンパイル使用メモリ 115,224 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-18 04:25:45
合計ジャッジ時間 5,145 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,676 KB
testcase_01 AC 3 ms
6,676 KB
testcase_02 AC 4 ms
6,676 KB
testcase_03 AC 4 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 4 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 3 ms
6,676 KB
testcase_08 AC 4 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 4 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 4 ms
6,676 KB
testcase_14 AC 4 ms
6,676 KB
testcase_15 AC 4 ms
6,676 KB
testcase_16 AC 4 ms
6,676 KB
testcase_17 AC 4 ms
6,676 KB
testcase_18 AC 4 ms
6,676 KB
testcase_19 AC 50 ms
6,676 KB
testcase_20 AC 45 ms
6,676 KB
testcase_21 AC 45 ms
6,676 KB
testcase_22 AC 52 ms
6,676 KB
testcase_23 AC 47 ms
6,676 KB
testcase_24 AC 47 ms
6,676 KB
testcase_25 AC 30 ms
6,676 KB
testcase_26 AC 32 ms
6,676 KB
testcase_27 AC 31 ms
6,676 KB
testcase_28 AC 31 ms
6,676 KB
testcase_29 AC 31 ms
6,676 KB
testcase_30 AC 43 ms
6,676 KB
testcase_31 AC 42 ms
6,676 KB
testcase_32 AC 38 ms
6,676 KB
testcase_33 AC 38 ms
6,676 KB
testcase_34 AC 35 ms
6,676 KB
testcase_35 AC 36 ms
6,676 KB
testcase_36 AC 24 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<cassert>
#include<set>
using namespace std;
using ll = long long;

#include<atcoder/dsu>

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

    int n;
    cin>>n;
    atcoder::dsu uf(2<<17);
    vector<int> use(2<<17,0),cnt(2<<17,0);
    for(int i = 0;i<n;i++){
        int h,w;
        cin>>h>>w;
        cnt[h]++;
        cnt[w]--;
        use[h] = 1;
        use[w] = 1;
        uf.merge(h,w);
    }
    set<int> s;
    int a = 0;
    int b = 0;
    bool fn = true;
    int ans = 0;
    for(int i = 0;i<2<<17;i++){
        if(use[i]==0) continue;
        if(cnt[i]==1) a++;
        if(cnt[i]==-1) b++;
        ans++;
        s.insert(uf.leader(i));
        if(abs(cnt[i])>=2) fn = false;
    }
    if(s.size()>=2) fn = false;
    if(!fn){
        cout<<0<<endl;
        return 0;
    }

    if(a==0&&b==0){
        cout<<ans<<endl;
        return 0;
    }
    if(a==1&&b==1){
        cout<<1<<endl;
        return 0;
    }
    cout<<0<<endl;

}

0