Hdu2444二分图

给你一个图,问是否为二分图,若是求出最大匹配。

并查集判图,原理黑白染色。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;

int n,m;
const int maxn = 555;
int link[maxn];
int used[maxn];
int Map[maxn][maxn];
int father[maxn*2];
int getfather(int x)
{
    if(father[x]!=x) father[x]=getfather(father[x]);
    return father[x];
}

int dfs(int x)
{
    for(int i=1;i<=n;i++){
        if(Map[x][i]&&!used[i]){
            used[i]=1;
            if(link[i]==-1||dfs(link[i])){
                link[i]= x; return 1;
            }
        }
    }
    return 0;
}

int gao()
{
    int ans=0;
    memset(link,-1,sizeof(link));
    for(int i=1;i<=n;i++){
        memset(used,0,sizeof(used));
        ans+=dfs(i);
    }
    return ans;
}

int main()
{
    int a,b;
    while(cin>>n>>m){
        memset(Map,0,sizeof(Map));
        for(int i=1;i<=2*n;i++)
        father[i]= i;
        int flag=0;
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            int fa=getfather(a);int fb= getfather(b); int fa1= getfather(a+n);int fb1= getfather(b+n);
            if(fa==fb) flag=1;
            father[fa]=fb1;father[fb]=fa1;
            Map[a][b]=1;Map[b][a]=1;
        }
        if(flag){
            printf("No\n");continue;
        }
        int t= gao();
        printf("%d\n",t/2);
    }
    return 0;
}

bfs判图

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;

int n,m;
const int maxn = 222;
int link[maxn];
int used[maxn];
int Map[maxn][maxn];
int Dye(int x)
{
    int vis[maxn];int color[maxn];
    memset(vis,0,sizeof(vis));
    memset(color,-1,sizeof(color));
    queue<int> q;
    q.push(x);
    vis[x]=1; color[x]= 1;
    while(!q.empty()){
        int cur=q.front(); q.pop();
        for(int i=1;i<=n;i++){
            if(!Map[cur][i])continue;
            if(color[i]==color[cur]) return 0;
            color[i]=color[cur]^1;
            if(!vis[i]){
                vis[i]=1 ;q.push(i);
            }
        }
    }
    return 1;
}

int dfs(int x)
{
    for(int i=1;i<=n;i++){
        if(Map[x][i]&&!used[i]){
            used[i]=1;
            if(link[i]==-1||dfs(link[i])){
                link[i]= x; return 1;
            }
        }
    }
    return 0;
}

int gao()
{
    int ans=0;
    memset(link,-1,sizeof(link));
    for(int i=1;i<=n;i++){
        memset(used,0,sizeof(used));
        ans+=dfs(i);
    }
    return ans;
}

int main()
{
    int a,b;
    while(cin>>n>>m){
        memset(Map,0,sizeof(Map));
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            Map[a][b]=1;Map[b][a]=1;
        }
        int flag=0;
        for(int i=1;i<=n;i++)
            if(!Dye(i)) flag=1;
        if(flag){
            printf("No\n");continue;
        }
        int t= gao();
        printf("%d\n",t/2);
    }
    return 0;
}
时间: 2024-08-28 03:11:46

Hdu2444二分图的相关文章

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

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 ot

hdu2444二分图最大匹配+判断二分图

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

HDU2444 二分图

题意:有n个学生,他们之间可能互相认识.先判断是否可以分成两组,每组的学生互相都不认识,如果不能输出“No”.如果可以,每次从两组各拿出一个相互认识的学生组成一对,输出最多可以有多少对. 例如: 第一组数据: 4 4 1 2 1 3 1 4 2 3 由于1和其他所有学生都认识,而其它学生又有互相认识的,肯定不能分成两组了. 第二组数据: 6 5 1 2 1 3 1 4 2 5 3 6 可以分成下图的两组学生: 最多可找出(1,4)(2,5)(3,6)三对学生.

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

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

染色法判断是否是二分图 hdu2444

用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 但是如果这个图不是二分图,那么就会这样 把与1相邻的点2,3染成白色,然后入队列,然后2出队列,要把与2相邻的点2,3染成黑色,但是都染过了,所以不用染色 但是3的颜色应该与2相反(如果是二分图的话),可是没有相反,所以就不是二分图 1 #include <stdio.h> 2 #include

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.emp

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

二分图入门

判断一个图是不是二分图 用染色法,二分图是这样一个图: 有两顶点集且图中每条边的的两个顶点分别位于两个顶点集中,每个顶点集中没有边相连接! 判断二分图的常见方法:开始对任意一未染色的顶点染色,之后判断其相邻的顶点中,若未染色则将其染上和相邻顶点不同的颜色, 若已经染色且颜色和相邻顶点的颜色相同则说明不是二分图,若颜色不同则继续判断, 每次用bfs/dfs遍历都可. 二分图最大匹配匈牙利算法: 算法的思路是不停的找增广轨,并增加匹配的个数,增广轨顾名思义是指一条可以使匹配数变多的路径,在匹配问题中