結果

問題 No.828 全方位神童数
ユーザー PachicobuePachicobue
提出日時 2019-04-11 19:52:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 2,264 bytes
コンパイル時間 1,133 ms
コンパイル使用メモリ 83,044 KB
実行使用メモリ 30,052 KB
最終ジャッジ日時 2023-08-30 05:04:54
合計ジャッジ時間 8,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 8 ms
4,452 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 8 ms
4,384 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 54 ms
12,552 KB
testcase_14 AC 129 ms
22,792 KB
testcase_15 AC 40 ms
10,236 KB
testcase_16 AC 56 ms
13,036 KB
testcase_17 AC 42 ms
10,988 KB
testcase_18 AC 86 ms
17,036 KB
testcase_19 AC 163 ms
27,088 KB
testcase_20 AC 102 ms
19,500 KB
testcase_21 AC 140 ms
23,976 KB
testcase_22 AC 26 ms
8,016 KB
testcase_23 AC 9 ms
4,704 KB
testcase_24 AC 87 ms
16,268 KB
testcase_25 AC 31 ms
8,656 KB
testcase_26 AC 181 ms
26,628 KB
testcase_27 AC 147 ms
23,504 KB
testcase_28 AC 177 ms
27,004 KB
testcase_29 AC 162 ms
25,376 KB
testcase_30 AC 81 ms
16,260 KB
testcase_31 AC 82 ms
16,044 KB
testcase_32 AC 172 ms
26,680 KB
testcase_33 AC 187 ms
28,724 KB
testcase_34 AC 142 ms
23,524 KB
testcase_35 AC 88 ms
17,180 KB
testcase_36 AC 112 ms
20,152 KB
testcase_37 AC 67 ms
14,408 KB
testcase_38 AC 95 ms
18,776 KB
testcase_39 AC 176 ms
26,948 KB
testcase_40 AC 72 ms
14,348 KB
testcase_41 AC 58 ms
12,516 KB
testcase_42 AC 190 ms
28,928 KB
testcase_43 AC 83 ms
30,052 KB
testcase_44 AC 34 ms
13,628 KB
testcase_45 AC 51 ms
18,952 KB
権限があれば一括ダウンロードができます

ソースコード

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