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

【题目链接】:click here~~

【题目大意】:

给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识。若不能分成两组输出No,否则输出两组间俩人互相认识的对数

【解题思路】:   先推断是否能构成二分图,推断二分图用交叉染色法:从某个未染色的点出发把此点染成白色,该点周围的点染成黑色。黑色周围的又染成白色。若走到某个点已经染色,而且它相邻点的颜色与它一样则不是二分图,能够这样理解,染白色既增加X集合,黑色既增加Y集合,若某个点即是X集合又是Y集合,那说明不是二分图,推断二分图之后,再求最大的匹配数。PS:二分图是无向图时最大匹配数是Sum/2。

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?

a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))

int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
int movv[5][2]= {{1,0},{0,1},{0,0},{-1,0},{0,-1}};

using namespace std;

typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int inf=0x3f3f3f3f;
const int N =205;

int head[N],link[N];///邻接表
bool vis[N],col[N];
int cnt,n,m;

inline LL read()
{
    int c=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){c=c*10+ch-‘0‘;ch=getchar();}
    return c*f;
}

struct edge{
    int to;
    int next;
}g[N*N];

void init(){
    cnt=0;
    mem(head,-1);
    mem(col,0);
}

void add_edge(int u,int v){
    g[cnt].to=v;
    g[cnt].next=head[u];
    head[u]=cnt++;
}

bool color(int u){ ///染色
  for(int i=head[u]; i!=-1; i=g[i].next){
    int v = g[i].to;
    if(!col[v]){
        col[v] = !col[u];
        if(!color(v)) return false;
    }
    else if(col[v]==col[u]) return false;
  }
  return true;
}

bool dfs(int u) ///匈牙利算法dfs实现
{
    for(int i=head[u]; i!=-1; i=g[i].next){///元素集合个数
        int v=g[i].to;
        if(!vis[v]){
            vis[v]=1;
            if(link[v]== -1|| dfs(link[v])){
                link[v]=u;
                return true;
            }
        }
    }
    return false;
}

int match()   ///最大匹配
{
    int ans = 0;
    mem(link,-1);
    for(int i=1; i<=n; ++i){
        mem(vis,0);
        if(dfs(i)) ans++;
    }
    return ans;
}

int main()
{
    while(~scanf("%d%d",&n,&m)){
        if(n==1){
            puts("No");
            continue;
        }
        init();
        while(m--){
            int u,v;
            u=read(),v=read();
            add_edge(u,v);
            add_edge(v,u);
        }
        col[1]=1;
        if(!color(1)) puts("No");
        else printf("%d\n",match()>>1);
    }
    return 0;
}
时间: 2024-08-03 23:47:06

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

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

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2444 Description There are a group of students. Some of them may know

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

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

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

The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8939    Accepted Submission(s): 3925 Problem DescriptionThere are a group of students. Some of them may know each othe

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) Total Submission(s): 7086    Accepted Submission(s): 3167 Problem Description There are a group of students. Some of them may know each o