-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2018-j5.py
51 lines (42 loc) · 953 Bytes
/
2018-j5.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
import queue
q = queue.Queue()
graph = {}
visited = []
dist = {}
checked = []
shortest = []
N = int(input())
def bfs(node):
q.put(node)
while not q.empty():
u = q.get()
checked.append(u)
for v in graph[u]:
if dist[v] == 0:
dist[v] = dist[u] + 1
q.put(v)
for i in range(1, N+1):
graph[str(i)] = []
line = input().split()
if line != ['0']:
for j in range(1, len(line)):
if str(i) in graph.keys():
graph[str(i)].append(line[j])
else:
graph[str(i)] = [line[j]]
else:
shortest.append(i)
for i in range(1, N+1):
dist[str(i)] = 0
bfs('1')
if len(checked) >= len(graph.keys()):
print("Y")
else:
print("N")
l = []
for i in shortest:
l.append(dist[str(i)])
for j in sorted(l):
if j != 0:
print(j+1)
break