結果

問題 No.556 仁義なきサルたち
ユーザー dnishdnish
提出日時 2018-06-22 02:00:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,837 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 167,264 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-13 07:48:53
合計ジャッジ時間 2,890 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 9 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 19 ms
4,380 KB
testcase_19 AC 18 ms
4,376 KB
testcase_20 AC 19 ms
4,376 KB
testcase_21 AC 19 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define REP(i,n,N) for(ll i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
#define p2(a,b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9+7;

const int MAX_N = 10010;

struct UnionFind {
    int par[MAX_N];    // 親ノードの番号
    int deph[MAX_N];   // ノードの深さ

    /* 上以外に持っておくデータ
    // 例)木であるかどうか(bool値)を持つ場合
    // bool istree[MAX_N];
    */

    UnionFind(int n) {
        fill(par, par + MAX_N, -1);
        for (int i = 0; i<n; i++) {
            par[i] = i;
            deph[i] = 0;

            /* 持っておくデータにおいて初期化が必要な場合
            // istree[i] = true;
            */
        }
    }

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

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return;
        }
        if (deph[x] < deph[y] ||(deph[x] == deph[y] && x > y)) {
            par[x] = par[y];
            deph[y]++;
            /* ルートをyとする時の処理
            */
        } else {
            par[y] = par[x];
            deph[x]++;
            /* ルートをxとする時の処理
            */
        }
    }

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

int main() {
    int N,M;
    cin>>N>>M;
    UnionFind uf(N);
    REP(i,0,M){
        int a,b;
        cin>>a>>b;
        a--;b--;
        if(!uf.same(a,b)) uf.unite(a, b);

    }
    REP(i,0,N){
        p(uf.find(i)+1);
    }
}
0