-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCycle_Finding.py
66 lines (53 loc) · 1.84 KB
/
Cycle_Finding.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
INT_MAX =float('infinity')
n,m = list(map(int,input().split()))
graph = [[]for i in range(n+1)]
# for i in range(m):
# a,b,w = list(map(int,input().split()))
# graph[a].append((b,w))
# since bellman ford is required we require all the edges for the loop
# therefore it is convinient to store all the edges in other format
# alo note that one of the test case has n = 2500 but they dont have many edges connected to them
# THis would cause the function to run many times therefore we assign INT_MAX to those nodes only which have edges
edges = []
distance = [False for i in range(n+1)]
for i in range(m):
a,b,w = list(map(int,input().split()))
edges.append((a,b,w))
distance[a] = INT_MAX
distance[b] = INT_MAX
visited = [False for i in range(n+1)]
parent = [-1 for i in range(n+1)]
def bellman_ford(source):
# print(source,"--")
for i in range(1,n):
distance[source] = 0
for a,b,w in edges:
if distance[a]!=INT_MAX and distance[b]>distance[a]+w:
distance[b] = distance[a]+w
parent[b] = a
for a,b,w in edges:
if distance[a]!=INT_MAX and distance[b]>distance[a]+w:
print("YES")
# print(a,b)
ans = [str(a)]
temp = a
a = parent[a]
while a!=temp and str(a) not in ans:
ans.append(str(a))
a= parent[a]
# print(ans,a)
ans.append(str(a))
# print(" ".join(ans[::-1]))
print(" ".join(ans[ans.index(str(a)):][::-1]))
return True
return False
flag = True
for i in range(1,n+1):
if distance[i]==INT_MAX:
a = bellman_ford(i)
if a:
flag = False
break
# print(parent)
if flag:
print("NO")