- Find the nearest clone
- Discussions
Find the nearest clone
Find the nearest clone
+ 0 comments Python3
import itertools class Node: def __init__(self, number, color_id): self.number = number self.color_id = color_id self.linked_nodes = set() def isLinkedWithNode(self, node): return node in self.linked_nodes def hasSameColorWithNode(self, node): return self.color_id == node.color_id class Graph: def __init__(self, graph_node_numbers, graph_from, graph_to, ids): self._graph_node_numbers = graph_node_numbers self._graph_from = graph_from self._graph_to = graph_to self._ids = ids self.nodes = self.__initNodes() self.__initEdges() def __initNodes(self): nodes = [] for number, color_id in zip(self._graph_node_numbers, self._ids): nodes.append(Node(number=number, color_id=color_id)) return nodes def __initEdges(self): for node_num1, node_num2 in zip(self._graph_from, self._graph_to): self.nodes[node_num1 - 1].linked_nodes.add(self.nodes[node_num2 - 1]) self.nodes[node_num2 - 1].linked_nodes.add(self.nodes[node_num1 - 1]) def nodeWithNumber(self, number): for node in self.nodes: if node.number == number: return node def nodesWithColorId(self, color_id): nodes = [] for node in self.nodes: if node.color_id == color_id: nodes.append(node) return nodes def pathsOfTwoNodes(self, node1, node2, traversed_nodes=set(), current_path=None): if current_path is None: current_path = [] true_paths = [] traversed_nodes.add(node1) for node in node1.linked_nodes: current_path.append(node) if node == node2: true_paths.append((current_path.copy())) else: if node not in traversed_nodes: sub_paths = self.pathsOfTwoNodes( node1=node, node2=node2, traversed_nodes=traversed_nodes, current_path=current_path.copy(), ) true_paths.extend(sub_paths) current_path.pop() return true_paths def findShortest(graph_nodes, graph_from, graph_to, ids, val): graph = Graph( graph_node_numbers=range(1, graph_nodes + 1), graph_from=graph_from, graph_to=graph_to, ids=ids, ) nodes_with_color = graph.nodesWithColorId(color_id=val) if len(nodes_with_color) < 2: return -1 paths = [] for node1, node2 in itertools.combinations(nodes_with_color, 2): paths.extend(graph.pathsOfTwoNodes( node1=node1, node2=node2, )) if not paths: return -1 else: return min(map(len, paths))
+ 0 comments It is possible that given color is not in list of colors :). I missed that and 7 test cases were failing.
+ 0 comments In swift
I just failed in 1 test case10 at submission time. can anyone please help me out
import Foundation struct Queue<Element> { private var elements: [Element] = [] mutating func enqueue(_ element: Element) { elements.append(element) } mutating func dequeue() -> Element? { if !elements.isEmpty { return elements.removeFirst() } return nil } var isEmpty: Bool { return elements.isEmpty } } func bfs(_ graph: [[Int]], _ a: [Int], _ id: Int, _ start: Int) -> Int { let n = graph.count var visited = [Bool](repeating: false, count: n) var queue = Queue<(Int, Int)>() queue.enqueue((start, 0)) visited[start] = true while !queue.isEmpty { if let (node, dist) = queue.dequeue() { for neighbor in graph[node] { if !visited[neighbor] { if a[neighbor] == id { return dist + 1 } visited[neighbor] = true queue.enqueue((neighbor, dist + 1)) } } } } return Int.max } func findShortestDistance() { let input = readLine()!.split(separator: " ").map { Int($0)! } let n = input[0] let m = input[1] var graph = [[Int]](repeating: [], count: n) var a = [Int](repeating: 0, count: n) for _ in 0..<m { let edge = readLine()!.split(separator: " ").map { Int($0)! } let x = edge[0] - 1 let y = edge[1] - 1 graph[x].append(y) graph[y].append(x) } let aValues = readLine()!.split(separator: " ").map { Int($0)! } for i in 0..<n { a[i] = aValues[i] } let id = Int(readLine()!)! var ans = Int.max for i in 0..<n { if a[i] == id { let c = bfs(graph, a, id, i) ans = min(ans, c) } } if ans == Int.max { ans = -1 } print(ans) } findShortestDistance()
+ 0 comments Hi All, How is the answer to the testcase 2 is 3 ? Should not this be 2 ?
5 4 1 2 1 3 2 4 3 5 1 2 3 3 2 So if we say start BFS from 2 from 2->4 and from 4->5 both 2 and 5 are of color id 2 and hence the answer should be 2 correct ?
+ 0 comments One clarifying question, would be glad if someonce could point out if I am missing anything here.
In the constraints, n: the number of nodes has been given to be 1<=n<=10^6 and ids[i]: the color code of the ith node is given to be 1<=ids[i]<=10^8.
But, how does it make sense to have more color id's than actual nodes. Lets say we have 6 nodes, then range of values for ids[i] is [1,6].
Sort 182 Discussions, By:
Please Login in order to post a comment