Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global cycle, cycle_begin, cycle_end
- cycle = -1
- cycle_begin = 0
- cycle_end = 0
- def dfs(s):
- global cycle, cycle_begin, cycle_end
- visited[s] = 1
- for i in range(len(g[s])):
- if visited[g[s][i]] == 0:
- parent[g[s][i]] = s
- dfs(g[s][i])
- elif visited[g[s][i]] == 1:
- cycle = 1
- cycle_begin = g[s][i]
- cycle_end = s
- visited[s] = 2
- n, m = map(int, input().split(' '))
- g = {}
- for i in range(1, n+1):
- g[i] = []
- for i in range(m):
- a, b = map(int, input().split(' '))
- g[a].append(b)
- parent = [0] * (n+1)
- visited = [0] * (n+1)
- path = []
- for i in range(1, n):
- if cycle == 1:
- break
- if visited[i] == 0:
- dfs(i)
- if cycle == 1:
- print("YES")
- a = []
- while cycle_end != cycle_begin:
- a.append(str(cycle_end))
- cycle_end = parent[cycle_end]
- a.append(str(cycle_begin))
- a.reverse()
- print(' '.join(a))
- else:
- print("NO")
Advertisement
Add Comment
Please, Sign In to add comment