0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 21:48:48 +00:00

P1129 [ZJOI2007] 矩阵游戏

https://www.luogu.com.cn/record/98680044
This commit is contained in:
Baoshuo Ren 2023-01-04 19:33:15 +08:00
parent f62e720b8e
commit a18fa4003b
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

58
Luogu/P1129/P1129.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
#include <functional>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 0;
std::vector<int> tag(n, -1), match(n, -1);
std::vector<std::vector<int>> g(n);
for (int i = 0; i < n; i++) {
for (int j = 0, x; j < n; j++) {
cin >> x;
if (x) g[i].emplace_back(j);
}
}
std::function<bool(int, int)> dfs = [&](int u, int t) {
if (tag[u] == t) return false;
tag[u] = t;
for (int v : g[u]) {
if (match[v] == -1 || dfs(match[v], t)) {
match[v] = u;
return true;
}
}
return false;
};
for (int i = 0; i < n; i++) {
if (dfs(i, i)) ans++;
}
cout << (ans == n ? "Yes" : "No") << endl;
}
return 0;
}