BZOJ1059: [ZJOI2007]矩阵游戏

1059: [ZJOI2007]矩阵游戏

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 5036  Solved: 2412
[Submit][Status][Discuss]

Description

  小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏。矩阵游戏在一个N
*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的)。每次可以对该矩阵进行两种操作:行交换操作:选择
矩阵的任意两行,交换这两行(即交换对应格子的颜色)列交换操作:选择矩阵的任意行列,交换这两列(即交换
对应格子的颜色)游戏的目标,即通过若干次操作,使得方阵的主对角线(左上角到右下角的连线)上的格子均为黑
色。对于某些关卡,小Q百思不得其解,以致他开始怀疑这些关卡是不是根本就是无解的!!于是小Q决定写一个程
序来判断这些关卡是否有解。

Input

  第一行包含一个整数T,表示数据的组数。接下来包含T组数据,每组数据第一行为一个整数N,表示方阵的大
小;接下来N行为一个N*N的01矩阵(0表示白色,1表示黑色)。

Output

  输出文件应包含T行。对于每一组数据,如果该关卡有解,输出一行Yes;否则输出一行No。

Sample Input

2
2
0 0
0 1
3
0 0 1
0 1 0
1 0 0

Sample Output

No
Yes
【数据规模】
对于100%的数据,N ≤ 200

HINT

Source

【题解】

手玩了几组数据,发现必须保证n个黑点的行列必须都不一样

显然法得证

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 
 6 inline void read(int &x)
 7 {
 8     x = 0;char ch = getchar(), c = ch;
 9     while(ch < '0' || ch > '9')c = ch, ch = getchar();
10     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
11     if(c == '-')x = -x; 
12 } 
13 
14 const int MAXN = 2000 + 10;
15 
16 int t,n,tmp,b[MAXN],lk[MAXN],g[MAXN][MAXN];
17 
18 int dfs(int u)
19 {
20     for(register int v = 1;v <= n;++ v)
21     {
22         if(!g[u][v] || b[v])continue;
23         b[v] = 1;
24         if(lk[v] == -1 || dfs(lk[v]))
25         {
26             lk[v] = u;
27             return 1; 
28         }
29     }
30     return 0;
31 }
32 
33 int xiongyali()
34 {
35     int ans = 0;
36     memset(lk, -1, sizeof(lk));
37     for(register int i = 1;i <= n;++ i)
38     {
39         memset(b, 0, sizeof(b));
40         if(!dfs(i))return 0;
41     }
42     return 1;
43 }
44 
45 int main()
46 {
47     read(t);
48     for(;t;--t)
49     {
50         read(n);
51         memset(g,0,sizeof(g));
52         for(register int i = 1;i <= n;++ i)
53             for(register int j = 1;j <= n;++ j) 
54             {
55                 read(tmp);
56                 if(tmp)g[i][j] = 1;
57             }
58         if(xiongyali())printf("Yes
");
59         else printf("No
");
60     }
61     return 0;
62 }
BZOJ1059