Task: | Tree matching |
Sender: | lippinj1 |
Submission time: | 2016-09-24 16:02:26 +0300 |
Language: | C++ |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.05 s | details |
#2 | ACCEPTED | 0.68 s | details |
#3 | ACCEPTED | 0.69 s | details |
#4 | ACCEPTED | 0.69 s | details |
#5 | ACCEPTED | 0.70 s | details |
#6 | ACCEPTED | 0.68 s | details |
#7 | ACCEPTED | 0.70 s | details |
Code
#include <iostream> #include <string> #include <vector> #include <cstdint> const unsigned long long N = 1000000007; std::vector<long long> with; std::vector<long long> without; std::vector<std::pair<int, int>> children; void figure_out(int i) { int l = children[i].first; int r = children[i].second; if (l == -1) { if (r == -1) { with[i] = 1; without[i] = 1; } else { figure_out(r); with[i] = without[r]; without[i] = (with[r] + without[r]) % N; } } else { figure_out(l); if (r == -1) { with[i] = without[l]; without[i] = (with[l] + without[l]) % N; } else { figure_out(r); with[i] = (without[l] * without[r]) % N; without[i] = (with[i] + (with[l] * without[r]) + (with[r] * without[l])) % N; } } } int main() { int n; std::cin >> n; with.resize(n, -1); without.resize(n, -1); for (int ni = 0; ni < n; ++ni) { int a, b; std::cin >> a >> b; children.emplace_back(a - 1, b - 1); } figure_out(0); // for (auto& w : with) std::cerr << w << " "; std::cerr << std::endl; // for (auto& w : without) std::cerr << w << " "; std::cerr << std::endl; std::cout << without[0] << std::endl; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
7 5 6 0 0 4 0 2 0 ... |
correct output |
---|
19 |
user output |
---|
19 |
Test 2
Verdict: ACCEPTED
input |
---|
1000000 406968 281253 108264 0 0 0 0 0 ... |
correct output |
---|
159944844 |
user output |
---|
159944844 |
Test 3
Verdict: ACCEPTED
input |
---|
1000000 1000000 325587 791377 324030 873422 0 0 0 ... |
correct output |
---|
114625606 |
user output |
---|
114625606 |
Test 4
Verdict: ACCEPTED
input |
---|
1000000 495129 959931 0 703069 862854 0 0 423221 ... |
correct output |
---|
922109659 |
user output |
---|
922109659 |
Test 5
Verdict: ACCEPTED
input |
---|
1000000 667150 768657 0 528730 716704 103616 554625 0 ... |
correct output |
---|
331516797 |
user output |
---|
331516797 |
Test 6
Verdict: ACCEPTED
input |
---|
1000000 490726 891332 0 988621 0 469251 224815 393797 ... |
correct output |
---|
831971669 |
user output |
---|
831971669 |
Test 7
Verdict: ACCEPTED
input |
---|
1000000 554662 668 0 0 0 0 0 806138 ... |
correct output |
---|
593438800 |
user output |
---|
593438800 |