-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepth_first_tree.cpp
93 lines (82 loc) · 2.74 KB
/
depth_first_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "test_utils.hpp"
#include "struct/depth_first_tree.hpp"
#include "lib/tree_action.hpp"
using namespace tree_testing;
void stress_test_depth_first_tree() {
static actions_t<RootedAT> stress_actions = {
{RootedAT::LINK, 1500}, //
{RootedAT::CUT, 300}, //
{RootedAT::LINK_CUT, 1200}, //
{RootedAT::FINDROOT, 1000}, //
// {RootedAT::LCA, 500}, //
// {RootedAT::LCA_CONN, 1000}, //
{RootedAT::UPDATE_NODE, 1500}, //
{RootedAT::QUERY_NODE, 2000}, //
// {RootedAT::UPDATE_PATH, 3500}, //
// {RootedAT::QUERY_PATH, 3500}, //
// {RootedAT::PATH_LENGTH, 1500}, //
{RootedAT::UPDATE_SUBTREE, 3500}, //
{RootedAT::QUERY_SUBTREE, 3500}, //
{RootedAT::SUBTREE_SIZE, 1500}, //
// {RootedAT::UPDATE_TREE, 2500}, //
// {RootedAT::QUERY_TREE, 2500}, //
// {RootedAT::TREE_SIZE, 1000}, //
};
const int N = 300;
slow_tree<true> slow(N);
depth_first_tree<dft_node_sum<int64_t>> tree(N);
auto actions = make_rooted_actions(N, 2s, stress_actions, 9 * N / 10);
bool ok = true;
for (int ia = 0, A = actions.size(); ia < A; ia++) {
print_regular(ia, A, 1000, "stress test dft tree");
auto [action, u, v, r, who, val] = actions[ia];
switch (action) {
case RootedAT::LINK: {
slow.link(u, v);
tree.link(u, v);
} break;
case RootedAT::CUT: {
slow.cut(u);
tree.cut(u);
} break;
case RootedAT::FINDROOT: {
int ans = tree.findroot(u);
ok = ans == who;
} break;
case RootedAT::QUERY_NODE: {
long ans = tree.access_node(u)->self;
ok = val == ans;
} break;
case RootedAT::UPDATE_NODE: {
slow.update_node(u, val);
tree.access_node(u)->self = val;
} break;
case RootedAT::UPDATE_SUBTREE: {
slow.update_subtree(u, val);
tree.access_subtree(u)->add_subtree(val);
tree.end_access();
} break;
case RootedAT::QUERY_SUBTREE: {
long ans = tree.access_subtree(u)->subtree();
tree.end_access();
ok = val == ans;
} break;
case RootedAT::SUBTREE_SIZE: {
long ans = tree.access_subtree(u)->subtree_size();
tree.end_access();
ok = val == ans;
} break;
default:
throw runtime_error("Unsupported action");
}
if (!ok) {
printcl("Failed action: {}\n", action_names<RootedAT>.at(action));
}
assert(ok);
}
assert(ok);
}
int main() {
RUN_BLOCK(stress_test_depth_first_tree());
return 0;
}