Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
764 B
31
Indexable
Never
class Solution:
    # @param A : integer
    # @param B : list of list of integers
    # @return an integer
    def solve(self, A, B):
        
        adj={i:[] for i in range(1,A+1)}
        for i in B:
            adj[i[0]].append(i[1])
        for i in adj:
            adj[i].sort()
        #print(adj)
        
        cycle=set()
        
        def dfs(crs):
            if crs in cycle:
                return False
            
            cycle.add(crs)
            
            for p in adj[crs]:
                if dfs(p)==False:
                    return False
            
        
        for i in range(1,A+1):
            cycle.clear()
            if dfs(i)==False:
                return 1       
        return 0