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
| #include <bits/stdc++.h> using namespace std;
const int MAX_V = 100;
vector<int> G[MAX_V]; int root;
int vs[MAX_V * 2 - 1]; int depth[MAX_V * 2 - 1]; int id[MAX_V];
void dfs(int v, int p, int d, int &k) { id[v] = k; vs[k] = v; depth[k++] = d; for(int i = 0; i < G[v].size(); i++) { if(G[v][i] != p) { dfs(G[v][i], v, d + 1, k); vs[k] = v; depth[k++] = d; } } }
void init(int V) { int k = 0; dfs(root, -1, 0, k); rmg_init(depth, V * 2 - 1); }
int lca(int u, int v) { return vs[query(min(id[u], id[v]), max(id[u], id[v]) + 1)]; }
|