-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTechnobabble.py
36 lines (29 loc) · 1014 Bytes
/
Technobabble.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
import networkx as nx
from networkx.algorithms import bipartite
T = int(input())
for i in range(1, T + 1):
N = int(input())
G = nx.DiGraph()
Gleft = set()
Gright = set()
for j in range(N):
u, v = input().split()
G.add_edge(u+"1", v+"2", capacity=1)
G.add_edge('SOURCE', u+"1", capacity=1)
G.add_edge(v+"2", 'SINK', capacity=1)
Gleft.add(u+"1")
Gright.add(v+"2")
mfmc = nx.max_flow_min_cost(G, 'SOURCE', 'SINK')
G1 = nx.DiGraph()
for u in mfmc.keys():
if u != 'SOURCE' and u != 'SINK':
for v in mfmc[u].keys():
if v != 'SOURCE' and v != 'SINK' and mfmc[u][v] == 1:
G1.add_edge(u, v)
G.remove_node('SOURCE')
G.remove_node('SINK')
G1left, G1right = bipartite.sets(G1)
leftDif = Gleft - G1left
rightDif = Gright - G1right
counter = len(leftDif) + len(rightDif)
print("Case #{}: {}".format(i, G.number_of_edges() - G1.number_of_edges() - counter))