結果
| 問題 |
No.828 全方位神童数
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-05-02 03:08:01 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 367 ms / 2,000 ms |
| コード長 | 4,307 bytes |
| コンパイル時間 | 1,035 ms |
| コンパイル使用メモリ | 82,352 KB |
| 最終ジャッジ日時 | 2025-01-07 03:20:04 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 |
ソースコード
#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;
}