就是二分图匹配,直接匈牙利即可。
#include <bits/stdc++.h> const int N = 220; bool used[N]; int hascow[N], n, link[N][N]; bool find(int x) { for (int i = 1; i <= n; i++) { if (link[x][i] && !used[i]) { used[i] = 1; if (!hascow[i] || find(hascow[i])) { hascow[i] = x; return 1; } } } return 0; } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) scanf("%d", &link[i][j]); hascow[i] = 0; } int ans = 0; for (int i = 1; i <= n; i++) { memset(used, 0, sizeof(used)); ans += find(i); } if (ans == n) puts("Yes"); else puts("No"); } return 0; }View Code