传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1059
保存匈牙利模板。
#include <cstdio> #include <cstring> const int maxn = 205; const char _outp[2][5] = {"No", "Yes"}; int T, n, left[maxn], ans; char g[maxn][maxn], cover[maxn]; bool dfs(int i) { for (int j = 1; j <= n; ++j) { if (!g[i][j] || cover[j]) { continue; } cover[j] = 1; if (!left[j] || dfs(left[j])) { left[j] = i; return true; } } return false; } int main(void) { //freopen("in.txt", "r", stdin); scanf("%d", &T); while (T--) { scanf("%d", &n); ans = 0; memset(left, 0, sizeof left); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { scanf("%d", (int*)(g[i] + j)); } } for (int i = 1; i <= n; ++i) { memset(cover, 0, sizeof cover); if (dfs(i)) { ++ans; } } puts(_outp[ans == n]); } return 0; }
时间: 2024-12-14 04:25:58