'''
查找流量图的关键边,输出关键边的数量即可
'''
from typing import List
from collections import deque
class FortdFulkerson:
# 输入图中所有边列表[(from1, to1, weight1), (form2, to2, weight2), ......]
# 边可以有重边和自环边
# souece_node 和 end_node 分别为源点和汇点
# 所有节点从1开始连续编号, max_node_num是最大节点数,max_edge_nums是最大边数
def __init__(self, edges, source_node, end_node, max_node_num, max_edge_num):
self.edges = edges[::]
self.source_node = source_node
self.end_node = end_node
self.max_edge_num = max_edge_num
self.max_node_num = max_node_num
# 获取关键路径的标号列表,标号从0开始编号
def getKeyEdges(self):
e = [-1] * (self.max_edge_num * 2 + 1) # e[idx]表示编号为idx残量图边的终点, idx//2 就是残量图边对应的原图边的编号
f = [-1] * (self.max_edge_num * 2 + 1) # f[idx]表示编号为idx的残量图边的流量
ne = [-1] * (self.max_edge_num * 2 + 1) # ne[idx]表示根编号为idx的边同一个起点的下一条边的编号
h = [-1] * (self.max_node_num + 1) # h[a]表示节点a为起点的所有边的链表头对应的边的编号
dis = [-1] * (self.max_node_num + 1) # dis[a]表示点a到源点的距离,用于记录分层图信息
cur = [-1] * (self.max_node_num + 1) # cur[a]表示节点a在dfs搜索中第一次开始搜索的边的下标,也称当前弧,用于优化dfs速度
orig_flow = [0] * (self.max_edge_num + 1) # 原图中有向边的流量
idx = 0
for a, b, w in self.edges:
e[idx], f[idx], ne[idx], h[a] = b, w, h[a], idx
idx += 1
e[idx], f[idx], ne[idx], h[b] = a, 0, h[b], idx
idx += 1
# bfs搜索有没有增广路
def bfs() -> bool:
for i in range(self.max_node_num + 1):
dis[i] = -1
que = deque()
que.append(self.source_node)
dis[self.source_node] = 0
cur[self.source_node] = h[self.source_node]
while len(que) > 0:
cur_node = que.popleft()
idx = h[cur_node]
while idx != -1:
next_node = e[idx]
if dis[next_node] == -1 and f[idx] > 0:
dis[next_node] = dis[cur_node] + 1
cur[next_node] = h[next_node]
if next_node == self.end_node:
return True
que.append(next_node)
idx = ne[idx]
return False
# dfs查找增广路, 返回当前残量图上node节点能流入汇点的不超过limit的最大流量有多事少
def dfs(node, limit) -> int:
if node == self.end_node:
return limit
flow = 0
idx = cur[node] # 从节点的当前弧开始搜索下一个点
while idx != -1 and flow < limit:
# 当前弧优化,记录每一个节点最后走的一条边,只要limit还没有减成0,已经搜过的边的终点
# 能够汇入汇点的流量就已经全部用完了,另外一条路径到同一个点时候没必要重复搜索已经不会
# 再提供流量贡献的邻接点
cur[node] = idx
next_node = e[idx]
if dis[next_node] == dis[node] + 1 and f[idx] > 0:
t = dfs(next_node, min(f[idx], limit - flow))
if t == 0:
# 已经无法提供流量的废点闪删除掉,不再参与搜索
dis[next_node] = -1
# 更新残量图边的流量
f[idx], f[idx ^ 1], flow = f[idx] - t, f[idx ^ 1] + t, flow + t
# 更新原图边的流量
if self.edges[idx >> 1][0] == node:
orig_flow[idx >> 1] += t
else:
orig_flow[idx >> 1] -= t
idx = ne[idx]
return flow
while bfs():
# 只要还有增广路,就dfs把增广路都找到,把增广路上的流量加到可行流上
dfs(self.source_node, 0x7fffffff)
# 筛选满流的边
candi_e = []
for i in range(len(self.edges)):
if self.edges[i][2] == orig_flow[i]:
candi_e.append(i)
s_reachable = set() # S能够到的点
t_reachable = set() # 能够到T的点
# 从S和T分别正向和反向搜索能够到的点
def travel(cur, visit):
visit[cur] = 1
s_reachable.add(cur)
idx = h[cur]
while idx != -1:
next_node = e[idx]
if visit[next_node] == 0 and f[idx] > 0:
travel(next_node, visit)
idx = ne[idx]
def rev_travle(cur, visit):
visit[cur] = 1
t_reachable.add(cur)
idx = h[cur]
while idx != -1:
next_node = e[idx]
if visit[next_node] == 0 and f[idx] != self.edges[idx>>1][2]:
# 方向边没有满流,等价于正向边还有流量可以用,next_node就可以沿着正向边到cur
rev_travle(next_node, visit)
idx = ne[idx]
travel(self.source_node, [0] * (self.max_node_num + 1))
rev_travle(self.end_node, [0] * (self.max_node_num + 1))
ans = []
for edge_idx in candi_e:
a, b, _ = self.edges[edge_idx]
if a in s_reachable and b in t_reachable:
ans.append(edge_idx)
return ans
n, m = map(int, input().split())
e = []
for _ in range(m):
a, b, w = map(int, input().split())
a, b = a + 1, b + 1
e.append((a, b, w))
algo = FortdFulkerson(e, 1, n, max_node_num=n, max_edge_num=len(e))
print(len(algo.getKeyEdges()))