# BZOJ 1059 矩阵游戏 二分图匹配

https://www.lydsy.com/JudgeOnline/problem.php?id=1059

*N黑白方阵进行（如同国际象棋一般，只是颜色是随意的）。每次可以对该矩阵进行两种操作：行交换操作：选择

 1 #include<bits/stdc++.h>
2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数，会超时
4 #define Min(a, b) ((a) < (b) ? (a) : (b))
5 #define Mem(a) memset(a, 0, sizeof(a))
6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
8 #define lson ((o)<<1)
9 #define rson ((o)<<1|1)
11 using namespace std;
13 {
14     int x=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
17     return x*f;
18 }
19
20 typedef long long ll;
21 const int maxn = 500 + 10;
22 const int MOD = 1000000007;//const引用更快，宏定义也更快
23 const int INF = 1e9 + 7;
24 const double eps = 1e-6;
25 int Map[maxn][maxn];
26 int cx[maxn], cy[maxn];
27 bool vis[maxn];
28 int n;
29 bool dfs(int u)
30 {
31     for(int v = 1; v <= n; v++)
32         if(Map[u][v] && !vis[v])
33         {
34             vis[v] = 1;
35             if(cy[v] == -1 || dfs(cy[v]))
36             {
37                 cx[u] = v;
38                 cy[v] = u;
39                 return 1;
40             }
41         }
42     return 0;
43 }
44 int maxmatch()
45 {
46     int ans = 0;
47     memset(cx, -1, sizeof(cx));
48     memset(cy, -1, sizeof(cy));
49     for(int i = 1; i <= n; i++)
50     {
51         if(cx[i] == -1)
52         {
53             memset(vis, 0, sizeof(vis));
54             ans += dfs(i);
55         }
56     }
57     return ans;
58 }
59
60 int main()
61 {
62     int T;
63     scanf("%d", &T);
64     while(T--)
65     {
66         memset(Map, 0, sizeof(Map));
67         scanf("%d", &n);
68         int x;
69         for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)
70         {
71             scanf("%d", &x);
72             if(x)Map[i][j] = 1;
73         }
74         if(maxmatch() == n)puts("Yes");
75         else puts("No");
76     }
77     return 0;
78 }

posted @ 2018-09-19 22:31  _努力努力再努力x  阅读(146)  评论(0编辑  收藏  举报