Help on Component DFS problem - MARLA

Anyone please help me what is wrong with the following code for the problem - MARLA Problem - CodeChef

import sys
sys.setrecursionlimit(10**7)
class ComponentGraph:
    def __init__(self,u,v):
        self.u = u
        self.v = v
        self.visited = [[False for _ in range(u) ] for _ in range(v)]
        self.graph = []
        self.dx = [0,0,1,-1]
        self.dy = [1,-1,0,0]
        
    def addEdges(self,row):
        self.graph.append(row)
        
    def valid(self,i,j,check):
        if i>=0 and i<self.u:
            if j>=0 and j<self.v:
                if not self.visited[i][j]:
                    if check==self.graph[i][j]:
                        return True
        return False
        
    def dfs(self,i,j,cnt):
        self.visited[i][j] = True
        cnt += 1
        for k in range(0,4):
            ui = i+self.dx[k]
            vi = i+self.dy[k]
            if self.valid(ui,vi,self.graph[i][j]):
                cnt+=self.dfs(ui,vi,0)
        return cnt
        
n,m = map(int,sys.stdin.readline().split())
g = ComponentGraph(n,m)
area = 0
bank = 10**9
for _ in range(n):
    g.addEdges(list(map(int,sys.stdin.readline().split())))
for i in range(n):
    for j in range(m):
        if not g.visited[i][j]:
            tmpArea = g.dfs(i,j,0)
            if tmpArea>area:
                area = tmpArea
                bank = g.graph[i][j]
            elif tmpArea==area and bank>g.graph[i][j]:
                bank = g.graph[i][j]
print(area,bank)

@l_returns will you please help me

Thank you so much @vijju123 for the help​:blush: Finally I understood what was the issue, I am glad about it :cowboy_hat_face:

1 Like