結果

問題 No.1451 集団登校
ユーザー こばゆこばゆ
提出日時 2021-03-31 15:30:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,893 bytes
コンパイル時間 1,868 ms
コンパイル使用メモリ 75,724 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-08 18:08:42
合計ジャッジ時間 4,609 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

// Union-Find
struct UnionFind {
    vector<int> par, siz;

    // 初期化
    UnionFind(int n) : par(n, -1) , siz(n, 1) { }

    // 根を求める
    int root(int x) {
        if (par[x] == -1) return x; // x が根の場合は x を返す
        else return par[x] = root(par[x]);
    }

    // x と y が同じグループに属するかどうか (根が一致するかどうか)
    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    // x を含むグループと y を含むグループとを併合する
    bool unite(int x, int y) {
        // x, y をそれぞれ根まで移動する
        x = root(x);
        y = root(y);

        // すでに同じグループのときは何もしない
        if (x == y) return false;

        // union by size (y 側のサイズが小さくなるようにする)
        if (siz[x] < siz[y]) swap(x, y);

        // y を x の子とする
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }

    // x を含むグループのサイズ
    int size(int x) {
        return siz[root(x)];
    }
};
int main(int argc, char const *argv[]) {
  int n,m;
  std::cin >> n>>m;
  std::vector<int> x(n,1);
  std::vector<bool> y(n,true);
  UnionFind uf(n);
  for (size_t i = 0; i < m; i++) {
    int a,b;
    std::cin >> a>>b;
    a--;
    b--;
    if (uf.size(a)>uf.size(b)) {
      y.at(uf.root(b))=false;
      uf.unite(a,b);
    } else if (uf.size(a)<uf.size(b)) {
      y.at(uf.root(a))=false;
      uf.unite(a,b);
    } else {
      uf.unite(a,b);
      for (size_t j = 0; j < n; j++) {
        if (uf.issame(a,j)) {
          x.at(j)*=2;
        }
      }
    }
  }
  for (size_t i = 0; i < n; i++) {
    if (y.at(i)) {
      std::cout << (1000000008)/x.at(i) << '\n';
    } else{
      std::cout << 0 << '\n';
    }
  }
  return 0;
}
0