結果

問題 No.556 仁義なきサルたち
ユーザー kappybarkappybar
提出日時 2020-07-09 22:38:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 1,596 ms
コンパイル使用メモリ 170,748 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 04:10:24
合計ジャッジ時間 2,737 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main(){
    int n,m;
    cin >> n >> m;
    vector<int> boss(n);
    vector<set<int>> num(n);
    iota(boss.begin(),boss.end(),0);
    rep(i,n) num[i].insert(i);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        --a;--b;
        int bossa = boss[a],bossb = boss[b];
        if(bossa==bossb) continue;
        if((int)(num[bossa].size())>(int)(num[bossb].size())){
            swap(bossa,bossb);
        }else if((int)(num[bossa].size())==(int)(num[bossb].size())){
            if(bossa<bossb) swap(bossa,bossb);
        }
        auto it = num[bossa].begin();
        while(it!=num[bossa].end()){
            boss[*it] = bossb;
            num[bossb].insert(*it);
            it = num[bossa].erase(it);
        }
    }
    rep(i,n) cout << boss[i]+1 << endl;
    return 0;
}
0