1
- #include < bits/stdc++.h>
1
+ #include < cstdio>
2
+ #include < vector>
2
3
3
- int numberOfNodes, MAXLOG;
4
+ const int MAX_NODE = 5000 ;
5
+ const int MAX_LOG = 20 ;
6
+
7
+ int numberOfNodes, maxLog;
4
8
std::vector< std::vector<int > > adjList;
5
- int parent[50005 ], nodeHeight[50005 ];
6
- bool visited[50005 ];
7
- int binaryLiftDp[50005 ][ 27 ];
9
+ int parent[MAX_NODE ], nodeHeight[MAX_NODE ];
10
+ bool visited[MAX_NODE ];
11
+ int binaryLiftDp[MAX_NODE][MAX_LOG ];
8
12
9
13
void dfs (int currentNode, int currentParent)
10
14
{
@@ -31,10 +35,10 @@ int getMaxLog(){
31
35
void initializeDP ()
32
36
{
33
37
nodeHeight[-1 ] = -1 ;
34
- MAXLOG = getMaxLog ();
38
+ maxLog = getMaxLog ();
35
39
dfs (0 , -1 );
36
40
for (int i = 0 ; i < numberOfNodes; i++) binaryLiftDp[i][0 ] = parent[i];
37
- for (int i = 1 ; i <= MAXLOG ; i++)
41
+ for (int i = 1 ; i <= maxLog ; i++)
38
42
{
39
43
for (int j = 0 ; j < numberOfNodes; j++)
40
44
{
@@ -48,13 +52,13 @@ void initializeDP()
48
52
int LCA (int a, int b)
49
53
{
50
54
if (nodeHeight[a] < nodeHeight[b]) std::swap (a,b);
51
- for (int i = MAXLOG ; i >= 0 ; i--)
55
+ for (int i = maxLog ; i >= 0 ; i--)
52
56
{
53
57
if (binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b])
54
58
a = binaryLiftDp[a][i];
55
59
}
56
60
if (!(a - b)) return a;
57
- for (int i = MAXLOG ; i >= 0 ; i--)
61
+ for (int i = maxLog ; i >= 0 ; i--)
58
62
{
59
63
if (binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i])
60
64
a = binaryLiftDp[a][i], b = binaryLiftDp[b][i];
0 commit comments