結果

問題 No.556 仁義なきサルたち
コンテスト
ユーザー 梧桐
提出日時 2026-01-23 01:19:02
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,054 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 664 ms
コンパイル使用メモリ 81,012 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-01-23 01:19:04
合計ジャッジ時間 1,615 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>

using namespace std;

const int N = 10010;

int n, m, fa[N], sz[N];

int Find(int x) {
    if (x != fa[x]) fa[x] = Find(fa[x]);
    return fa[x];
}

int main() {
    // freopen("monkey.in", "r", stdin);
    // freopen("monkey.out", "w", stdout);

    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;

    while (m--) {
        int x, y;
        scanf("%d%d", &x, &y);
        int fx = Find(x), fy = Find(y);
        if (fx != fy) {
            int sx = sz[fx], sy = sz[fy];
            if (sx == sy) {
                if (fx < fy) {
                    fa[fy] = fx;
                    sz[fx] += sz[fy];
                } else {
                    fa[fx] = fy;
                    sz[fy] += sz[fx];
                }
            } else if (sx > sy) {
                fa[fy] = fx;
                sz[fx] += sz[fy];
            } else {
                fa[fx] = fy;
                sz[fy] += sz[fx];
            }
        }
    }
    for (int i = 1; i <= n; ++i) printf("%d\n", Find(i));

    return 0;
}
0