题意
给定(n imes m)的01矩阵,一对(1)能匹配当且仅当在同一行或同一列,一个矩阵合法当且仅当完美匹配且仅有一种方案。可以使矩阵中的(0)变成(1),要求变化次数最小使得矩阵合法(可能没有方法时矩阵合法)
(n,mle 10)
做法
将矩阵抽象成二分图
若(a_{i,j}=1),则左边(i)点与右边(j)点有连边
若有环则显然不合法
进一步的,合法的充要条件为每棵树的边为偶数,且从叶子节点往上仅有一种方法使相邻边匹配
对于森林,可以(O(n+m))判断是否合法
显然最优解不会给一个点加两条边
可以(O(2^{n+m}))枚举给每个点加的边({a}),这里加的边是算到匹配里面的
也就是若加入边((u,v)),那么(u,v)不会在一棵树种,将匹配算到(v)里面树中,那么枚举给点加边时(a_v=1),(a_u)不会由于这条边而变化
显然若一个点本来没有边,那么(a)会为(0)
还要判断一下({a})是否会形成环,先给空点匹配,再给树之间匹配