zoukankan      html  css  js  c++  java
  • Swap(二分图的最大匹配)

    Swap

     HDU - 2819 

    Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. Can you find a way to make all the diagonal entries equal to 1?

    InputThere are several test cases in the input. The first line of each test case is an integer N (1 <= N <= 100). Then N lines follow, each contains N numbers (0 or 1), separating by space, indicating the N*N matrix.OutputFor each test case, the first line contain the number of swaps M. Then M lines follow, whose format is “R a b” or “C a b”, indicating swapping the row a and row b, or swapping the column a and column b. (1 <= a, b <= N). Any correct answer will be accepted, but M should be more than 1000. 

    If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”. 
    Sample Input

    2
    0 1
    1 0
    2
    1 0
    1 0

    Sample Output

    1
    R 1 2
    -1
    题意:交换图的某些行或者是某些列,使得这个N*N的图对角线上全部都是1。
    题解:首先我们需要明白一个道理,如果通过交换某些行没有办法的到解的话,那么只交换列或者既交换行又交换列那也没办法得到解。为了解这道题,我们需要构造二分图,第一部分X表示的是横坐标,第二部分Y表示纵坐标,如果mp[i][j]==1.那我们就从X的i向Y的j引一条边,那么这条边的含义就可以解释为可以将Y的第j列(因为Y表示的是列的集合)移到第i列,使得a[i][i]变成1,这样就相当于是第i行第i列就变成了1,也就是说对角线多了一个1。这个地方一定要注意理解mp[i][j]的含义!!
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=1100;
    struct node{
        int st;
        int ed;
    }e[maxn];
    int used[maxn];
    int a[maxn],b[maxn];
    int match[maxn],mp[maxn][maxn];
    int vis[maxn][maxn];
    int x;
    int n,cnt;
    int dfs(int u)
    {
        int i;
    
        for(i=1;i<=n;i++)
        {
            if(mp[u][i]&&!used[i])
            {
                used[i]=1;
                if(match[i]==-1||dfs(match[i]))
                {
                    match[i]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int hungary()
    {
        int ans=0,i;
    
        memset(match,-1,sizeof(match));
        for(i=1;i<=n;i++)
        {
            memset(used,0,sizeof(used));
            if(dfs(i))
                ans++;
        }
        return ans;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(vis,0,sizeof(vis));
            memset(mp,0,sizeof(mp));
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&x);
                    if(x==1)
                    {
                        mp[i][j]=1;
                    }
                }
            }
            int ans=hungary();
    
            if(ans<n)
                printf("-1
    ");
            else
            {
                int cnt = 0, a[maxn]={0}, b[maxn]={0};
            for(int i=1; i<=n; i++)
            {
                while(i != match[i])
                {
                    a[cnt] = i;
                    b[cnt] = match[i];
                    swap(match[i],  match[match[i]]);
                    cnt ++;
                }
            }
    
            printf("%d
    ", cnt);
            for(int i=0; i<cnt; i++)
                printf("C %d %d
    ", a[i], b[i]);
            }
    
        }
    }
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=1100;
    struct node{
        int st;
        int ed;
    }e[maxn];
    int used[maxn];
    int a[maxn],b[maxn];
    int match[maxn],mp[maxn][maxn];
    int vis[maxn][maxn];
    int x;
    int n,cnt;
    
    int dfs(int u)
    {
        int i;
    
        for(i=1;i<=n;i++)
        {
            if(mp[u][i]&&!used[i])
            {
                used[i]=1;
                if(match[i]==-1||dfs(match[i]))
                {
                    match[i]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int hungary()
    {
        int ans=0,i;
    
        memset(match,-1,sizeof(match));
        for(i=1;i<=n;i++)
        {
            memset(used,0,sizeof(used));
            if(dfs(i))
                ans++;
        }
        return ans;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(vis,0,sizeof(vis));
            memset(mp,0,sizeof(mp));
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&x);
                    if(x==1)
                    {
                        mp[j][i]=1;
                    }
                }
            }
            int ans=hungary();
    
            if(ans<n)
                printf("-1
    ");
            else
            {
                int cnt=0;
                memset(a,0,sizeof(a));
                memset(b,0,sizeof(b));
                for(int i=1;i<=n;i++)
                {
                    while(i!=match[i])
                    {
                        a[cnt]=i;
                        b[cnt]=match[i];
                        swap(match[i],match[match[i]]);
                        cnt++;
                    }
                }
                printf("%d
    ",cnt);
                for(int i=0;i<cnt;i++)
                {
                    printf("R %d %d
    ",a[i],b[i]);
                }
            }
    
        }
    }
     
  • 相关阅读:
    CentOS进程资源占用高原因分析命令
    Centos下修改启动项和网络配置
    CentOS查看系统信息命令和方法
    [vim]设置vim语法高亮显示和自动缩进
    [vim]vim中有中文乱码
    setState回调
    服务器安装nginx
    小程序map
    后台合成图片
    阿里云服务器添加nginx
  • 原文地址:https://www.cnblogs.com/1013star/p/9794769.html
Copyright © 2011-2022 走看看