-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17412.py
More file actions
32 lines (31 loc) · 770 Bytes
/
Copy path17412.py
File metadata and controls
32 lines (31 loc) · 770 Bytes
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
import sys
rl = sys.stdin.readline
N, P = map(int, rl().split())
c = [[0 for _ in range(N+1)] for _ in range(N+1)]
for _ in range(P):
a, b = map(int, rl().split())
c[a][b] = 1
flow = [[0 for _ in range(N+1)] for _ in range(N+1)]
ans=0
while True:
p = [-1 for _ in range(N+1)]
q = [1]
while (q and p[2]==-1):
now = q.pop()
for i in range(1,N+1):
if c[now][i]-flow[now][i]>0 and p[i]==-1:
q.append(i)
p[i] = now
if p[2] == -1: break
amount = 1e9
x = 2
while x != 1:
amount = min(c[p[x]][x]-flow[p[x]][x], amount)
x = p[x]
x = 2
while x!= 1:
flow[p[x]][x] += amount
flow[x][p[x]] -= amount
x = p[x]
ans += amount
print(ans)