http://codeforces.com/blog/entry/63046

http://codeforces.com/blog/entry/63046

 

Big spoilers ahead:

Dogs with the same tag are equivalent. Same for cats with the same tag. So, imagine a flow network where we "merge" all those into a single node. From s to each "cat-set", put an edge with capacity equal to the set's size. From each "dog-set" to t, put an edge with capacity equal to the set size. Put an edge from each "cat-set" to each "dog-set" having a different tag, with infinite capacity [this is important for the problem's model].

If you add the "sets of size 0" so that every set has a matching oposite set, the graph structure (except for the capacities) is completely fixed: it is almost a "complete bipartite", except for the edges going from vertex i on one side to vertex i on the other side.

Now, max-flow on that network would solve the problem. But let's compute min-cut instead: the network has so many edges that there are very few cuts to consider, and by max-flow = min-cut, that will be the solution.

Since the edges in the middle have infinite capacity, the min cut will never cut them, so we only need to consider cutting the other edges. The simplest possibility is cutting a whole side, which gives M or N as answers. If we leave the edge associated to vertex i on one side, without cutting it, then we are forced to cut the edges of every vertex on the other side, except for the corresponding vertex i. Applying the same reasoning to that other vertex, we must cut every single "outer" edge, except for the ones corresponding to both vertices "i". So, that gives only max(N, M)additional cuts to consider, apart from the previous two. The capacity of each one is M + N - ai - bi , where ai and biare the amount of cats and dogs with tag i.

So the final answer is min(M,N, min(over all iM + N - ai - bi))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值