結果

問題 No.1660 Matrix Exponentiation
ユーザー nawawannawawan
提出日時 2021-08-27 23:18:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,464 bytes
コンパイル時間 2,177 ms
コンパイル使用メモリ 206,212 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-05-01 04:14:08
合計ジャッジ時間 3,436 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
6,940 KB
testcase_07 WA -
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 2 ms
6,940 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 3 ms
6,940 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 22 ms
6,944 KB
testcase_28 AC 25 ms
6,944 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, K;
    cin >> N >> K;
    UnionFind U(2 * N);
    vector<int> r(K), c(K);
    bool f = false;
    for(int i = 0; i < K; i++) {
        cin >> r[i] >> c[i];
        r[i]--;
        c[i]--;
        if(U.same(r[i], N  + c[i])){
            f = true;
        }
        U.unite(r[i], N + c[i]);
    }
    if(f){
        cout << -1 << endl;
        return 0;
    }
    if(K == 0){
        cout << 1 << endl;
        return 0;
    }
    for(int i = 0; i < K; i++){
        if(r[i] == c[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    int ma = 0;
    for(int i = 0; i < N; i++){
        ma = max(U.size(i), ma);
    }
    cout << ma / 2 + 1 << endl;
}
0