-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_gold5_1068.py
48 lines (38 loc) · 939 Bytes
/
python_gold5_1068.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
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
graph = list([set() for i in range(n)])
idx = 0
for value in list(map(int, input().split())):
if value != -1:
graph[value].add(idx)
idx += 1
print(graph)
rid_node = int(input())
cnt = 0
visited = []
# rid node process
queue = deque([rid_node])
while queue:
idx = queue.popleft()
print(f"deleting idx {idx}")
visited.append(idx)
for j in graph[idx]:
if j not in visited:
queue.append(j)
print(f"deleted nodes = {visited}")
for i in range(len(graph)):
if i not in visited:
queue.append(i)
print(f"visiting node = {i}")
while queue:
idx = queue.popleft()
visited.append(idx)
if len(graph[idx]) == 0 or ((len(graph[idx]) <= 1) and (rid_node in graph[idx])):
cnt += 1
continue
for j in graph[idx]:
if j not in visited:
queue.append(j)
print(cnt)