-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdagparent.cpp
More file actions
45 lines (39 loc) · 1.27 KB
/
dagparent.cpp
File metadata and controls
45 lines (39 loc) · 1.27 KB
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
class solution{
vector<int> parent(MAX_NODES, 0), visited(MAX_NODES,0); /*Keeps track of the parent of every vertex in the tree*/
vector<vector<int> > graph;
/*GetParents() function traverses the tree and computes the parent array such that
The pre-order traversal begins by calling GetParents(root_node,-1) */
void getparent(int node , int parent){
if (visited(node))
return;
visited.insert(node);
for (auto a:graph[node]) {
if (a != parent) {
parent[a] = node;
getparent(a, node);
}
}
}
int LCA(int u , int v){
/*traverse from node u uptil root node and mark the vertices encountered along the path . */
int lca = -1 ;
fill(visited.begin(),visited.end(),0);
while(1){
visited[u] = true ;
if (u == entry_node)
break;
u = parent[u];
}
while(1) {
if (visited[v]) {
lca = v;
break;
}
if (v == entry_node_2)
break;
v = parent[v];
}
return lca ;
}
http://www.gghh.name/dibtp/2014/02/25/how-does-mercurial-select-the-greatest-common-ancestor.html
Need find the parent list (depth, parent_node}