結果

問題 No.1605 Matrix Shape
ユーザー nawawannawawan
提出日時 2021-07-16 23:31:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 390 ms / 2,000 ms
コード長 1,515 bytes
コンパイル時間 2,130 ms
コンパイル使用メモリ 179,340 KB
実行使用メモリ 17,064 KB
最終ジャッジ日時 2023-09-20 16:03:12
合計ジャッジ時間 8,109 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,236 KB
testcase_01 AC 5 ms
6,328 KB
testcase_02 AC 5 ms
6,152 KB
testcase_03 AC 5 ms
6,468 KB
testcase_04 AC 4 ms
6,300 KB
testcase_05 AC 6 ms
6,148 KB
testcase_06 AC 5 ms
6,236 KB
testcase_07 AC 5 ms
6,468 KB
testcase_08 AC 5 ms
6,240 KB
testcase_09 AC 6 ms
6,196 KB
testcase_10 AC 5 ms
6,512 KB
testcase_11 AC 5 ms
6,336 KB
testcase_12 AC 5 ms
6,336 KB
testcase_13 AC 5 ms
6,188 KB
testcase_14 AC 6 ms
6,192 KB
testcase_15 AC 5 ms
6,264 KB
testcase_16 AC 5 ms
6,360 KB
testcase_17 AC 6 ms
6,208 KB
testcase_18 AC 6 ms
6,208 KB
testcase_19 AC 375 ms
17,064 KB
testcase_20 AC 254 ms
12,572 KB
testcase_21 AC 256 ms
12,628 KB
testcase_22 AC 355 ms
17,028 KB
testcase_23 AC 379 ms
17,012 KB
testcase_24 AC 390 ms
16,972 KB
testcase_25 AC 101 ms
8,056 KB
testcase_26 AC 102 ms
7,820 KB
testcase_27 AC 101 ms
7,784 KB
testcase_28 AC 99 ms
7,788 KB
testcase_29 AC 101 ms
7,820 KB
testcase_30 AC 257 ms
12,260 KB
testcase_31 AC 238 ms
12,220 KB
testcase_32 AC 191 ms
10,224 KB
testcase_33 AC 183 ms
10,148 KB
testcase_34 AC 107 ms
7,916 KB
testcase_35 AC 267 ms
14,952 KB
testcase_36 AC 145 ms
11,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
    vector<int> par;
    vector<int> rank;
    UnionFind(int n){
        par.resize(n);
        rank.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;
            rank[i] = 1;
        }
    }

    int root(int x){
        if(par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    bool same(int x, int y){
        return root(x) == root(y);
    }

    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y) return;
        if(rank[x] < rank[y]) swap(x, y);
        par[y] = x;
        rank[x] += rank[y];
    }

    int size(int x) {
        return rank[root(x)];
    }
};
int main(){
    int N;
    cin >> N;
    vector<int> H(N), W(N);
    UnionFind U(200010);
    vector<int> iri(200010), de(200010);
    set<int> s;
    for(int i = 0; i < N; i++){
        cin >> H[i] >> W[i];
        H[i]--;
        W[i]--;
        s.insert(H[i]);
        s.insert(W[i]);
        U.unite(H[i], W[i]);
        iri[W[i]]++;
        de[H[i]]++;
    }
    int temp = 0;
    for(int i = 0; i < 200010; i++){
        temp = max(temp, U.size(i));
    }
    if(temp == (int)s.size()){
        map<int, int> m;
        for(int i = 0; i < 200010; i++){
            if(s.count(i)) m[iri[i] - de[i]]++;
        }
        if(m[0] == (int)s.size()) cout << s.size() << endl;
        else if(m[-1] == 1 && m[1] == 1) cout << 1 << endl;
        else cout << 0 << endl;
    }
    else cout << 0 << endl;
}
0