ZOJ 3646 Matrix Transformer(二分图最大匹配)

Description

Alice and Bob meet again. This time they play a game named MATRIX TRANSFORMER.

 

They got an n * n board. Every grid has two positions, UP and DOWN. In this game you can push some amazing buttons to exchange any two rows or any two columns. Alice will win if she got the grids in the main diagonal line all UP.

 

But Alice finds that for some board, no matter how many times she tries, she cannot get the grids in the main diagonal line all UP. Now she asks you for help, tell her if she can win this board or not.

 

Input

There are several test cases. 

For each test case: 

The 1st line contains 1 integer n, indicating the size of the board. (1 ≤ n ≤ 200) 

The next n lines, each contains n characters. 'U' indicates the position UP, and 'D' indicates the position DOWN. 

There is no separation line between any two test cases.

 

Output

For each test case, you should print one line. You should print 'YES' if Alice can win, print 'NO' if not. 

Sample Input

3

DUD

UDD

DDU

3

DUD

DUD

UDD

Sample Output

YES

NO

 

大致题意为,给一个n行n列只有D和U的矩阵,问是否能作数次行交换或列交换,使得矩阵的主对角线上都是U

构图,每一行作为一个点,每一列作为一个点,对每一个U,其行和列建一条边,用匈牙利算法求最大匹配,若最大匹配为n,匹配边所对应的点的行和列应该能覆盖整个矩阵,此时作变换应该能得到主对角线上都是U的矩阵

 1 #include <cstdio> 
 2 #include <cstring> 
 3 #define MAXN 210 
 4 #define MAXM 210*200 
 5    
 6 int head[MAXN], next[MAXM], to[MAXM], vis[MAXN], link[MAXN]; 
 7 int ecnt; 
 8    
 9 void addEdge(int u, int v) 
10 { 
11     next[ecnt] = head[u]; head[u] = ecnt; to[ecnt++] = v; 
12 } 
13    
14 bool dfs(int u) 
15 { 
16     for(int v = head[u]; v; v = next[v]) 
17         if(!vis[to[v]]){ 
18         vis[to[v]] = true; 
19         if(!link[to[v]] || dfs(link[to[v]])){ 
20             link[to[v]] = u; 
21             return true; 
22         } 
23     } 
24     return false; 
25 } 
26    
27 int main() 
28 { 
29     int n; 
30     while(scanf("%d",&n) != EOF){ 
31         memset(head,0,sizeof(head)); 
32         memset(link,0,sizeof(link)); 
33         ecnt = 1; 
34         for(int i = 1; i <= n; ++i){ 
35             getchar(); 
36             for(int j = 1; j <= n; ++j){ 
37                 char c = getchar(); 
38                 if(c == 'U') addEdge(i,j); 
39             } 
40         } 
41         int ans = 0; 
42         for(int i = 1; i <= n; ++i){ 
43             memset(vis,0,sizeof(vis)); 
44             if(dfs(i)) ++ans; 
45             else break; 
46         } 
47         if(ans == n) printf("YES\n"); 
48         else printf("NO\n"); 
49     } 
50 }
View Code

 

转载于:https://www.cnblogs.com/oyking/archive/2013/06/04/3116668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值