spfa+差分约束
from collections import deque
n, m = map(int, input().split())
g = [[] for i in range(n + 1)]
for i in range(m):
a, b = map(int, input().split())
g[b].append((a, 1))
for i in range(1, n + 1):
g[0].append((i, 100))
dist = [float('-inf')] * (n + 1)
dist[0] = 0
cnt = [0] * (n + 1)
st = [False] * (n + 1)
def spfa():
q = deque()
q.append(0)
st[0] = True
while q:
i = q.popleft()
st[i] = False
for j, d in g[i]:
if dist[j] < dist[i] + d:
dist[j] = dist[i] + d
cnt[j] = cnt[i] + 1
if cnt[j] >= n + 1:
return True
if not st[j]:
st[j] = True
q.append(j)
return False
if spfa():
print("Poor Xed")
else:
print(sum(dist))