結果

問題 No.1605 Matrix Shape
ユーザー nawawannawawan
提出日時 2021-07-16 23:30:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,500 bytes
コンパイル時間 1,656 ms
コンパイル使用メモリ 180,668 KB
実行使用メモリ 17,096 KB
最終ジャッジ日時 2023-09-20 16:01:49
合計ジャッジ時間 6,900 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,340 KB
testcase_01 WA -
testcase_02 AC 5 ms
6,488 KB
testcase_03 WA -
testcase_04 AC 5 ms
6,172 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 5 ms
6,188 KB
testcase_08 WA -
testcase_09 AC 6 ms
6,116 KB
testcase_10 AC 6 ms
6,356 KB
testcase_11 AC 5 ms
6,364 KB
testcase_12 WA -
testcase_13 AC 5 ms
6,336 KB
testcase_14 AC 6 ms
6,184 KB
testcase_15 AC 6 ms
6,128 KB
testcase_16 AC 5 ms
6,168 KB
testcase_17 AC 5 ms
6,164 KB
testcase_18 AC 5 ms
6,360 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 220 ms
12,512 KB
testcase_22 AC 327 ms
17,068 KB
testcase_23 WA -
testcase_24 AC 323 ms
16,988 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 100 ms
7,804 KB
testcase_28 AC 99 ms
7,924 KB
testcase_29 AC 100 ms
7,712 KB
testcase_30 WA -
testcase_31 AC 217 ms
12,680 KB
testcase_32 WA -
testcase_33 AC 172 ms
10,236 KB
testcase_34 WA -
testcase_35 AC 215 ms
14,896 KB
testcase_36 AC 115 ms
11,824 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++){
            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