結果

問題 No.556 仁義なきサルたち
ユーザー tsunenarazu
提出日時 2017-08-12 00:11:16
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,231 bytes
コンパイル時間 656 ms
コンパイル使用メモリ 69,456 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-12 22:28:08
合計ジャッジ時間 1,532 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct tree tree;

struct tree {
  int key;
  int rank;
  struct tree *parent;
};


void make_set(tree *x, int key){
  x->parent = x;
  x->key = key;
  x->rank = 1;
}

tree *find_set(tree *x){
  if(x != x->parent)
    x->parent = find_set(x->parent);
  return x->parent;
}

void link(tree *x, tree *y){
  if( find_set(x)->rank > find_set(y)->rank ||
   ( find_set(x)->rank == find_set(y)->rank && find_set(x)->key < find_set(y)->key)  ){
    find_set(x)->rank += find_set(y)->rank;
    y->parent = x;
  }
  else{
    find_set(y)->rank += find_set(x)->rank;
    x->parent = y;
  }
}

void union_set(tree *x, tree *y){
  link(find_set(x),find_set(y));
}

int main()
{
  int n,m;  cin >> n >> m;
  tree *t[10001];
  for(int i=0;i<n;i++){
    t[i] = (tree*)malloc(sizeof(tree));
    make_set(t[i],i);
  }

  int a,b;
  for(int i=0;i<m;i++){
    cin >> a >> b;
    a--;  b--;
    if(find_set(t[a]) != find_set(t[b]))
      union_set(t[a],t[b]);
  }


  int ans[10001]={};
  for(int i=0;i<n;i++)
    cout << find_set(t[i])->key + 1 << endl;


  return 0;
}
0