forked from shunr/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtle16c8p5.py
More file actions
55 lines (51 loc) · 1.2 KB
/
tle16c8p5.py
File metadata and controls
55 lines (51 loc) · 1.2 KB
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
from collections import *
SINK = 42069
n, m = map(int, input().split())
wanted = set()
graph = defaultdict(list, {0: [], SINK:[]})
edges = {}
for g in [int(i) for i in input().split()][1:]:
wanted.add(g)
for i in range(1,n):
girls = [int(i) for i in input().split()][1:]
graph[0].append(i)
graph[i].append(0)
edges[(0, i)] = 1
edges[(i, 0)] = 0
for x in girls:
if x in wanted:
g = x + n
graph[i].append(g)
graph[g].append(i)
graph[SINK].append(g)
graph[g].append(SINK)
edges[(i, g)] = 1
edges[(g, i)] = 0
edges[(g, SINK)] = 1
edges[(SINK, g)] = 0
def bfs(G, E, s, t, parent):
visited = set([s])
q = deque([s])
while q:
x = q.popleft()
if x == t:
return True
for n in G[x]:
if n not in visited and E[(x, n)] > 0:
q.append(n)
visited.add(n)
parent[n] = x
return False
def ford(G, E, src, sink):
parent = {}
flow = 0
while bfs(G, E, src, sink, parent):
flow += 1
x = sink
while(x != src):
p = parent[x]
E[(p, x)] -= 1
E[(x, p)] += 1
x = p
return flow
print(len(wanted) - ford(graph, edges, 0, SINK))