bzoj1059: [ZJOI2007]矩阵游戏

二分图最大匹配。给一个01矩阵,可移动行列,问是否能让左上到右下的对角线全为1。行列为点,直接跑即可。忘了return false;WA了一次

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define qwq(x) for(edge *o=head[x];o;o=o->next)
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return x;
}
const int nmax=205;
struct edge{
	int to;edge *next;
};
edge edges[nmax*nmax],*pt,*head[nmax<<1];
bool vis[nmax<<1];int match[nmax<<1];
void add(int s,int t){
	pt->to=t;pt->next=head[s];head[s]=pt++;
}
bool dfs(int x){
	qwq(x){
		int to=o->to;
		if(vis[to]) continue;
		vis[to]=true;
		if(match[to]==-1||dfs(match[to])){
			match[to]=x;return true;
		}
	}
	return false;
}
bool solve(int n){
	int ans=0;
	rep(i,1,n) {
		clr(vis,0);
		if(dfs(i)) ans++;
		else break;
	}
	return ans==n;
}
void init(){
	clr(head,0);pt=edges;clr(match,-1);
}
int main(){
	int cas=read(); 
	rep(i,1,cas){
		init();
		int n=read();
		rep(j,1,n) rep(k,1,n) {
			int tmp=read();
			if(tmp==1) add(j,n+k);
		}
		if(solve(n)) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

  

1059: [ZJOI2007]矩阵游戏

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3732  Solved: 1799
[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

 
[Submit][Status][Discuss]
posted @ 2016-08-01 13:52  BBChq  阅读(196)  评论(0编辑  收藏  举报