結果

問題 No.556 仁義なきサルたち
ユーザー tactac
提出日時 2019-09-07 18:28:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,569 bytes
コンパイル時間 1,973 ms
コンパイル使用メモリ 168,452 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-09 07:24:25
合計ジャッジ時間 2,559 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }


struct UnionFind {
    vector<double> data;
    int sz;
    UnionFind(int sz) : data(sz, -1) { sz = sz; }
    bool connect(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        if (data[x] > data[y]) swap(x, y);
        else if (data[x] == data[y]) {
            if (root(x) > root(y)) swap(x, y);
        }
        data[x] += data[y]; data[y] = x; sz--;
        return true;
    }
    bool issame(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return sz; }
};


int main() {
    
    int n, m;
    cin >> n >> m;
    UnionFind uni(n);
    rep(i, m) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        uni.connect(x, y);
    }
    rep(i, n) cout << uni.root(i) + 1 << '\n';
    
}
0