-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphs-Find the nearest clone.py
68 lines (50 loc) · 1.48 KB
/
Graphs-Find the nearest clone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Problem Link : https://www.hackerrank.com/challenges/find-the-nearest-clone/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=graphs
#Ans:
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the findShortest function below.
#
# For the weighted graph, <name>:
#
# 1. The number of nodes is <name>_nodes.
# 2. The number of edges is <name>_edges.
# 3. An edge exists between <name>_from[i] to <name>_to[i].
#
#
from collections import defaultdict, deque
def solve(n, g, color, color_to_analyze):
queue = deque()
length = [-1] * (n + 1)
visited = [False] * (n + 1)
for v in range(1, n + 1):
if color[v] == color_to_analyze:
length[v] = 0
queue.appendleft(v)
while queue:
v = queue.pop()
visited[v] = True
for u in g[v]:
if not visited[u]:
if length[u] != -1:
return length[v] + length[u] + 1
else:
length[u] = length[v] + 1
queue.appendleft(u)
return -1
def main():
n, m = map(int, input().split())
g = defaultdict(set)
for _ in range(m):
u, v = map(int, input().split())
g[u].add(v)
g[v].add(u)
color = [0] * (n + 1)
color[1:] = list(map(int, input().split()))
color_to_analyze = int(input())
print(solve(n, g, color, color_to_analyze))
if __name__ == '__main__':
main()