#684

from collections import defaultdict

class Solution:
    def find_redundant_connection(self, edges):
        graph = defaultdict(set)

        for u, v in edges:
            visited = set()
            if u in graph and v in graph and self.dfs(u, v, graph, visited):
                return [u, v]
            
            graph[u].add(v)
            graph[v].add(u)
    
    def dfs(self, source, target, graph, visited):
        if source in visited:
            return False

        visited.add(source)
        
        if source == target:
            return True
        
        return any(self.dfs(nbr, target, graph, visited) for nbr in graph[source])

solution = Solution()
edges = [[1,2], [1,3], [2,3]]
# edges = [[1,2], [2,3], [3,4], [1,4], [1,5]]
print(solution.find_redundant_connection(edges))