diff options
| author | pjungeblut <paul.jungeblut@gmail.com> | 2014-11-23 23:01:04 +0100 |
|---|---|---|
| committer | pjungeblut <paul.jungeblut@gmail.com> | 2014-11-23 23:01:04 +0100 |
| commit | 3bf9e44bf552ef5ceef2a4eef87907cc1a8db09b (patch) | |
| tree | 0348c99d32361c5787a41740c0d1f5156a5bd031 /graph/LCA.cpp | |
| parent | 4cc304e57566d149582d974cdaf4a7f724c6b5c1 (diff) | |
| parent | 213662f659ed8b0a95da110ae6eb5e91e2ecae71 (diff) | |
gebaut
Diffstat (limited to 'graph/LCA.cpp')
| -rw-r--r-- | graph/LCA.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/graph/LCA.cpp b/graph/LCA.cpp new file mode 100644 index 0000000..b4e6bdd --- /dev/null +++ b/graph/LCA.cpp @@ -0,0 +1,16 @@ +//RMQ muss hinzugefuegt werden! +vector<int> visited(2*MAX_N), first(MAX_N, 2*MAX_N), depth(2*MAX_N); +vector<vector<int>> graph(MAX_N); + +void initLCA(int gi, int d, int &c) { + visited[c] = gi, depth[c] = d, first[gi] = min(c, first[gi]), c++; + for(int gn : graph[gi]) { + initLCA(gn, d+1, c); + visited[c] = gi, depth[c] = d, c++; + } +} +//[a, b] +int getLCA(int a, int b) { + return visited[queryRMQ(min(first[a], first[b]), max(first[a], first[b]))]; +} +//=> int c = 0; initLCA(0,0,c); initRMQ(); done! |
