Python 3 DFS solution, Time complexity: O(edges)


#1
def dfs(count,adj,visited,curr):
    if count == 0:
        return 

    if adj.get(curr)==None:
        return 
    for i in adj[curr]:
        if visited[i]==0:
            visited[i] = 1
            if dfs(count-1,adj,visited,I):
                return True
            visited[i] = 0
        else:
            return True


class Solution:
    def solve(self, A, B, C):
        adj_list = {}
    
        for i in range(len(B)):
            adj_list[B[i]] = adj_list.get(B[i],[])+[C[i]]
    
        visited = [0]*(A+1)
        visited[1] = 1
        return 0 if dfs(A,adj_list,visited,1) else 1