結果

問題 No.1451 集団登校
ユーザー 👑 hitonanodehitonanode
提出日時 2021-05-13 16:13:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 2,419 bytes
コンパイル時間 1,725 ms
コンパイル使用メモリ 148,364 KB
実行使用メモリ 6,728 KB
最終ジャッジ日時 2023-10-25 09:55:35
合計ジャッジ時間 3,648 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 21 ms
6,004 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 14 ms
6,128 KB
testcase_11 AC 9 ms
6,244 KB
testcase_12 AC 14 ms
5,884 KB
testcase_13 AC 23 ms
6,728 KB
testcase_14 AC 30 ms
6,556 KB
testcase_15 AC 14 ms
5,164 KB
testcase_16 AC 22 ms
6,320 KB
testcase_17 AC 17 ms
4,348 KB
testcase_18 AC 10 ms
4,480 KB
testcase_19 AC 7 ms
5,044 KB
testcase_20 AC 31 ms
6,608 KB
testcase_21 AC 12 ms
4,348 KB
testcase_22 AC 8 ms
4,348 KB
testcase_23 AC 4 ms
4,348 KB
testcase_24 AC 14 ms
6,080 KB
testcase_25 AC 14 ms
5,024 KB
testcase_26 AC 21 ms
6,344 KB
testcase_27 AC 24 ms
4,832 KB
testcase_28 AC 9 ms
4,348 KB
testcase_29 AC 15 ms
4,972 KB
権限があれば一括ダウンロードができます

ソースコード

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