Hdu_2444 The Accomodation of Students -二分图判断+最大匹配

题意:先判断学生能否分成两块互不认识的集体,然后找出这两块集体的最大匹配

吐槽:迷之WA了几次,后来把判断是否是二分图弄成函数写在外面就ok了。

/************************************************
Author        :DarkTong
Created Time  :2016/8/1 13:02:46
File Name     :Hdu_2444.cpp
*************************************************/

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int maxn = 200 + 10;
int w[maxn][maxn], n, m;
int Left[maxn];
bool used[maxn];
bool match(int i)
{
    for(int j=1;j<=m;++j) if(w[i][j]&&!used[j])
    {
        used[j] = true;
        if(!Left[j]||match(Left[j]))
        {
            Left[j] = i;
            return true;
        }
    }
    return false;
}
//返回最大匹配数
int hungary()
{
    int res=0;
    memset(Left, 0, sizeof(Left));
    for(int i=1;i<=n;++i)
    {
        memset(used, 0, sizeof(used));
        if(match(i)) res++;
    }
    return res;
}
int vis[maxn];
bool bfs()
{
    memset(vis, 0, sizeof(vis));
    for(int i=1;i<=n;++i)
    {
        if(!vis[i])
        {
            queue<int> q;
            q.push(i);
            vis[i] = 1;
            while(!q.empty())
            {
                int t = q.front(); q.pop();
                for(int j=1;j<=n;++j)
                {
                    if(w[t][j])
                    {
                        if(vis[j]==vis[t]) return false;
                        else if(vis[j]==0)
                        {
                            vis[j] = - vis[t];
                            q.push(j);
                        }
                    }
                }
            }
        }
    }
    return true;
}

int main()
{
    int T, cas=1, k;
    while(scanf("%d%d", &n, &k)==2)
    {
        memset(w, 0, sizeof(w));
        memset(vis, 0, sizeof(vis));
        m=n;

        int u, v;
        for(int i=1;i<=k;++i)
        {
            scanf("%d%d", &u, &v);
            w[v][u] = w[u][v]=1;
        }

        if(!bfs())
        {
            puts("No");
            continue;
        }

        int ans = hungary();
        printf("%d\n", ans>>1);
    }

    return 0;
}

时间: 2024-08-01 10:24:36

Hdu_2444 The Accomodation of Students -二分图判断+最大匹配的相关文章

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];

hdu2444The Accomodation of Students【判断二分图+最大匹配】

我觉得有必要粘一下英文: The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2502    Accepted Submission(s): 1190 Problem Description There are a group of students. Some of them may

HDU 2444 The Accomodation of Students(判断是否是二分图)

题目链接 题意:n个学生,m对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多需要多少个房间. 是否能分成两组?也就是说判断是不是二分图,判断二分图的办法,用染色法 把初始点染成黑色,然后与之相连的染成白色,重复,使路径黑白相间, 如果当前点的颜色和与他相连点的颜色相同时,则说明这个图不是二分图 求最多需要多少个房间?也就是求最大匹配数. #include <iostream> #include <cstdio> #include <

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

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

HDU 2444 The Accomodation of Students (二分图判定,二分图匹配,匈牙利算法)

题意:有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是否为二分图,可以用黑白着色法(DFS或BFS都行).若是二分图,再进行匹配,用匈牙利算法,注:给的是整个图,没有区分男女,用邻接表比较好. 1 #include <bits/stdc++.h> 2 #define LL long long 3 using namespace std; 4 const

HDU 2444 The Accomodation of Students(判断是否可图 + 二分图)

题目大意:有一群人他们有一些关系,比如A认识B, B认识C, 但是这并不意味值A和C认识.现在给你所有互相认识的学生,你的任务是把所有的学生分成两个一组, 住在一个双人房里.相互认识的同学可以住在一个双人房里. 输入数据: 有n个学生 m个关系(m对是相互认识的) 接下来m行是,是m个关系. 如果能够匹配成功则输出需要双人房的个数,否则输出'No' 思路:先判断是否是个二分图,可以使用黑白染色的方法来判断.然后再进行最大匹配. #include<stdio.h> #include<str

HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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

hdu2444The Accomodation of Students (最大匹配+判断是否为二分图)

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2244 Accepted Submission(s): 1056 Problem Description There are a group of students. Some of them may know each other,

hdu 2444 The Accomodation of Students 判断是否为二分图+最大匹配

The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3042    Accepted Submission(s): 1425 Problem Description There are a group of students. Some of them may know each o