diff options
| author | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
|---|---|---|
| committer | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
| commit | 5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch) | |
| tree | ed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /graph/LCA.cpp | |
| parent | adabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff) | |
updated tcr
Diffstat (limited to 'graph/LCA.cpp')
| -rw-r--r-- | graph/LCA.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/graph/LCA.cpp b/graph/LCA.cpp new file mode 100644 index 0000000..bdb8f12 --- /dev/null +++ b/graph/LCA.cpp @@ -0,0 +1,24 @@ +vector<vector<int>> adjlist(); +vector<int> visited(); +vector<int> first(); +vector<int> depth(); + +void initLCA(int gi, int d, int& c) { + visited[c] = gi, depth[c] = d, first[gi] = min(c, first[gi]), c++; + for(int gn : adjlist[gi]) { + initLCA(gn, d+1, c); + visited[c] = gi, depth[c] = d, c++; +}} + +int getLCA(int a, int b) { + return visited[query(min(first[a], first[b]), max(first[a], first[b]))]; +} + +void exampleUse() { + int c = 0; + visited = vector<int>(2*adjlist.size()); + first = vector<int>(adjlist.size(), 2*adjlist.size()); + depth = vector<int>(2*adjlist.size()); + initLCA(0, 0, c); + init(depth); +} |
