hdu2444 判断二分图+最大匹配

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 210
int map[maxn][maxn],color[maxn];
int vis[maxn],match[maxn],n;
int bfs(int u,int n)
{
    int i;
    queue<int>q;
    q.push(u);
    color[u]=1;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(i=1;i<=n;i++)
        {
            if(color[i]==-1&&map[v][i])
            {
                q.push(i);
                color[i]=!color[v];
            }
            if(color[i]==color[v]&&map[v][i])
                return 0;
        }
    }
    return 1;
}
int dfs(int u)
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        if(map[u][i]&&!vis[i])
        {
            vis[i]=1;
            if(match[i]==-1||dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(color,-1,sizeof(color));
        memset(map,0,sizeof(map));
        memset(match,-1,sizeof(match));
        for(i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map[x][y]=map[y][x]=1;
        }
        int flag=1;
        for(i=1;i<=n;i++)
        {
            if(color[i]==-1&&!bfs(i,n))
            {
                flag=0;
                break;
            }
        }
        if(!flag)
        {
            printf("No\n");
            continue;
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",ans/2);
    }
}
时间: 2024-08-29 07:00:10

hdu2444 判断二分图+最大匹配的相关文章

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 判断二分图+最大匹配数

题目链接:http://acm.split.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 t

POJ2446(二分图最大匹配)

Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16924   Accepted: 5284 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2

hdu2444(判二分图+最大匹配)

传送门:The Accomodation of Students 题意:有n个学生,m对相互认识的,问能否分成两队,使得每对中没有相互认识的,如果可以求最大匹配,否则输出No. 分析:判断二分图用染色法,然后直接匈牙利算法求最大匹配. #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <al

hdu 2444 二分图判断与最大匹配

题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No. 首先判断是否为二分图,然后判断最大匹配 Sample Input 4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6Sample Output No 3 1 #include<iostream> 2 #include<string.h> 3 #i

hdu 2444 The Accomodation of Students 【二分图判断+求最大匹配】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=2444 题意:判断所有人是否分为两个集合,每个集合里的人互不相识. 思路:先判断是否为二分图,是的话求最大匹配,否则输出"No". 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #i

二分图最大匹配|UOJ#78|匈牙利算法|边表|Elena

#78. 二分图最大匹配 从前一个和谐的班级,有 nlnl 个是男生,有 nrnr 个是女生.编号分别为 1,-,nl1,-,nl 和 1,-,nr1,-,nr. 有若干个这样的条件:第 vv 个男生和第 uu 个女生愿意结为配偶. 请问这个班级里最多产生多少对配偶? 输入格式 第一行三个正整数,nl,nr,mnl,nr,m. 接下来 mm 行,每行两个整数 v,uv,u 表示第 vv 个男生和第 uu 个女生愿意结为配偶.保证 1≤v≤nl1≤v≤nl,1≤u≤nr1≤u≤nr,保证同一个条件

bzoj 1059: [ZJOI2007]矩阵游戏 [二分图][二分图最大匹配]

Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换操作:选择 矩阵的任意两行,交换这两行(即交换对应格子的颜色)列交换操作:选择矩阵的任意行列,交换这两列(即交换 对应格子的颜色)游戏的目标,即通过若干次操作,使得方阵的主对角线(左上角到右下角的连线)上的格子均为黑 色.对于某些关卡,小Q百思不得其解,以致他开始怀疑这些关卡是不是根

二分图最大匹配,最小路径覆盖,最小点覆盖,最大独立集,最小边覆盖与建图方法

转载请注明出处(别管写的好坏,码字也不容易):http://blog.csdn.net/hitwhacmer1 前言:         有自己写的,有摘的别人的,前面是摘的,也是无心整理,出错是难免的,反正我都不会证明,智人见智,别被我误导了. §1图论点.边集和二分图的相关概念和性质 点覆盖.最小点覆盖 点覆盖集即一个点集,使得所有边至少有一个端点在集合里.或者说是"点" 覆盖了所有"边"..极小点覆盖(minimal vertex covering):本身为点覆