結果

問題 No.556 仁義なきサルたち
ユーザー rapurasurapurasu
提出日時 2017-09-11 21:13:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,457 bytes
コンパイル時間 1,382 ms
コンパイル使用メモリ 158,596 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 06:52:21
合計ジャッジ時間 2,288 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 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 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 17 ms
5,376 KB
testcase_17 AC 18 ms
5,376 KB
testcase_18 AC 20 ms
5,376 KB
testcase_19 AC 19 ms
5,376 KB
testcase_20 AC 21 ms
5,376 KB
testcase_21 AC 20 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;

int par1[100001];
int rank1[100001];
int size[100001];
//n要素で初期化
void init(int n){
   for(int i=0;i<n;i++){
       par1[i]=i;
       rank1[i]=0;
       size[i]=1;
   }
}
//木の根を求める
int find(int x){
    if(par1[x] ==x){
         return x;
    }else{
         return par1[x]=find(par1[x]);
    }
}
//xとyの属する集合を併合
void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y) return;
   // cout<<"size"<<size[x]<<" "<<size[y]<<endl;
    if(size[x]<size[y]){
         par1[x]=y;
         size[y]+=size[x];
         //cout<<"y"<<x<<" "<<y<<endl;
    }else if((size[x]==size[y])&&(x>y)){
         par1[x]=y;
         size[y]+=size[x];
         //cout<<"y"<<x<<" "<<y<<endl;
    }else{
         par1[y]=x;
         size[x]+=size[y];
         rank1[x]++;
        // cout<<"x"<<x<<" "<<y<<endl;
    }
}
//xとyが同じ集合に属するか否か
bool same(int x,int y){
     return find(x)==find(y);
}

int N,M;
int A[100001];
int B[100001];
int main(){
    cin>>N>>M;
    init(N+10);
    REP(i,M){
        cin>>A[i]>>B[i];
        A[i]--;B[i]--;
    }
    REP(i,M){
        unite(A[i],B[i]);
    }
    REP(i,N){
        cout<<find(i)+1<<endl;
    }
    return(0);
}
0