mooyyu

G2 - 二分图-最大匹配

c++11 graph

class TwoSet {
	const int ln, rn;
	int ori[maxn + 1] {};
	bool vis[maxn + 1] {};
	bool match(const int x) {
		for (int i = head[x]; i; i = ar[i].ptr)
			if (!vis[ar[i].to]) {
				vis[ar[i].to] = true;
				if (!ori[ar[i].to] || match(ori[ar[i].to])) {
					ori[ar[i].to] = x;
					return true;
				}
			}
		return false;
	}
public:
	explicit TwoSet(const int ln, const int rn): ln(ln), rn(rn) {}
	struct { int to, ptr; } ar[maxm + 1] {};
	int head[maxn + 1] {};
	int solve() {
		fill(ori + 1, ori + rn + 1, 0);
		int ret = 0;
		for (int i = 1; i <= ln; i++) {
			fill(vis + 1, vis + rn + 1, false);
			if (match(i)) ++ret;
		}
		return ret;
	}
};

Notes

  • 链式前向星存储无向图但只需要一倍的maxm长度(+1), 因为只需要一个集合指向另一个集合的单向边
  • 注意每求解起始集合中的一个点时vis所表示的状态都需要重置