結果

問題 No.1605 Matrix Shape
ユーザー ytftytft
提出日時 2021-12-29 19:20:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 658 ms / 2,000 ms
コード長 1,822 bytes
コンパイル時間 1,913 ms
コンパイル使用メモリ 178,496 KB
実行使用メモリ 34,532 KB
最終ジャッジ日時 2024-04-15 01:06:27
合計ジャッジ時間 10,384 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 658 ms
34,520 KB
testcase_20 AC 458 ms
24,280 KB
testcase_21 AC 451 ms
24,348 KB
testcase_22 AC 560 ms
34,532 KB
testcase_23 AC 616 ms
34,508 KB
testcase_24 AC 655 ms
34,440 KB
testcase_25 AC 155 ms
14,160 KB
testcase_26 AC 160 ms
14,240 KB
testcase_27 AC 158 ms
14,208 KB
testcase_28 AC 160 ms
14,288 KB
testcase_29 AC 156 ms
14,312 KB
testcase_30 AC 435 ms
24,436 KB
testcase_31 AC 449 ms
24,356 KB
testcase_32 AC 329 ms
19,252 KB
testcase_33 AC 328 ms
19,352 KB
testcase_34 AC 129 ms
14,064 KB
testcase_35 AC 406 ms
28,216 KB
testcase_36 AC 221 ms
18,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind{
    vector<int> parent;
    vector<int> numberOfElements;//根ノードのみ有効
    UnionFind(int n){
        parent.resize(n);
        numberOfElements.resize(n);
        for(int i=0;i<n;++i){
            parent[i]=-1;
            numberOfElements[i]=1;
        }
    }
    int find(int a){
        vector<int> path(0);
        int pointer=a;
        while(parent[pointer]!=-1){
            path.push_back(pointer);
            pointer=parent[pointer];
        }
        for(int i:path){
            parent[i]=pointer;
        }
        return pointer;
    }
    void unite(int a,int b){
        int p=find(a);
        int q=find(b);
        if(p==q){
            return;
        }
        if(numberOfElements[p]<numberOfElements[q]){
            parent[p]=q;
            numberOfElements[q]+=numberOfElements[p];
        }else{
            parent[q]=p;
            numberOfElements[p]+=numberOfElements[q];
        }
    }
};

int main(){
    int N,temp=0,check=0;
    cin>>N;
    map<int,int> cnt;
    vector<vector<int>> a(N,vector<int>(2));
    for(int i=0;i<N;++i){
        for(int j=0;j<2;++j){
            cin>>a[i][j];
            cnt[a[i][j]]+=j*2-1;
        }
    }
    int p=cnt.size();
    map<int,int> comp;
    for(auto i=cnt.begin();i!=cnt.end();++i){
        comp[i->first]=temp;
        ++temp;
        check+=abs(i->second);
    }
    UnionFind U(p);
    for(vector<int> v:a){
        U.unite(comp[v[0]],comp[v[1]]);
    }
    temp=U.find(comp[a[0][0]]);
    for(auto i=cnt.begin();i!=cnt.end();++i){
        if(U.find(comp[i->first])!=temp){
            cout<<0<<endl;
            return 0;
        }
    }
    if(check>2){
        cout<<0<<endl;
    }else if(check){
        cout<<1<<endl;
    }else{
        cout<<p<<endl;
    }
}
0