Submission #27455889
Source Code Expand
#include <iostream> #include <vector> #include <algorithm> using std::cout; using std::endl; using std::vector; using std::pair; class Tree { private: static const int ROOT = 0; const int mod; const vector<vector<int>>& neighbors; vector<long long> paint_ways; vector<long long> parent_ways; vector<long long> other_subtrees; /* * first find the number of painting ways for each node * given that we only consider the stuff in its subtree */ void subtree_paint(int at, int prev) { for (int n : neighbors[at]) { if (n != prev) { subtree_paint(n, at); paint_ways[at] *= paint_ways[n] + 1; } } for (int n : neighbors[at]) { if (n != prev) { other_subtrees[n] = paint_ways[at] / (paint_ways[n] + 1); other_subtrees[n] %= mod; } } paint_ways[at] %= mod; } // ok now let's actually consider literally all the other nodes void total_paint(int at, int prev) { if (at != ROOT) { long long prev_ways = parent_ways[prev]; long long other_ways = other_subtrees[at]; long long new_ways = prev_ways * other_ways % mod; paint_ways[at] = paint_ways[at] * (new_ways + 1) % mod; parent_ways[at] = (new_ways + 1) % mod; } for (int n : neighbors[at]) { if (n != prev) { total_paint(n, at); } } } public: Tree(const vector<vector<int>>& neighbors, int mod = 1e9 + 7) : neighbors(neighbors), mod(mod), paint_ways(neighbors.size(), 1), parent_ways(neighbors.size()), other_subtrees(neighbors.size(), 1) { subtree_paint(ROOT, ROOT); parent_ways[0] = 1; total_paint(ROOT, ROOT); } int get_paint_ways(int start) { return (int) paint_ways[start]; } }; /** * https://atcoder.jp/contests/dp/tasks/dp_v * 4 100 * 1 2 * 1 3 * 1 4 should output 8, 5, 5, and 5, each on a new line */ int main() { int node_num; int mod; std::cin >> node_num >> mod; vector<vector<int>> neighbors(node_num); for (int e = 0; e < node_num - 1; e++) { int n1, n2; std::cin >> n1 >> n2; neighbors[--n1].push_back(--n2); neighbors[n2].push_back(n1); } Tree tree(neighbors, mod); for (int n = 0; n < node_num; n++) { cout << tree.get_paint_ways(n) << endl; } }
Submission Info
Submission Time | |
---|---|
Task | V - Subtree |
User | SansPapyrus683 |
Language | C++ (GCC 9.2.1) |
Score | 0 |
Code Size | 2854 Byte |
Status | WA |
Exec Time | 240 ms |
Memory | 16716 KiB |
Compile Error
./Main.cpp: In constructor ‘Tree::Tree(const std::vector<std::vector<int> >&, int)’: ./Main.cpp:14:36: warning: ‘Tree::neighbors’ will be initialized after [-Wreorder] 14 | const vector<vector<int>>& neighbors; | ^~~~~~~~~ ./Main.cpp:13:19: warning: ‘const int Tree::mod’ [-Wreorder] 13 | const int mod; | ^~~ ./Main.cpp:55:9: warning: when initialized here [-Wreorder] 55 | Tree(const vector<vector<int>>& neighbors, int mod = 1e9 + 7) | ^~~~
Judge Result
Set Name | All | ||||
---|---|---|---|---|---|
Score / Max Score | 0 / 100 | ||||
Status |
|
Set Name | Test Cases |
---|---|
All | 0_00, 0_01, 0_02, 0_03, 1_00, 1_01, 1_02, 1_03, 1_04, 1_05, 1_06, 1_07, 1_08, 1_09, 1_10, 1_11, 1_12, 1_13, 1_14, 1_15, 1_16, 1_17, 1_18, 1_19, 1_20, 1_21, 1_22, 1_23, 1_24, 1_25, 1_26, 1_27, 1_28, 1_29, 1_30, 1_31 |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00 | AC | 7 ms | 3480 KiB |
0_01 | AC | 2 ms | 3488 KiB |
0_02 | AC | 2 ms | 3540 KiB |
0_03 | AC | 2 ms | 3620 KiB |
1_00 | AC | 2 ms | 3500 KiB |
1_01 | AC | 2 ms | 3412 KiB |
1_02 | AC | 237 ms | 15692 KiB |
1_03 | AC | 238 ms | 15308 KiB |
1_04 | WA | 215 ms | 11368 KiB |
1_05 | WA | 215 ms | 11252 KiB |
1_06 | AC | 218 ms | 11352 KiB |
1_07 | WA | 218 ms | 11196 KiB |
1_08 | WA | 219 ms | 11436 KiB |
1_09 | WA | 223 ms | 11208 KiB |
1_10 | WA | 219 ms | 11280 KiB |
1_11 | WA | 221 ms | 11468 KiB |
1_12 | WA | 221 ms | 11344 KiB |
1_13 | WA | 220 ms | 11280 KiB |
1_14 | AC | 223 ms | 11268 KiB |
1_15 | WA | 225 ms | 11236 KiB |
1_16 | AC | 219 ms | 11172 KiB |
1_17 | WA | 228 ms | 11068 KiB |
1_18 | AC | 228 ms | 10956 KiB |
1_19 | WA | 240 ms | 11028 KiB |
1_20 | AC | 232 ms | 10752 KiB |
1_21 | WA | 240 ms | 11084 KiB |
1_22 | AC | 233 ms | 11120 KiB |
1_23 | WA | 236 ms | 11084 KiB |
1_24 | AC | 229 ms | 10908 KiB |
1_25 | WA | 230 ms | 10960 KiB |
1_26 | AC | 223 ms | 11440 KiB |
1_27 | WA | 225 ms | 11112 KiB |
1_28 | AC | 229 ms | 12868 KiB |
1_29 | WA | 234 ms | 12436 KiB |
1_30 | AC | 237 ms | 14548 KiB |
1_31 | AC | 238 ms | 16716 KiB |