結果

問題 No.556 仁義なきサルたち
ユーザー 0w10w1
提出日時 2017-09-08 11:19:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 749 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 197,108 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 19:56:27
合計ジャッジ時間 2,963 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Dsu {
  vector<int> fa, size;
  Dsu(int n) {
    fa.assign(n, 0);
    size.assign(n, 0);
    for (int i = 0; i < n; ++i) {
      fa[i] = i;
      size[i] = 1;
    }
  }
  int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
  }
  void unite(int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a < b) size[a] += size[b], fa[b] = a;
    else size[b] += size[a], fa[a] = b;
  }
};

int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  Dsu *uf = new Dsu(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    uf->unite(u, v);
  }
  for (int i = 0; i < n; ++i) {
    cout << 1 + uf->find(i) << endl;
  }
  return 0;
}
0