結果

問題 No.556 仁義なきサルたち
ユーザー rapurasurapurasu
提出日時 2017-09-11 21:05:49
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,416 bytes
コンパイル時間 1,087 ms
コンパイル使用メモリ 144,324 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-07 13:04:22
合計ジャッジ時間 2,169 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 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 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 10 ms
4,384 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 19 ms
4,380 KB
testcase_19 AC 18 ms
4,380 KB
testcase_20 AC 20 ms
4,376 KB
testcase_21 AC 20 ms
4,380 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[10001];
int rank1[10001];
int size[10001];
//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 main(){
    cin>>N>>M;
    init(N+10);
    REP(i,M){
        int a;
        int b;
        cin>>a>>b;
        a--;b--;
        unite(a,b);
    }
    REP(i,N){
        cout<<find(i)+1<<endl;
    }
    return(0);
}
0