diff options
Diffstat (limited to 'content/graph/virtualTree.cpp')
| -rw-r--r-- | content/graph/virtualTree.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/content/graph/virtualTree.cpp b/content/graph/virtualTree.cpp new file mode 100644 index 0000000..27d2d6c --- /dev/null +++ b/content/graph/virtualTree.cpp @@ -0,0 +1,22 @@ +// needs dfs in- and out- time and lca function +vector<int> in, out; + +void virtualTree(vector<int> ind) { // indices of used nodes + sort(all(ind), [&](int x, int y) {return in[x] < in[y];}); + for (int i = 0, n = sz(ind); i < n - 1; i++) { + ind.push_back(lca(ind[i], ind[i + 1])); + } + sort(all(ind), [&](int x, int y) {return in[x] < in[y];}); + ind.erase(unique(all(ind)), ind.end()); + + int n = ind.size(); + vector<vector<int>> tree(n); + vector<int> st = {0}; + for (int i = 1; i < n; i++) { + while (in[ind[i]] >= out[ind[st.back()]]) st.pop_back(); + tree[st.back()].push_back(i); + st.push_back(i); + } + // virtual directed tree with n nodes, original indices in ind + // weights can be calculated, e.g. with binary lifting +} |
