-
Notifications
You must be signed in to change notification settings - Fork 1
/
lca.cpp
49 lines (43 loc) · 1.76 KB
/
lca.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
#include <assert.h>
#include <iostream>
#include <string>
#include "tree.hpp"
#include "lca.hpp"
#include "pm_rmq.hpp"
int main(int argc, const char *argv[]) {
using std::string;
std::vector<tree<string> > a_children;
{
std::vector<tree<string> > b_children;
b_children.push_back(tree<string>("c"));
b_children.push_back(tree<string>("d"));
b_children.push_back(tree<string>("e"));
a_children.push_back(tree<string>("b", std::move(b_children)));
}
{
std::vector<tree<string> > f_children;
{
std::vector<tree<string> > g_children;
g_children.push_back(tree<string>("h"));
f_children.push_back(tree<string>("g", std::move(g_children)));
}
f_children.push_back(tree<string>("i"));
a_children.push_back(tree<string>("f", std::move(f_children)));
}
tree<string> input("a", std::move(a_children));
lca<string, pm_rmq<std::vector<ssize_t>::const_iterator>> lca(input);
#define LCA_TEST(u, v, expect) do { \
string ret = lca.query(u, v); \
if (expect != ret) { \
std::cout << "expected LCA(" << u.id() << ", " << v.id() << ") = " << expect << std::endl; \
std::cout << "got " << ret << std::endl; \
} \
assert(ret == expect); \
} while (0)
LCA_TEST(input, input, "a");
LCA_TEST(input.children()[0], input.children()[1], "a");
LCA_TEST(input.children()[0].children()[0], input.children()[0].children()[2], "b");
LCA_TEST(input.children()[1].children()[0].children()[0], input.children()[1].children()[1], "f");
#undef LCA_TEST
return 0;
}