結果

問題 No.556 仁義なきサルたち
ユーザー vtr_codervtr_coder
提出日時 2017-08-11 23:38:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 2,008 bytes
コンパイル時間 1,461 ms
コンパイル使用メモリ 166,616 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-02 23:56:31
合計ジャッジ時間 2,507 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))

#define MOD 1000000007
#define INF 1000000000
#define MAX_N 10010

int N, M;
// ランク無し版
int par[MAX_N];  // 親の番号
int team[MAX_N]; // 各チームの人数
// n 要素で初期化
void init(int n) {
    REP(i, n){
        par[i] = i;
        team[i] = 1;
    }    // はじめは全部の頂点が根
}
// 木の根を求める
int root(int x) {
    if(par[x] == x) { return x; }           // 根
    else { return par[x] = root(par[x]); }  // 経路圧縮
}
// x と y が同じ集合に属するか否か
bool same(int x, int y) {
    return root(x) == root(y);
}
// x と y の属する集合を併合
void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) { return; }
    par[x] = y;
}

int main()
{
    cin >> N >> M;
    init(N);
    REP(i, M) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        int winner, loser;
        //printf("a: %d b: %d\n", a, b);
        if(root(a) == root(b)) {
            // 同じチーム
            continue;
        }
        if(team[root(a)] == team[root(b)]) {
            // 同数なら優先度を見る
            winner = min(root(a), root(b));
            loser = max(root(a), root(b));
        } else {
            // 人数を見る
            if(team[root(a)] > team[root(b)]) {
                winner = root(a);
                loser = root(b);
            } else {
                winner = root(b);
                loser = root(a);
            }
        }
        unite(loser, winner);
        team[winner] += team[loser];
        //printf("winner %d loser %d team[%d]: %d team[%d]: %d\n",
        //        winner, loser, winner, team[winner], loser, team[loser]);
    }
    REP(i, N) {
        printf("%d\n", root(i) + 1);
    }

    return 0;
}
0