結果
問題 | No.828 全方位神童数 |
ユーザー | Pachicobue |
提出日時 | 2019-05-02 03:10:17 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 280 ms / 2,000 ms |
コード長 | 4,727 bytes |
コンパイル時間 | 994 ms |
コンパイル使用メモリ | 83,856 KB |
実行使用メモリ | 50,816 KB |
最終ジャッジ日時 | 2024-06-10 06:12:40 |
合計ジャッジ時間 | 7,556 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 10 ms
5,632 KB |
testcase_04 | AC | 4 ms
5,376 KB |
testcase_05 | AC | 9 ms
5,632 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 7 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 7 ms
5,376 KB |
testcase_13 | AC | 71 ms
20,864 KB |
testcase_14 | AC | 170 ms
39,296 KB |
testcase_15 | AC | 47 ms
16,256 KB |
testcase_16 | AC | 71 ms
21,504 KB |
testcase_17 | AC | 54 ms
17,792 KB |
testcase_18 | AC | 111 ms
29,040 KB |
testcase_19 | AC | 221 ms
47,488 KB |
testcase_20 | AC | 137 ms
33,648 KB |
testcase_21 | AC | 187 ms
41,892 KB |
testcase_22 | AC | 37 ms
12,544 KB |
testcase_23 | AC | 13 ms
6,144 KB |
testcase_24 | AC | 119 ms
27,576 KB |
testcase_25 | AC | 39 ms
13,312 KB |
testcase_26 | AC | 272 ms
46,720 KB |
testcase_27 | AC | 211 ms
40,576 KB |
testcase_28 | AC | 250 ms
46,976 KB |
testcase_29 | AC | 223 ms
44,636 KB |
testcase_30 | AC | 109 ms
27,648 KB |
testcase_31 | AC | 104 ms
26,624 KB |
testcase_32 | AC | 242 ms
46,672 KB |
testcase_33 | AC | 267 ms
50,432 KB |
testcase_34 | AC | 202 ms
40,960 KB |
testcase_35 | AC | 116 ms
29,056 KB |
testcase_36 | AC | 155 ms
34,940 KB |
testcase_37 | AC | 88 ms
24,320 KB |
testcase_38 | AC | 137 ms
32,000 KB |
testcase_39 | AC | 235 ms
46,928 KB |
testcase_40 | AC | 89 ms
24,192 KB |
testcase_41 | AC | 71 ms
20,736 KB |
testcase_42 | AC | 280 ms
50,816 KB |
testcase_43 | AC | 116 ms
47,872 KB |
testcase_44 | AC | 47 ms
20,608 KB |
testcase_45 | AC | 71 ms
29,440 KB |
ソースコード
#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; }