hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)

//判断是否为二分图:在无向图G中,如果存在奇数回路,则不是二分图。否则是二分图。
//判断回路奇偶性:把相邻两点染成黑白两色,如果相邻两点出现颜色相同则存在奇数回路。也就是非二分图。
# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
int vis[210],map[210][210],cott[210];
int c[210];
int flag,n,m;
void dfs(int i,int color)//染色法判断是否是二分图
{
    for(int j=1; j<=n; j++)
    {
        if(map[i][j])
        {
            if(c[j]==0)
            {
                c[j]=-color;
                dfs(j,-color);
            }
            else if(c[j]==color)
            {
                flag=false;
                return ;
            }
            if(!flag)
                return ;
        }
    }
}
int check()
{
    flag=1;
    memset(c,0,sizeof(c));
    c[1]=1;//一号为黑色,与他相邻的都染为白色
    dfs(1,1);//从一号开始
    return flag;
}

int bfs(int x)
{
    for(int i=1; i<=n; i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=1;
            if(!cott[i]|bfs(cott[i]))
            {
                cott[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int a,b;
    while(~scanf("%d%d",&n,&m))
    {
        memset(map,0,sizeof(map));
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&a,&b);
            map[b][a]=map[a][b]=1;
        }
        if(!check())
        {
            printf("No\n");
        }
        else
        {
            int cot=0;
            memset(cott,0,sizeof(cott));
            for(int i=1; i<=n; i++)//以x集合为准找了一遍,又以y集合为准找了一遍,匹配数量增倍
            {
                memset(vis,0,sizeof(vis));
                if(bfs(i))
                    cot++;
            }
            printf("%d\n",cot/2);

        }

    }
    return 0;
}

hdu2444 The Accomodation of Students(判断二分匹配+最大匹配),布布扣,bubuko.com

时间: 2025-01-05 04:36:39

hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)的相关文章

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

二分匹配最大匹配 PKU1469

一个学生可以有多种选择,问能否每个学生刚好选一门课,但是每门课最多只有一个学生可以选择 典型的二分匹配最大匹配,直接套模板 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17878   Accepted: 7048 Description Consider a group of N students and P courses. Each student visits zero, one or more

ZOJ 1516 Uncle Tom&#39;s Inherited Land(二分匹配 最大匹配 匈牙利啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=516 Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncl

二分匹配最大匹配的理解(附图解)

定义一个PXP的有向图中,路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联:(如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次):如果不考虑图中存在回路,那么每条路径就是一个弱连通子集.由上面可以得出:1.一个单独的顶点是一条路径:2.如果存在一路径p1,p2,......pk,其中p1 为起点,pk为终点,那么在覆盖图中,顶点p1,p2,......pk不再与其它的顶点之间存在有向边.路径覆盖与二分图匹

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

【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 8 using namespace std; 9 const int maxn=2e2

(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

hdu 2444 The Accomodation of Students 判断时候构成二分图 + 最大匹配

此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足异或关系就结束.如果没被标记,就标记为相邻点的异或. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue>