結果

問題 No.828 全方位神童数
ユーザー Pachicobue
提出日時 2019-04-11 19:52:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 2,264 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 82,032 KB
最終ジャッジ日時 2025-01-07 01:38:41
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <cassert>
#include <numeric>
using ull = unsigned long long;
constexpr ull MOD = 1000000007;
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)]; }
    void debugPrint() const
    {
        std::cerr << "[";
        for (std::size_t i = 0; i < parent.size(); i++) { std::cerr << parent[i] << (i + 1 == parent.size() ? "" : ","); }
        std::cerr << "]\n";
    }

private:
    const std::size_t V;
    std::vector<std::size_t> parent, size;
};
int main()
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::size_t N;
    std::cin >> N;
    std::vector<ull> R(N);
    for (std::size_t i = 0; i < N; i++) { std::cin >> R[i]; }
    std::vector<std::vector<std::size_t>> G(N);
    for (std::size_t i = 0; i < N - 1; i++) {
        std::size_t u, v;
        std::cin >> u >> v, u--, v--;
        G[u].push_back(v), G[v].push_back(u);
    }
    UnionFind uf(N);
    std::vector<std::size_t> max(N);
    std::vector<std::vector<std::size_t>> G2(N);
    for (std::size_t i = 0; i < N; i++) {
        for (const std::size_t to : G[i]) {
            if (to > i) { continue; }
            const std::size_t c = max[uf.find(to)];
            G2[i].push_back(c), uf.unite(i, c);
        }
        max[uf.find(i)] = i;
    }
    ull ans = 1;
    auto dfs = [&](auto&& self, const std::size_t s, const std::size_t d) -> void {
        (ans *= (R[s] + d) % MOD) %= MOD;
        for (const std::size_t to : G2[s]) { self(self, to, d + 1); }
    };
    dfs(dfs, N - 1, 1);
    std::cout << ans << std::endl;
    return 0;
}
0