結果

問題 No.828 全方位神童数
ユーザー PachicobuePachicobue
提出日時 2019-04-23 16:50:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 505 ms / 2,000 ms
コード長 7,394 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 89,084 KB
実行使用メモリ 52,140 KB
最終ジャッジ日時 2023-08-30 05:42:36
合計ジャッジ時間 12,307 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 17 ms
5,816 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 16 ms
5,372 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 11 ms
4,960 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 12 ms
4,896 KB
testcase_13 AC 142 ms
21,012 KB
testcase_14 AC 330 ms
40,392 KB
testcase_15 AC 102 ms
16,452 KB
testcase_16 AC 148 ms
22,028 KB
testcase_17 AC 114 ms
18,048 KB
testcase_18 AC 220 ms
29,584 KB
testcase_19 AC 411 ms
48,968 KB
testcase_20 AC 262 ms
34,336 KB
testcase_21 AC 351 ms
43,216 KB
testcase_22 AC 69 ms
12,772 KB
testcase_23 AC 21 ms
6,236 KB
testcase_24 AC 212 ms
28,228 KB
testcase_25 AC 82 ms
13,672 KB
testcase_26 AC 445 ms
47,876 KB
testcase_27 AC 383 ms
41,676 KB
testcase_28 AC 454 ms
48,316 KB
testcase_29 AC 427 ms
45,752 KB
testcase_30 AC 240 ms
28,436 KB
testcase_31 AC 218 ms
27,396 KB
testcase_32 AC 464 ms
47,680 KB
testcase_33 AC 502 ms
51,872 KB
testcase_34 AC 383 ms
41,992 KB
testcase_35 AC 256 ms
29,572 KB
testcase_36 AC 319 ms
35,776 KB
testcase_37 AC 193 ms
24,716 KB
testcase_38 AC 273 ms
32,948 KB
testcase_39 AC 454 ms
48,252 KB
testcase_40 AC 194 ms
24,704 KB
testcase_41 AC 152 ms
20,952 KB
testcase_42 AC 505 ms
52,140 KB
testcase_43 AC 218 ms
48,272 KB
testcase_44 AC 85 ms
20,680 KB
testcase_45 AC 130 ms
29,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <utility>
class UnionFind
{
public:
    UnionFind(const std::size_t v) : V(v), parent(v), size(v, 1) { std::iota(parent.begin(), parent.end(), 0); }
    std::size_t find(const std::size_t a) { return assert(a < V), parent[a] == a ? a : parent[a] = find(parent[a]); }
    bool same(const std::size_t a, const std::size_t b)
    {
        assert(a < V), assert(b < V);
        return find(a) == find(b);
    }
    void unite(std::size_t a, std::size_t b)
    {
        assert(a < V), assert(b < V);
        a = find(a), b = find(b);
        if (a == b) { return; }
        if (size[a] < size[b]) { std::swap(a, b); }
        size[a] += size[b], parent[b] = a;
    }
    std::size_t getSize(const std::size_t a) { return assert(a < V), size[find(a)]; }
    friend std::ostream& operator<<(std::ostream& os, const UnionFind& uf)
    {
        os << "[";
        for (std::size_t i = 0; i < uf.parent.size(); i++) { os << uf.parent[i] << (i + 1 == uf.parent.size() ? "" : ","); }
        return (os << "]\n");
    }

private:
    const std::size_t V;
    std::vector<std::size_t> parent, size;
};
template <typename T>
constexpr std::pair<T, T> extgcd(const T a, const T b)
{
    if (b == 0) { return std::pair<T, T>{1, 0}; }
    const auto p = extgcd(b, a % b);
    return {p.second, p.first - p.second * (a / b)};
}
template <typename T>
constexpr T inverse(const T a, const T mod) { return (mod + extgcd((mod + a % mod) % mod, mod).first % mod) % mod; }
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template <uint mod>
class ModInt
{
private:
    uint v;
    static uint norm(const uint& x) { return x < mod ? x : x - mod; }
    static ModInt make(const uint& x)
    {
        ModInt m;
        return m.v = x, m;
    }
    static ModInt power(ModInt x, ll n)
    {
        ModInt ans = 1;
        for (; n; n >>= 1, x *= x) {
            if (n & 1) { ans *= x; }
        }
        return ans;
    }
    static ModInt inv(const ModInt& x) { return ModInt{inverse((ll)x.v, (ll)mod)}; }

public:
    ModInt() : v{0} {}
    ModInt(const ll val) : v{norm(uint(val % (ll)mod + (ll)mod))} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
    explicit operator bool() const { return v != 0; }
    ModInt<mod>& operator=(const ModInt<mod>& n) { return v = n(), (*this); }
    ModInt<mod>& operator=(const ll val) { return v = norm(uint(val % (ll)mod + (ll)mod)), (*this); }
    ModInt<mod> operator+() const { return *this; }
    ModInt<mod> operator-() const { return make(norm(mod - v)); }
    ModInt<mod> operator+(const ModInt<mod>& val) const { return make(norm(v + val())); }
    ModInt<mod> operator-(const ModInt<mod>& val) const { return make(norm(v + mod - val())); }
    ModInt<mod> operator*(const ModInt<mod>& val) const { return make((uint)((ll)v * val() % (ll)mod)); }
    ModInt<mod> operator/(const ModInt<mod>& val) const { return *this * inv(val()); }
    ModInt<mod>& operator+=(const ModInt<mod>& val) { return *this = *this + val; }
    ModInt<mod>& operator-=(const ModInt<mod>& val) { return *this = *this - val; }
    ModInt<mod>& operator*=(const ModInt<mod>& val) { return *this = *this * val; }
    ModInt<mod>& operator/=(const ModInt<mod>& val) { return *this = *this / val; }
    ModInt<mod> operator+(const ll val) const { return ModInt{v + val}; }
    ModInt<mod> operator-(const ll val) const { return ModInt{v - val}; }
    ModInt<mod> operator*(const ll val) const { return ModInt{(ll)v * (val % mod)}; }
    ModInt<mod> operator/(const ll val) const { return ModInt{(ll)v * inv(val)}; }
    template <typename I>
    ModInt<mod> operator^(const I n) const { return power(v, n); }
    ModInt<mod>& operator+=(const ll val) { return *this = *this + val; }
    ModInt<mod>& operator-=(const ll val) { return *this = *this - val; }
    ModInt<mod>& operator*=(const ll val) { return *this = *this * val; }
    ModInt<mod>& operator/=(const ll val) { return *this = *this / val; }
    template <typename I>
    ModInt<mod>& operator^=(const I n) { return (*this) = ((*this) ^ n); }
    bool operator==(const ModInt<mod>& val) const { return v == val.v; }
    bool operator!=(const ModInt<mod>& val) const { return not(*this == val); }
    bool operator==(const ll val) const { return v == norm(uint((ll)mod + val % (ll)mod)); }
    bool operator!=(const ll val) const { return not(*this == val); }
    uint operator()() const { return v; }
};
template <uint mod>
inline ModInt<mod> operator+(const ll val, const ModInt<mod>& n) { return n + val; }
template <uint mod>
inline ModInt<mod> operator-(const ll val, const ModInt<mod>& n) { return ModInt<mod>{val - (ll)n()}; }
template <uint mod>
inline ModInt<mod> operator*(const ll val, const ModInt<mod>& n) { return n * val; }
template <uint mod>
inline ModInt<mod> operator/(const ll val, const ModInt<mod>& n) { return ModInt<mod>(val) / n; }
template <uint mod>
inline bool operator==(const ll val, const ModInt<mod>& n) { return n == val; }
template <uint mod>
inline bool operator!=(const ll val, const ModInt<mod>& n) { return not(val == n); }
template <uint mod>
inline std::istream& operator>>(std::istream& is, ModInt<mod>& n)
{
    uint v;
    return is >> v, n = v, is;
}
template <uint mod>
std::ostream& operator<<(std::ostream& os, const ModInt<mod>& n) { return (os << n()); }
struct Graph
{
    Graph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {}
    void addEdge(const std::size_t from, const std::size_t to, const bool bi = false)
    {
        assert(from < V), assert(to < V);
        edge[from].push_back(to), rev_edge[to].push_back(from);
        if (bi) { addEdge(to, from, false); }
    }
    const std::vector<std::size_t>& operator[](const std::size_t i) const { return assert(i < V), edge[i]; }
    friend std::ostream& operator<<(std::ostream& os, const Graph& g)
    {
        os << "[\n";
        for (std::size_t i = 0; i < g.V; i++) {
            for (const std::size_t to : g.edge[i]) { os << i << "->" << to << "\n"; }
        }
        return (os << "]\n");
    }
    std::size_t V;
    std::vector<std::vector<std::size_t>> edge, rev_edge;
};
constexpr uint MOD = 1000000007;
using mint = ModInt<MOD>;
int main()
{
    int N;
    std::cin >> N;
    assert(1 <= N and N <= 200000);
    std::vector<uint> r(N);
    for (int i = 0; i < N; i++) {
        std::cin >> r[i];
        assert(r[i] < MOD);
    }
    UnionFind uf(N);
    Graph G(N);
    for (int i = 0; i < N - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        assert(1 <= u and u <= N);
        assert(1 <= v and v <= N);
        u--, v--;
        assert(not uf.same(u, v));
        uf.unite(u, v);
        G.addEdge(u, v, true);
    }
    UnionFind uf2(N);
    Graph T(N);
    std::vector<int> max(N);
    std::iota(max.begin(), max.end(), 0);
    for (int i = 0; i < N; i++) {
        for (const int to : G[i]) {
            if (to < i) {
                const int top = max[uf2.find(to)];
                T.addEdge(i, top);
                uf2.unite(i, top);
                max[uf2.find(i)] = i;
            }
        }
    }
    mint ans = 1;
    auto dfs = [&](auto&& self, const int s, const uint d) -> void {
        ans *= (r[s] + d);
        for (const int to : T[s]) { self(self, to, d + 1); }
    };
    dfs(dfs, N - 1, 1);
    std::cout << ans << std::endl;
    return 0;
}
0