[Bzoj1059][ZJOI2007]矩阵游戏(二分图匹配)

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1059

将行与列看成二分图的两个部分,发现交换行和交换列并不影响二分图,例如:

发现交换第一行和第三行不会影响二分图。

然后就随便写写.......

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 typedef unsigned long long ull;
 5 const int maxn = 205;
 6 int vis[maxn], Link[maxn], Nx, Ny;
 7 int mp[205][205];
 8 bool dfs(int x) {
 9     for (int i = 1; i <= Nx; i++) {
10         if (!vis[i]&&mp[x][i]) {
11             vis[i] = 1;
12             if (Link[i] == -1 || dfs(Link[i])) {
13                 Link[i] = x;
14                 return true;
15             }
16         }
17     }
18     return false;
19 }
20 bool xyl() {
21     int ans = 0;
22     memset(Link, -1, sizeof(Link));
23     for (int i = 1; i <= Nx; i++) {
24         memset(vis, 0, sizeof(vis));
25         if (dfs(i))
26             ans++;
27     }
28     return ans >= Nx;
29 }
30 int main() {
31     int t;
32     scanf("%d", &t);
33     while (t--) {
34         int n, x;
35         scanf("%d", &n);
36         memset(mp, 0, sizeof(mp));
37         for (int i = 1; i <= n; i++) {
38             for (int j = 1; j <= n; j++) {
39                 scanf("%d", &x);
40                 if (x)
41                     mp[i][j] = 1;
42             }
43         }
44         Nx = Ny = n;
45         if (xyl())
46             printf("Yes\n");
47         else
48             printf("No\n");
49     }
50     return 0;
51 }
View Code

 

posted @ 2019-07-03 14:38  祈梦生  阅读(117)  评论(0编辑  收藏  举报