zoukankan      html  css  js  c++  java
  • The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
    题意:判断给出的图是否是二分图,如果是,则求出最大匹配。
     
    解法:判断是否是二分图可以用染色法,标记相邻的点为不同颜色,如果标记途中发现要标记的两个点是相同颜色的,则说明这个图不是二分图。如果没遇到这种情况,就说明是二分图。
    还有一种判断方法,就是用并查集,具体可以参考:http://www.cnblogs.com/scaugsh/p/5533202.html
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <vector>
    #include <queue>
    using namespace std;
    int n,m;
    int f[500];
    int A[300],B[300],match[300],book[300];
    vector <int> V[300];//邻接表储存边的关系
    void init()
    {
        memset(match,0,sizeof(match));
        for (int i=1;i<=n;i++)
        V[i].clear();
        for (int i=1; i<=2*n; i++)
            f[i]=i;
    }
    int find(int x)
    {
        int r=x,i=x,t;
        while (r!=f[r]) r=f[r];
        while (i!=r)
        {
            t=f[i];
            f[i]=r;
            i=t;
        }
        return r;
    }
    void mix(int x,int y)
    {
        int fx=find(x),fy=find(y);
        f[fx]=fy;
    }
    bool IsTwo()//染色法求二分图
    {
        memset(book,0,sizeof(book));
        queue <int> Q;
        Q.push(1);
        book[1]=1;
        while (!Q.empty())
        {
            int temp=Q.front();
            Q.pop();
            for (int i=0;i<V[temp].size();i++)
            {
                int num=V[temp][i];
                if (book[num]==0)
                {
                    book[num]=-book[temp];
                    Q.push(num);
                }
                else if (book[num]==book[temp]) return 0;
            }
        }
        return 1;
    }
    int dfs(int u)
    {
        for (int i=0; i<V[u].size(); i++)
        {
            int pos=V[u][i];
            if (book[pos]==0)
            {
                book[pos]=1;
                if (match[pos]==0||dfs(match[pos]))
                {
                    match[pos]=u;
                    return  1;
                }
            }
        }
        return 0;
    }
    int solve()
    {
        int ans=0;
        for (int i=1; i<=n; i++)
        {
            memset(book,0,sizeof(book));
            if (dfs(i)) ans++;
        }
        return ans;
    }
    int main()
    {
        int a,b;
        while (scanf("%d%d",&n,&m)>0)
        {
            init();
            int ok=1;
            while (m--)
            {
                scanf("%d%d",&a,&b);
                if (find(a)==find(b))
                    ok=0;
                if (ok)
                {
                    mix(a,b+n);
                    mix(b,a+n);
                    V[a].push_back(b);
                    V[b].push_back(a);
    } }
    if (!ok) { puts("No"); continue; } /*if (!IsTwo()) { puts("No"); continue; }*/ printf("%d ",solve()/2); } return 0; }
     
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 相关阅读:
    2011年需要关注的9大编程语言 狼人:
    微软在华推广Win7拒绝“黑屏” 狼人:
    ifanr访谈:GuruDigger — Web工程师排排坐 狼人:
    10种破除网页设计师障碍的实用方法 狼人:
    英特尔CEO:微软Windows 7是PC更新的催化剂 狼人:
    Windows 7市场份额突破25% XP持续下滑 狼人:
    Office Web Apps中文版正式上线 狼人:
    机器学习实现线性梯度算实现octave
    管理系统数据库sql server 数据库管理
    缓存代码sencha Touch 缓存问题解析
  • 原文地址:https://www.cnblogs.com/scaugsh/p/5688728.html
Copyright © 2011-2022 走看看