結果

問題 No.828 全方位神童数
ユーザー PachicobuePachicobue
提出日時 2019-05-02 03:10:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 4,727 bytes
コンパイル時間 1,476 ms
コンパイル使用メモリ 83,452 KB
実行使用メモリ 50,724 KB
最終ジャッジ日時 2023-08-30 05:42:23
合計ジャッジ時間 9,054 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 10 ms
5,764 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 10 ms
5,488 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 7 ms
4,700 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 8 ms
4,972 KB
testcase_13 AC 76 ms
20,428 KB
testcase_14 AC 190 ms
39,284 KB
testcase_15 AC 57 ms
16,068 KB
testcase_16 AC 83 ms
21,332 KB
testcase_17 AC 62 ms
17,628 KB
testcase_18 AC 125 ms
28,872 KB
testcase_19 AC 238 ms
47,356 KB
testcase_20 AC 152 ms
33,596 KB
testcase_21 AC 200 ms
41,852 KB
testcase_22 AC 38 ms
12,244 KB
testcase_23 AC 13 ms
6,272 KB
testcase_24 AC 135 ms
27,344 KB
testcase_25 AC 44 ms
13,096 KB
testcase_26 AC 272 ms
46,568 KB
testcase_27 AC 210 ms
40,360 KB
testcase_28 AC 265 ms
46,816 KB
testcase_29 AC 240 ms
44,600 KB
testcase_30 AC 118 ms
27,336 KB
testcase_31 AC 121 ms
26,584 KB
testcase_32 AC 252 ms
46,336 KB
testcase_33 AC 285 ms
50,228 KB
testcase_34 AC 225 ms
40,952 KB
testcase_35 AC 141 ms
28,716 KB
testcase_36 AC 178 ms
34,624 KB
testcase_37 AC 100 ms
24,168 KB
testcase_38 AC 160 ms
31,688 KB
testcase_39 AC 262 ms
46,832 KB
testcase_40 AC 105 ms
23,872 KB
testcase_41 AC 84 ms
20,420 KB
testcase_42 AC 321 ms
50,724 KB
testcase_43 AC 118 ms
47,668 KB
testcase_44 AC 48 ms
20,532 KB
testcase_45 AC 70 ms
29,192 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;
};
//!============================================!//
//!    8888ba.88ba             oo              !//
//!    88  '8b  '8b                            !//
//!    88   88   88  .d8888b.  dP  88d888b.    !//
//!    88   88   88  88'  '88  88  88'  '88    !//
//!    88   88   88  88.  .88  88  88    88    !//
//!    dP   dP   dP  '88888P8  dP  dP    dP    !//
//!============================================!//
int main()
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    using ull = unsigned long long;
    constexpr ull MOD = 1000000007;
    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]; }
    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;
        }
    }
    ull ans = 1;
    auto dfs = [&](auto&& self, const std::size_t s, const ull d) -> void {
        (ans *= (r[s] + d)) %= MOD;
        for (const std::size_t to : T2[s]) { self(self, to, d + 1); }
    };
    dfs(dfs, N - 1, 1);
    std::cout << ans << std::endl;
    return 0;
}
0