HDU2444(二分图判定+最大匹配)

The Accomodation of Students

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

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

题意:一些学生存在认识与不认识的关系,但这种关系不传递。问能否将学生分为两组,每组中的学生互相不认识(即二分图判定)。若可以,把相互认识的同学放在一个双人间,问最多需要几个双人间(即二分图最大匹配)。

#include"cstdio"
#include"cstring"
#include"vector"
#include"algorithm"
using namespace std;
const int MAXN=205;
vector<int> G[MAXN];
int V,E;
bool bfs(int s)//二分图判断
{
    int que[MAXN],rear=0,front=0;
    int color[MAXN];
    memset(color,-1,sizeof(color));
    color[s]=1;que[rear++]=s;
    while(rear!=front)
    {
        int v=que[front++];
        for(int i=0;i<G[v].size();i++)
        {
            int u=G[v][i];
            if(color[u]==-1)
            {
                color[u]=!color[v];//若没有染色则染成相反颜色
                que[rear++]=u;
            }
            else if(color[u]==color[v])    return false;//若相邻结点颜色相同则不是二分图
        }
    }
    return true;
}
int match[MAXN];
int vis[MAXN];
bool dfs(int u)//二分图匹配
{
    vis[u]=1;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i],w=match[v];
        if(w==0||(!vis[w]&&dfs(w)))
        {
            match[u]=v;
            match[v]=u;
            return true;
        }
    }
    return false;
}
int matching()
{
    memset(match,0,sizeof(match));
    int ans=0;
    for(int i=1;i<=V;i++)
    {
        if(!match[i])
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))    ans++;
        }
    }
    return ans;
}
int main()
{
    while(scanf("%d%d",&V,&E)!=EOF)
    {
        for(int i=1;i<=V;i++)    G[i].clear();
        for(int i=0;i<E;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        if(bfs(1))    printf("%d\n",matching());
        else    printf("No\n");
    }
    return 0;
}
时间: 2024-08-05 17:43:51

HDU2444(二分图判定+最大匹配)的相关文章

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

题目来源:HDU 2444 The Accomodation of Students 题意:n个人是否可以分成2组 每组的人不能相互认识 就是二分图判定 可以分成2组 每组选一个2个人认识可以去一个双人间 最多可以有几组 思路:二分图判定+最大匹配 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 550; int vis[maxn];

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

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 o

hdu2444 二分图的匹配,先判断是否为二分图

http://acm.hdu.edu.cn/showproblem.php?pid=2444 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

HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

本题就是先判断是否可以组成二分图,然后用匈牙利算法求出最大匹配. 到底如何学习一种新算法呢? 我也不知道什么方法是最佳的了,因为看书本和大牛们写的匈牙利算法详细分析,看了差不多两个小时没看懂,最后自己直接看代码,居然不到半个小时看懂了.然后就可以直接拿来解题啦. 比如topcoder上有这个算法的很详细的分析,真没看懂. 代码居然比分析更清晰了?我也不好下结论. 但是我觉得主要的思想还是有作用的. 说说我对这个算法的理解吧: 1 假设二分图分为两个集合 U, V,那么从一个集合U出发 2 U的一

hdu 5285 wyh2000 and pupil(二分图判定)

对每两个不认识的人连一条边,则此题可转化为二分图判定(二分图可有多个). 如果有一部分图判定为不是二分图,则输出“Poor wyh”. 否则,分别累加每个二分图的最多的颜色数. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <cmat

二分图判定 nyoj1015(模板)

题目:点击打开链接nyoj1015 分析:题意很清楚,就是让判断一个图是不是二分图,思路当然就是染色法,首先给一个顶点然色,然后与它相邻的顶点全部染相反的颜色,如果过程中发现要染的点已经染色了,而且是和现在点相同的颜色的话,那么就说明不是一个二分图. 其实就是广搜模板 #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector&g

UVA 11080 - Place the Guards(二分图判定)

UVA 11080 - Place the Guards 题目链接 题意:一些城市.之间有道路相连,如今要安放警卫,警卫能看守到当前点周围的边,一条边仅仅能有一个警卫看守,问是否有方案,假设有最少放几个警卫 思路:二分图判定,判定过程记录下白点和黑点个数,小的就是要安放的个数,注意假设是0,那么应该是加1 代码: #include <cstdio> #include <cstring> #include <vector> using namespace std; con

二分图判定【图的搜索】

二分图判定 给定一个图,要给图上每个顶点染色,并且要使得相邻的顶点颜色不同.问是否能最多用2种颜色进行染色? 参考如下: Source Code: #include <iostream> #include <vector> using namespace std; vector<int> Edge[1010]; int colorArr[1010]; void initColorArr(int length){ for(int i=0;i<=length;++i)

hdu3729 I&#39;m Telling the Truth (二分图的最大匹配)

http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1427    Accepted Submission(s): 719 Problem Description After this year’s col