結果

問題 No.1451 集団登校
ユーザー hitonanode
提出日時 2021-05-13 16:13:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 2,419 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 142,632 KB
最終ジャッジ日時 2025-01-21 10:35:14
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using pint = pair<int, int>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)

#include <atcoder/modint>
using mint = atcoder::modint1000000007;

// UnionFind Tree (0-indexed), based on size of each disjoint set
struct UnionFind {
    std::vector<int> par, cou;
    UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); }
    int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }
    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return false;
        if (cou[x] < cou[y]) std::swap(x, y);
        par[y] = x, cou[x] += cou[y];
        return true;
    }
    int count(int x) { return cou[find(x)]; }
    bool same(int x, int y) { return find(x) == find(y); }
};


int main() {
    int N, M;
    cin >> N >> M;
    vector<pint> to(N);
    UnionFind uf(N);
    vector<int> fi(N);
    vector<int> sz(N, 1);
    iota(fi.begin(), fi.end(), 0);
    sz.reserve(N + M);
    fi.reserve(N + M);
    while (M--) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        a = uf.find(a), b = uf.find(b);
        if (uf.unite(a, b)) {
            to.emplace_back(fi[a], fi[b]);
            sz.push_back(uf.count(a));
            fi[uf.find(a)] = int(to.size()) - 1;
        }
    }
    mint inv2 = 500000004;
    vector<mint> prob(sz.size(), 1);
    IFOR(i, N, sz.size()) {
        auto [a, b] = to[i];
        if (sz[a] < sz[b]) swap(a, b);
        if (sz[a] == sz[b]) prob[a] = prob[b] = prob[i] * inv2;
        else prob[a] = prob[i], prob[b] = 0;
    }
    REP(i, N) cout << prob[i].val() << '\n';
}
0