染色判读二分图+Hungary匹配

The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1705    Accepted Submission(s): 821

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
 

Recommend
gaojie
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

typedef struct
{
    int to,next;
}Edge;

Edge E[50000];
int Adj[50000],Size=0,color[500],n,m,from[500];
bool use[500];

void Init()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v)
{
    E[Size].to=v;
    E[Size].next=Adj;
    Adj=Size++;
}

bool match(int x)
{
    for(int i=Adj[x];~i;i=E.next)
    {
        int v=E.to;
        if(!use[v])
        {
            use[v]=true;
            if(from[v]==-1||match(from[v]))
            {
                from[v]=x;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int sum=0;
    memset(from,-1,sizeof(from));
    for(int i=1;i<=n;i++)
    {
        if(color==1)
        {
            memset(use,false,sizeof(use));
            sum+=match(i);
        }
    }
    return sum;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u,v;
        Init();
        while(m--)
        {
            scanf("%d%d",&u,&v);
            Add_Edge(u,v);
            Add_Edge(v,u);
        }
        memset(color,0,sizeof(color));
        bool flag=true;
while(true)
{
        int pos=-1;
        if(flag==false) break;
        for(int i=1;i<=n;i++)
        {
            if(Adj==-1) continue;
            else if(color!=0) continue;
            else
            {
                pos=i; break;
            }
        }
        if(pos==-1) break;
        queue<int> q;
        q.push(pos); color[pos]=1;
        //printf("%d: \n",pos);
        while(!q.empty())
        {
            if(flag==false) break;
            int u=q.front(); q.pop();
            for(int i=Adj;~i;i=E.next)
            {
                int v=E.to;
                //printf("%d--->%d\n",u,v);
                if(color[v]==0)
                {
                    color[v]=-color;
                    q.push(v);
                }
                else if(color[v]==color)
                {
                    flag=false; break;
                }
            }
        }
}
        if(flag==false)
        {
            puts("No");
        }
        else
        {
            printf("%d\n",hungary());
        }
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 2444 The Accomodation of Students的更多相关文章

  1. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  2. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  3. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. HDU 2444 The Accomodation of Students(判断二分图+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  5. HDU——2444 The Accomodation of Students

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. hdu 2444 The Accomodation of Students (判断二分图,最大匹配)

    The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. 【HDOJ】2444 The Accomodation of Students

    图论的题目.着色原理+二分图匹配. #include <cstdio> #include <cstring> #define MAXN 205 char map[MAXN][M ...

  9. (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...

随机推荐

  1. Android知识体系图

    网上看到,不知哪位大神总结的,存个档(需要放大网页才能看清)

  2. iOS设置圆角矩形和阴影效果

    1.设置圆角矩形 //设置dropview属性 _dropView.backgroundColor=[[UIColor whiteColor] colorWithAlphaComponent:0.8] ...

  3. HTML5学习总结-05 HTML5表单

    一 HTML5 新的类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. email url number range Date pickers (date, month ...

  4. POJ3659 [usaco2008jan_gold]电话网络

    校内OJ上的题,刚开始做的时候以为是道SB题10分钟就能搞完.. 然后准备敲了才发现自己是个SB.. 刚开始以为是个很裸的TreeDP,然后就只设了两个状态,但是怎么想怎么不对.复杂度好像要爆炸.改成 ...

  5. Linux Canbus调试笔记

    STM32之CAN---错误管理分析      牛人博客 http://blog.csdn.net/flydream0/article/details/8161418 CAN总线在嵌入式Linux下驱 ...

  6. 安装Virtual Box增强功能 - Ubuntu

    一.开发环境 操作系统:Windows 7Virtual Box 版本: 5.0.10 虚拟机系统: Ubuntu 12.04 LTS 二.问题 进入Ubuntu图形界面后,选择“设备” --> ...

  7. c#多态之抽象类与虚方法的异同点~

    多态之抽象类与虚方法的相似点及不同点 : 不同点 1.方法关键字不一样 虚方法的方法关键字是:virtual. 抽象类的方法关键词是:abstract 2.基类的方法是否有方法体/实现 虚方法的方法: ...

  8. c# 操作datatable

    1.创建 datatable DataTable dt=new Datable();// 可以给表创建一个名字,tb 2.给表加个列名: dt.Columns.Add("id", ...

  9. C#验证子网掩码的正确性

    1.       IP合法关于IP地址的合法性验证很简单,方法也很多,比如字符串分解.正则表达式等. 2.       子网掩码是否合法简单来讲,子网掩码就类似这样一串数字,前面一段是连续的1, 类似 ...

  10. Sort 整理

    文章.图片参考:http://www.jianshu.com/p/1b4068ccd505?hmsr=toutiao.io&utm_medium=toutiao.io&utm_sour ...