zoukankan      html  css  js  c++  java
  • hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    Problem Description
    There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

    Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

    Calculate the maximum number of pairs that can be arranged into these double rooms.
     
    Input
    For each data set:
    The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

    Proceed to the end of file.

     
    Output
    If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
     
    Sample Input
    4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
     
    Sample Output
    No 3
     
    Source
    用染色法,即从其中一个顶点开始,将跟它邻接的点染成与其不同的颜色,如果邻接的点有相同颜色的,则说明不是二分图,每次用bfs遍历即可。
    二分匹配:套版子;
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    #define bug(x) cout<<"bug"<<x<<" "<<endl;
    const int N=2e2+10,M=1e6+10,inf=2e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int n,m;
    int mp[N][N];
    int linker[N];
    bool used[N];
    bool dfs(int a)
    {
        for(int i=0;i<n;i++)
          if(mp[a][i]&&!used[i])
          {
              used[i]=true;
              if(linker[i]==-1||dfs(linker[i]))
              {
                  linker[i]=a;
                  return true;
              }
          }
          return false;
    }
    int hungary()
    {
        int result=0;
        memset(linker,-1,sizeof(linker));
        for(int i=0;i<n;i++)
        {
            memset(used,0,sizeof(used));
            if(dfs(i))  result++;
        }
        return result;
    }
    queue<int>q;
    int color[N];
    int bfs(int s)
    {
        while(!q.empty())q.pop();
        q.push(s);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=0;i<n;i++)
            {
                if(mp[x][i])
                {
                    if(color[i]==-1)
                        color[i]=color[x]^1,q.push(i);
                    else if(color[i]==color[x])
                        return 0;
                }
            }
        }
        return 1;
    }
    int check()
    {
        memset(color,-1,sizeof(color));
        for(int i=0;i<n;i++)
        {
            if(color[i]==-1)
            {
                color[i]=1;
                if(!bfs(i))return 0;
            }
        }
        return 1;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(mp,0,sizeof(mp));
            for(int i=1;i<=m;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                mp[u-1][v-1]=1;
            }
            if(!check())printf("No
    ");
            else
            {
                int cnt=hungary();
                printf("%d
    ",cnt);
            }
        }
        return 0;
    }
  • 相关阅读:
    JBoss目录结构说明
    Velocity简介
    如何把文件上传到另外一台服务器
    控制GridView列显示
    CheckBoxList取值及勾选上
    Email发送中不允许使用邮箱名称原因
    关于URL重写的一点心得
    修改数据库所有者
    验证CheckBoxList至少要有一项选中
    SQL中字符串处理(分割及反分割)
  • 原文地址:https://www.cnblogs.com/jhz033/p/6412989.html
Copyright © 2011-2022 走看看