結果

問題 No.1451 集団登校
ユーザー KoDKoD
提出日時 2021-03-31 14:42:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 5,381 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 206,076 KB
実行使用メモリ 5,088 KB
最終ジャッジ日時 2023-08-21 09:17:15
合計ジャッジ時間 4,201 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 21 ms
4,548 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 11 ms
4,884 KB
testcase_11 AC 7 ms
5,088 KB
testcase_12 AC 11 ms
4,628 KB
testcase_13 AC 20 ms
4,952 KB
testcase_14 AC 27 ms
4,628 KB
testcase_15 AC 13 ms
4,380 KB
testcase_16 AC 18 ms
4,584 KB
testcase_17 AC 18 ms
4,380 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 28 ms
4,696 KB
testcase_21 AC 11 ms
4,384 KB
testcase_22 AC 7 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 11 ms
4,980 KB
testcase_25 AC 12 ms
4,380 KB
testcase_26 AC 18 ms
4,748 KB
testcase_27 AC 21 ms
4,376 KB
testcase_28 AC 8 ms
4,376 KB
testcase_29 AC 13 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept: itr(pos) { }
        constexpr void operator ++ () noexcept { ++itr; }
        constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator * () const noexcept { return itr; }
    };
    const Iter first, last;
public:
    explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { }
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T>
constexpr T rem_euclid(T value, const T& mod) {
    return (value %= mod) >= 0 ? value : value + mod;
}

namespace static_modint_internal {

constexpr u32 totient(u32 x) noexcept {
    u32 ret = x;
    for (u32 i = 2; i * i <= x; ++i) {
        if (x % i == 0) {
            ret /= i; ret *= i - 1;
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) { ret /= x; ret *= x - 1; }
    return ret;
}

}

template <u32 MOD, std::enable_if_t<((u32) 1 <= MOD and MOD <= ((u32) 1 << 31))>* = nullptr>
class StaticModint {
    using Mint = StaticModint;
    static inline constexpr u32 PHI = static_modint_internal::totient(MOD);
    u32 v;
public:
    static constexpr u32 mod() noexcept { return MOD; }

    template <class T, std::enable_if_t<std::is_signed_v<T> and std::is_integral_v<T>>* = nullptr>
    static constexpr T normalize(const T x) noexcept { return rem_euclid<std::common_type_t<T, i64>>(x, MOD); }
    template <class T, std::enable_if_t<std::is_unsigned_v<T> and std::is_integral_v<T>>* = nullptr>
    static constexpr T normalize(const T x) noexcept { return x % MOD; }

    constexpr StaticModint() noexcept: v(0) { }
    template <class T>
    explicit constexpr StaticModint(const T x) noexcept: v(normalize(x)) { }
    template <class T>
    static constexpr Mint raw(const T x) noexcept {
        Mint ret;
        ret.v = x;
        return ret;
    }

    constexpr u32 get() const noexcept { return v; }
    constexpr Mint neg() const noexcept { return raw(v == 0 ? 0 : MOD - v); }
    constexpr Mint operator - () const noexcept { return neg(); }
    constexpr Mint inv() const noexcept { return pow(PHI - 1); }
    constexpr Mint operator ~ () const noexcept { return inv(); }
    constexpr Mint pow(u64 exp) const noexcept {
        Mint ret(1), mult(*this);
        for (; exp > 0; exp >>= 1) {
            if (exp & 1) ret *= mult;
            mult *= mult;
        }
        return ret;
    }

    constexpr Mint operator + (const Mint& rhs) const noexcept { return Mint(*this) += rhs; }
    constexpr Mint& operator += (const Mint& rhs) noexcept {
        if ((v += rhs.v) >= MOD) v -= MOD;
        return *this;
    }
    constexpr Mint operator - (const Mint& rhs) const noexcept { return Mint(*this) -= rhs; }
    constexpr Mint& operator -= (const Mint& rhs) noexcept {
        if (v < rhs.v) v += MOD;
        v -= rhs.v;
        return *this;
    }
    constexpr Mint operator * (const Mint& rhs) const noexcept { return Mint(*this) *= rhs; }
    constexpr Mint& operator *= (const Mint& rhs) noexcept {
        v = (u64) v * rhs.v % MOD;
        return *this;
    }
    constexpr Mint operator / (const Mint& rhs) const noexcept { return Mint(*this) /= rhs; }
    constexpr Mint& operator /= (const Mint& rhs) noexcept { 
        return *this *= rhs.inv();
    }
    constexpr bool operator == (const Mint& rhs) const noexcept { return v == rhs.v; }
    constexpr bool operator != (const Mint& rhs) const noexcept { return v != rhs.v; }
    friend std::ostream& operator << (std::ostream& stream, const Mint& rhs) { 
        return stream << rhs.v;
    }
};

template <class T>
using Vec = std::vector<T>;

using Fp = StaticModint<1000000007>;
constexpr auto INV2 = Fp(1) / Fp(2);

void G_main() {
    usize N, M;
    std::cin >> N >> M;
    Vec<isize> parent(N, -1);
    Vec<Fp> coeff(N);
    const auto find = [&](isize u) {
        while (parent[u] >= 0) {
            u = parent[u];
        }
        return u;
    };
    while (M--) {
        usize a, b;
        std::cin >> a >> b;
        a = find(a - 1);
        b = find(b - 1);
        if (a == b) {
            continue;
        }
        if (parent[a] > parent[b]) {
            std::swap(a, b);
        }
        if (parent[a] == parent[b]) {
            coeff[a] = INV2;
            coeff[b] = INV2;
            const auto n = parent.size();
            parent.push_back(parent[a] + parent[b]);
            coeff.push_back(Fp(0));
            parent[a] = n;
            parent[b] = n;
        }
        else {
            coeff[b] = Fp(0);
            parent[a] += parent[b];
            parent[b] = a;
        }
    }
    for (const auto i: rep(0, N)) {
        usize u = i;
        Fp ans(1);
        while (parent[u] >= 0) {
            ans *= coeff[u];
            u = parent[u];
        }
        std::cout << ans << '\n';
    }
    return;
}

int main() {
    std::ios_base::sync_with_stdio(false);
#ifndef LOCAL
    std::cin.tie(nullptr);
#endif
    G_main();
    return 0;
}
0