結果

問題 No.556 仁義なきサルたち
ユーザー ikdikd
提出日時 2017-08-11 23:47:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 875 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 63,696 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 23:51:31
合計ジャッジ時間 1,328 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>

using namespace std;
#define repeat(i,n) for(int i=0;i<(n);i++)

struct UF{

   int n;
   vector<int> pr, sz;
   UF(int n_){
      n=n_;
      pr.resize(n);
      repeat(i, n) pr[i]=i;
      sz.resize(n, 1);
   }

   int fi(int x){
      if(x==pr[x]) return pr[x];
      else return pr[x]=fi(pr[x]);
   }

   void mg(int x, int y){
      x=fi(x);
      y=fi(y);
      if(x==y) return;
      if(sz[x]==sz[y]){
         sz[min(x, y)]+=sz[max(x, y)];
         pr[max(x, y)]=min(x, y);
      }else{
         if(sz[x]<sz[y]) swap(x, y); // x>y
         sz[x]+=sz[y];
         pr[y]=x;
      }
   }

};

int main(){

   int n, m;
   cin>> n>> m;
   vector<int> a(m), b(m);
   repeat(i, m) cin>> a[i]>> b[i];

   UF uf(n);
   repeat(i, m){
      uf.mg(a[i]-1, b[i]-1);
   }

   repeat(i, n){
      cout<< uf.fi(i)+1<< endl;
   }

   return 0;
}
0