結果

問題 No.556 仁義なきサルたち
ユーザー tactac
提出日時 2019-09-07 18:14:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,684 bytes
コンパイル時間 1,621 ms
コンパイル使用メモリ 168,292 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-09 07:05:44
合計ジャッジ時間 3,385 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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<int> data;
    int sz;
    UnionFind(int sz) : data(sz) {
        sz = sz;
        rep(i, sz) {
            data[i] = -10000;
            data[i] -= i;
        }
    }
    bool connect(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        // size優先
        if (data[x] % 10000 * 10000 < data[y] % 10000 * 10000) {
            swap(x, y);
        } else if (data[x] / 10000 > data[y] / 10000) {
            swap(x, y);
        }
        
        // xが親
        data[x] += data[y] % 10000 * 10000;
        data[y] = x;
        return true;
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
};

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