結果

問題 No.1605 Matrix Shape
ユーザー nawawannawawan
提出日時 2021-07-16 22:20:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,015 bytes
コンパイル時間 1,773 ms
コンパイル使用メモリ 175,720 KB
実行使用メモリ 43,512 KB
最終ジャッジ日時 2023-09-20 14:32:07
合計ジャッジ時間 8,554 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
10,860 KB
testcase_01 AC 8 ms
10,744 KB
testcase_02 AC 8 ms
10,724 KB
testcase_03 AC 9 ms
10,892 KB
testcase_04 AC 8 ms
10,824 KB
testcase_05 AC 9 ms
10,724 KB
testcase_06 AC 9 ms
10,888 KB
testcase_07 AC 9 ms
10,780 KB
testcase_08 AC 8 ms
10,964 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 9 ms
10,948 KB
testcase_13 AC 8 ms
11,088 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 8 ms
10,860 KB
testcase_18 AC 8 ms
10,880 KB
testcase_19 AC 466 ms
43,512 KB
testcase_20 AC 307 ms
27,884 KB
testcase_21 WA -
testcase_22 AC 475 ms
35,892 KB
testcase_23 AC 487 ms
40,024 KB
testcase_24 WA -
testcase_25 AC 109 ms
13,388 KB
testcase_26 AC 109 ms
13,448 KB
testcase_27 WA -
testcase_28 AC 107 ms
13,476 KB
testcase_29 WA -
testcase_30 AC 329 ms
20,216 KB
testcase_31 WA -
testcase_32 AC 239 ms
19,400 KB
testcase_33 AC 235 ms
19,384 KB
testcase_34 AC 112 ms
12,996 KB
testcase_35 AC 311 ms
27,824 KB
testcase_36 AC 176 ms
27,188 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 dfs(int now, int par, vector<vector<int>>&G, vector<int>&d){
    if(d[now] != -1) return d[now];
    d[now] = 0;
    for(int t: G[now]){
        if(t == now || t == par) continue;
        d[now] = max(d[now], dfs(t, now, G, d));
    }
    d[now]++;
    return d[now];
}
int main(){
    int N;
    cin >> N;
    vector<int> H(N), W(N);
    vector<vector<int>>  G(200010);
    set<int> s;
    vector<int> cnt(200010, -1);
    UnionFind U(200010);
    for(int i = 0; i < N; i++){
        cin >> H[i] >> W[i];
        H[i]--;
        W[i]--;
        s.insert(H[i]);
        s.insert(W[i]);
        G[H[i]].push_back(W[i]);
        if(cnt[W[i]] == -1) cnt[W[i]] = 0;
        if(cnt[H[i]] == -1) cnt[H[i]] = 0;
        U.unite(W[i], H[i]);
        cnt[W[i]]++;
    }
    bool f = true;
    vector<int> d(200010, -1);
    for(int i = 0; i < 200010; i++){
        if(cnt[i] == 0) {
            f = false;
        }
    }
    int temp = 0;
    for(int i = 0; i < 200010; i++){
        temp = max(U.size(i), temp);
        dfs(i, -1, G, d);
    }
    if(f) {
        if(temp == (int)s.size()) cout << s.size() << endl;
        else cout << 0 << endl;
    }
    else {
        temp = *max_element(d.begin(), d.end());
        if(temp == (int)s.size()) cout << 1 << endl;
        else cout << 0 << endl;
    }

}
0