zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Windows安全事件日志中的事件编号与描述
    Apache启动失败,请检查相关配置。MySQL5.1已启动成功
    scrapy
    python 与mongodb 交互
    mongo 的导入和导出
    MongoDB
    json字符串和字典的区别补充
    第七章:错误处理
    第六章:个人主页和头像
    第五章:用户登录
  • 原文地址:https://www.cnblogs.com/oyking/p/3116668.html
Copyright © 2011-2022 走看看