Skip to content

Commit ffe13e9

Browse files
author
Mohamed Ayman
committed
Add const size for array
1 parent c9fae93 commit ffe13e9

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/lca_demo.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
#include <bits/stdc++.h>
1+
#include <cstdio>
2+
#include <vector>
23

3-
int numberOfNodes, MAXLOG;
4+
const int MAX_NODE = 5000;
5+
const int MAX_LOG = 20;
6+
7+
int numberOfNodes, maxLog;
48
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];
812

913
void dfs(int currentNode, int currentParent)
1014
{
@@ -31,10 +35,10 @@ int getMaxLog(){
3135
void initializeDP()
3236
{
3337
nodeHeight[-1] = -1;
34-
MAXLOG = getMaxLog();
38+
maxLog = getMaxLog();
3539
dfs(0, -1);
3640
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++)
3842
{
3943
for(int j = 0; j < numberOfNodes; j++)
4044
{
@@ -48,13 +52,13 @@ void initializeDP()
4852
int LCA(int a, int b)
4953
{
5054
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--)
5256
{
5357
if(binaryLiftDp[a][i] + 1 && nodeHeight[binaryLiftDp[a][i]] >= nodeHeight[b])
5458
a = binaryLiftDp[a][i];
5559
}
5660
if(!(a - b)) return a;
57-
for(int i = MAXLOG; i >= 0; i--)
61+
for(int i = maxLog; i >= 0; i--)
5862
{
5963
if(binaryLiftDp[a][i] + 1 && binaryLiftDp[a][i] - binaryLiftDp[b][i])
6064
a = binaryLiftDp[a][i], b = binaryLiftDp[b][i];

0 commit comments

Comments
 (0)