Contest Duration: ~ (local time) (240 minutes)

Submission #7296129

Source Code Expand

Copy
```import sys

INF = 10**18
C = [INF]*N

from collections import deque
class PushRelabel:
def __init__(self, N):
self.N = N
self.G = [[] for i in range(N)]
self.initial = [N]*N
self.zeros = [0]*(N+1)

forward = [to, cap, None]
forward[2] = backward = [fr, 0, forward]
self.G[fr].append(forward)
self.G[to].append(backward)

def add_multi_edge(self, v1, v2, cap1, cap2):
edge1 = [v2, cap1, None]
edge1[2] = edge2 = [v1, cap2, edge1]
self.G[v1].append(edge1)
self.G[v2].append(edge2)

# Global labeling
def bfs(self, H, D, t, que=deque()):
que.append(t)
G = self.G
H[:] = self.initial
H[t] = 0
D[:] = self.zeros
D[0] = 1
c = N-1
while que:
v = que.popleft()
d = H[v] + 1
for w, cap, backward in G[v]:
if H[w] <= d or backward[1] == 0:
continue
H[w] = d
D[d] += 1
c -= 1
que.append(w)
D[N] = c
return d # gap

# FIFO preflow-push algorithm
def flow(self, s, t):
N = self.N
H = [0]*N # height
F = [0]*N # excess flow
D = [0]*(N+1) # distance label
active = [0]*N # active node

G = self.G

que = deque([s])
F[s] = 10**18
active[s] = 1

gap = self.bfs(H, D, t)

cnt = 0
while que:
v = que.popleft()
if v == t:
continue

hv = H[v]
# Gap-relabeling
if hv > gap:
D[hv] -= 1
D[N] += 1
hv = H[v] = N
continue
# push
rest = F[v]
for e in G[v]:
w, cap, backward = e
if cap and hv > H[w] < gap:
d = min(rest, cap)
e[1] -= d
backward[1] += d
rest -= d
F[w] += d
if not active[w]:
que.append(w)
active[w] = 1
if rest == 0:
break
F[v] = rest

if rest == 0:
active[v] = 0
continue

# relabel
h0 = H[v]
hv = N
for w, cap, backward in G[v]:
if cap and hv > H[w] + 1 <= gap:
hv = H[w] + 1
if hv != h0:
D[h0] -= 1
if D[h0] == 0 and h0 < gap:
gap = h0
hv = N
elif hv == gap:
gap += 1
D[hv] += 1

H[v] = hv
if hv < N:
que.append(v)
else:
active[v] = 0

cnt += 1
if cnt % N == 0:
gap = self.bfs(H, D, t)
return F[t]

pl = PushRelabel(2*N+2)
ok = 1
for i in range(M):
a, b = map(int, readline().split()); a -= 1; b -= 1
if C[a] == C[b] == -1:
ok = 0
break
for i in range(N):
if C[i] == -1:
else:
if not ok:
sys.stdout.write("-1\n")
exit(0)
sys.stdout.write("%d\n" % pl.flow(2*N, 2*N+1))```

#### Submission Info

Submission Time 2019-09-02 00:03:40+0900 F - RPG yaketake08 PyPy3 (2.4.0) 200 3773 Byte AC 239 ms 43760 KB

#### Test Cases

Set Name Score / Max Score Test Cases
All 200 / 200 0-sample-1, 0-sample-2, 1-random-small-0, 1-random-small-1, 1-random-small-2, 1-random-small-3, 2-random-large-0, 2-random-large-1, 2-random-large-2, 2-random-large-3, 2-random-large-4, 2-random-large-5, 2-random-large-6, 2-random-large-7, 2-random-large-8, 2-random-large-9, 3-line-0, 3-line-1, 3-line-2, 3-line-3, 4-special-0, 4-special-1, 5-comp-0, 5-comp-1, 5-comp-2, 9-hand-0, 9-hand-1, 9-hand-2, 9-hand-3, 9-hand-4, 9-hand-5
Case Name Status Exec Time Memory
0-sample-1 168 ms 38384 KB
0-sample-2 162 ms 38256 KB
1-random-small-0 162 ms 38256 KB
1-random-small-1 161 ms 38256 KB
1-random-small-2 161 ms 38256 KB
1-random-small-3 160 ms 38256 KB
2-random-large-0 161 ms 38256 KB
2-random-large-1 210 ms 42608 KB
2-random-large-2 164 ms 38256 KB
2-random-large-3 183 ms 39792 KB
2-random-large-4 165 ms 38256 KB
2-random-large-5 217 ms 43376 KB
2-random-large-6 161 ms 38256 KB
2-random-large-7 208 ms 42352 KB
2-random-large-8 169 ms 38256 KB
2-random-large-9 210 ms 42096 KB
3-line-0 166 ms 38256 KB
3-line-1 214 ms 42992 KB
3-line-2 218 ms 42992 KB
3-line-3 162 ms 38256 KB
4-special-0 239 ms 43760 KB
4-special-1 226 ms 43504 KB
5-comp-0 170 ms 38256 KB
5-comp-1 171 ms 38768 KB
5-comp-2 170 ms 38256 KB
9-hand-0 163 ms 38256 KB
9-hand-1 171 ms 38384 KB
9-hand-2 172 ms 38256 KB
9-hand-3 161 ms 38256 KB
9-hand-4 162 ms 38256 KB
9-hand-5 161 ms 38256 KB