The Accomodation of Students

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

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
 

题目:一些学生之间是朋友关系(关系不能传递),现在要将一堆学生分成两堆,使得在同一堆的学生之间没有朋友关系。如果不可以输出“No”,可以的话输出最多可以分出几对小盆友。

思路:

我们先二分图染色,若能被染成两部分的话说明可以被分成两部分,然后再在我们分出的图上面跑最大匹配。若不能被染成两部分直接输出no

代码:

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 510
using namespace std;
bool flag,vis[N];
int n,m,x,y,tot,ans,col[N],girl[N],head[N],map[N][N];
queue<int>q;
struct Edge
{
    int from,to,next;
}edge[N*N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int find(int x)
{
    ;i<=n;i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=true;
            ||find(girl[i])){girl[i]=x; ;}
        }
    }
    ;
}
int color(int s)
{
    queue<int>q;
    q.push(s); col[s]=;
    while(!q.empty())
    {
        int x=q.front();
        for(int i=head[x];i;i=edge[i].next)
        {
            int t=edge[i].to;
            ){;}}
            else
            {
                col[t]=col[x]^;
                q.push(t);
            }
        }
        q.pop();
    }
    ;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=;flag=;
        memset(map,,sizeof(map));
        memset(col,-,sizeof(col));
        memset(edge,,sizeof(edge));
        memset(head,,sizeof(head));
        ;i<=m;i++)
        {
            x=read(),y=read();
            map[x][y]=;
            add(x,y),add(y,x);
        }
        ;i<=n;i++)
         )
         {
             if(color(i)) break;
          }
        if(flag) {printf("No\n"); continue;}
        memset(girl,-,sizeof(girl));
        ;i<=n;i++)
        {
            memset(vis,,sizeof(vis));
            if(find(i)) ans++;
        }
        printf("%d\n",ans);
    }
    ;
}

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

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

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

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

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

  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 StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

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

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

  7. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  8. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  9. hdu 2444 The Accomodation of Students 【二分图匹配】

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

随机推荐

  1. mybatis获取存储过程返回结果

    获取存储过程返回结果 代码: // Map<String,Object> map = new HashMap<String,Object>(); map.put("i ...

  2. faster rcnn训练过程讲解

    http://blog.csdn.net/u014696921/article/details/60321425

  3. ORA-03113: end-of-file on & ORA-07445

    --------------ORA-03113: end-of-file on-------------- SQL> show parameter background_dump; NAME T ...

  4. intellij idea关闭重复代码提醒

  5. 【传智播客】Libevent学习笔记(二):创建event_base

    目录 00. 目录 01. 简介 02. 创建默认的event_base 03. 创建复杂的event_base 3.1 event_config_new函数 3.2 event_base_new_w ...

  6. ios 封装sqllite3接口

    2013-03-25 17:41 261人阅读 评论(0) 收藏 举报 // #import <Foundation/Foundation.h> #import "sqlite3 ...

  7. 文本三剑客之awk

    awk和流编辑器sed在工作原理和用法上有很多类似之处,它们都是检查输入数据中的行是否匹配指定的模式,如果匹配成功就对匹配的行执行相应的操作,重复这个过程直到所有的输入数据都被处理完,因此awk和se ...

  8. Go:json(序列化、反序列化)

    一.示例 package main import ( "encoding/json" "fmt" ) type Person struct { Name str ...

  9. CSS3---渲染属性

    1.计数器 CSS3计数器( CSS Counters )可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能.与有序列表相比,它的突出特性在于可以对任意元素计数,同时实现个性化 ...

  10. PHP调用webService WSDL 接口发送邮件

    1.什么是 webService WSDL?  webService WSDL 暴露一些接口给第三方调用,在底层会转化成一个HTTP请求,主要是不同语言之间为了通讯的一个协议,比如发送邮件的系统是用J ...