結果

問題 No.828 全方位神童数
ユーザー PachicobuePachicobue
提出日時 2019-05-02 03:08:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 4,307 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 83,840 KB
実行使用メモリ 50,504 KB
最終ジャッジ日時 2023-08-30 05:42:08
合計ジャッジ時間 9,369 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 11 ms
5,476 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 10 ms
5,664 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 8 ms
4,832 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 8 ms
4,868 KB
testcase_13 AC 92 ms
20,408 KB
testcase_14 AC 215 ms
39,468 KB
testcase_15 AC 63 ms
15,956 KB
testcase_16 AC 92 ms
21,508 KB
testcase_17 AC 74 ms
17,624 KB
testcase_18 AC 134 ms
28,752 KB
testcase_19 AC 266 ms
47,288 KB
testcase_20 AC 172 ms
33,472 KB
testcase_21 AC 221 ms
41,588 KB
testcase_22 AC 44 ms
12,224 KB
testcase_23 AC 13 ms
6,352 KB
testcase_24 AC 151 ms
27,396 KB
testcase_25 AC 52 ms
13,144 KB
testcase_26 AC 315 ms
46,760 KB
testcase_27 AC 259 ms
40,340 KB
testcase_28 AC 307 ms
46,832 KB
testcase_29 AC 303 ms
44,504 KB
testcase_30 AC 151 ms
27,412 KB
testcase_31 AC 139 ms
26,612 KB
testcase_32 AC 296 ms
46,348 KB
testcase_33 AC 325 ms
50,284 KB
testcase_34 AC 248 ms
40,796 KB
testcase_35 AC 158 ms
28,628 KB
testcase_36 AC 193 ms
34,540 KB
testcase_37 AC 125 ms
24,192 KB
testcase_38 AC 188 ms
31,760 KB
testcase_39 AC 292 ms
46,928 KB
testcase_40 AC 124 ms
24,332 KB
testcase_41 AC 96 ms
20,476 KB
testcase_42 AC 334 ms
50,504 KB
testcase_43 AC 119 ms
47,788 KB
testcase_44 AC 49 ms
20,540 KB
testcase_45 AC 71 ms
29,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <cassert>
#include <numeric>
//!=================================================!//
//!   .88888.                             dP        !//
//!  d8'   '88                            88        !//
//!  88        88d888b. .d8888b. 88d888b. 88d888b.  !//
//!  88   YP88 88'  '88 88'  '88 88'  '88 88'  '88  !//
//!  Y8.   .88 88       88.  .88 88.  .88 88    88  !//
//!   '88888'  dP       '88888P8 88Y888P' dP    dP  !//
//!                              88                 !//
//!                              dP                 !//
//!=================================================!//
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;
};
//!==============================================================================!//
//!  dP     dP          oo                       88888888b oo                dP  !//
//!  88     88                                   88                          88  !//
//!  88     88 88d888b. dP .d8888b. 88d888b.    a88aaaa    dP 88d888b. .d888b88  !//
//!  88     88 88'  '88 88 88'  '88 88'  '88     88        88 88'  '88 88'  '88  !//
//!  Y8.   .8P 88    88 88 88.  .88 88    88     88        88 88    88 88.  .88  !//
//!  'Y88888P' dP    dP dP '88888P' dP    dP     dP        dP dP    dP '88888P8  !//
//!==============================================================================!//
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;
};
int main()
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    using ll = long long;
    constexpr ll MOD = 1000000007;
    std::size_t N;
    std::cin >> N;
    std::vector<ll> r(N);
    for (std::size_t i = 0; i < N; i++) { std::cin >> r[i]; }
    Graph T(N);
    for (std::size_t i = 0; i < N - 1; i++) {
        std::size_t u, v;
        std::cin >> u >> v, u--, v--;
        T.addEdge(u, v, true);
    }
    UnionFind uf(N);
    std::vector<std::size_t> top(N);
    std::iota(top.begin(), top.end(), 0);
    Graph T2(N);
    for (std::size_t i = 0; i < N; i++) {
        for (const std::size_t to : T[i]) {
            if (to > i) { continue; }
            const std::size_t rep = uf.find(to);
            T2.addEdge(i, top[rep]);
            uf.unite(i, rep);
            top[uf.find(i)] = i;
        }
    }
    ll ans = 1;
    auto dfs = [&](auto&& self, const std::size_t s, const ll d) -> void {
        (ans *= (r[s] + d)) %= MOD;
        for (const std::size_t to : T2[s]) { self(self, to, d + 1); }
    };
    dfs(dfs, N - 1, 1LL);
    std::cout << ans << std::endl;
    return 0;
}
0