結果

問題 No.1605 Matrix Shape
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-08 23:15:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,863 bytes
コンパイル時間 1,195 ms
コンパイル使用メモリ 111,612 KB
実行使用メモリ 6,456 KB
最終ジャッジ日時 2023-08-30 02:08:38
合計ジャッジ時間 6,881 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,316 KB
testcase_01 AC 4 ms
6,188 KB
testcase_02 AC 4 ms
6,224 KB
testcase_03 AC 5 ms
6,208 KB
testcase_04 AC 4 ms
6,196 KB
testcase_05 AC 4 ms
6,144 KB
testcase_06 AC 4 ms
6,228 KB
testcase_07 AC 4 ms
6,208 KB
testcase_08 AC 4 ms
6,144 KB
testcase_09 AC 4 ms
6,148 KB
testcase_10 AC 4 ms
6,308 KB
testcase_11 AC 5 ms
6,192 KB
testcase_12 AC 4 ms
6,192 KB
testcase_13 AC 5 ms
6,188 KB
testcase_14 AC 4 ms
6,144 KB
testcase_15 AC 4 ms
6,188 KB
testcase_16 AC 4 ms
6,288 KB
testcase_17 AC 4 ms
6,144 KB
testcase_18 AC 5 ms
6,292 KB
testcase_19 AC 131 ms
6,200 KB
testcase_20 AC 116 ms
6,232 KB
testcase_21 AC 116 ms
6,184 KB
testcase_22 AC 135 ms
6,228 KB
testcase_23 AC 131 ms
6,224 KB
testcase_24 AC 136 ms
6,192 KB
testcase_25 AC 79 ms
6,172 KB
testcase_26 AC 79 ms
6,300 KB
testcase_27 AC 79 ms
6,304 KB
testcase_28 AC 78 ms
6,204 KB
testcase_29 AC 79 ms
6,228 KB
testcase_30 AC 117 ms
6,456 KB
testcase_31 AC 117 ms
6,204 KB
testcase_32 AC 108 ms
6,176 KB
testcase_33 AC 107 ms
6,172 KB
testcase_34 AC 104 ms
6,292 KB
testcase_35 AC 107 ms
6,188 KB
testcase_36 AC 62 ms
6,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>

using namespace std;
using ll = long long;

struct UnionFind {
    int ngroup;
    vector<int> par;
    vector<int> siz;

    UnionFind(int N) : par(N), siz(N) {
        ngroup = N;
        for(int i = 0; i < N; i++){
            par[i] = i;
            siz[i] = 1;
        }
    }

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

    int unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return rx;
        ngroup--;
        if (siz[rx] > siz[ry]) swap(rx, ry);
        par[rx] = ry;
        siz[ry] += siz[rx];
        return ry;
    }

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

    int size(int x){
        return siz[root(x)];
    }

    int group_count(){
        return ngroup;
    }
};

int main(){
 
    int N, p=0, m=0, z=0, a, b, M=2e5, K=0;
    cin >> N;
    UnionFind tree(M+1);
    vector<ll> cnt(M+1);
    vector<bool> visit(M+1);
 
    for (int i=0; i<N; i++){
        cin >> a >> b;
        tree.unite(a, b);
        cnt[a]--;
        cnt[b]++;
        if (!visit[a]){
            K++;
            visit[a] = 1;
        }
        if (!visit[b]){
            K++;
            visit[b] = 1;
        }
    }

    for (int i=1; i<=M; i++){
        if (!visit[i]) continue;
        if (cnt[i] == 0) z++;
        else if (cnt[i] == 1) p++;
        else if (cnt[i] == -1) m++;
    }

    if (tree.size(a) != K) cout << 0 << endl;
    else{
        if (z == K) cout << K << endl;
        else if (p == 1 && m == 1 && z == (K-2)) cout << 1 << endl;
        else cout << 0 << endl;
    }
 
    return 0;
}
0