HDU 2444 二分图判定+最大匹配

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2444
用模拟上色来判定了。然后判定完再用最大匹配就好了。
尴尬的是No打成NO,WA了1个小时找不到错。尴尬

#include<bits/stdc++.h>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 205;
bool mp[_max][_max],vis[_max];
int n,m,link[_max],col[_max];

bool judge(int i,int co){
    col[i] = co;
    for(int j = 1 ; j <= n ; j++){
        if(!mp[i][j]) continue;
        if(!col[j]){
            if(!judge(j,-co)) return 0;
        }
        else if(col[i] == col[j]) return 0;
    }
    return 1;
}
bool isBigraph(){
    memset(col,0,sizeof(col));
    for(int i = 1 ; i <= n ; i++){
        if(col[i]==0){
            if(!judge(i,-1)) return 0;
        } 
    }
    return 1;
}
bool dfs(int rt){
    for(int i = 1 ; i <= n ; i++){
        if(!mp[rt][i] || !vis[i]) continue;
        vis[i] = 0;
        if(link[i] == 0 || dfs(link[i])){
            link[i] = rt;
            return true;
        }
    }
    return false;
}
int hungary(){
    int res = 0;
    memset(link,0,sizeof(link));
    for(int i = 1 ; i <= n ; i++){
        memset(vis,1,sizeof(vis));
        if(dfs(i)) res++;
    }   
    return res;
}
int main(){
    IOS;
    int u,v;
    while(cin>>n>>m){
        memset(mp,0,sizeof(mp));
        for(int i = 1 ; i <= m ; i++){
            cin>>u>>v;
            mp[u][v] = 1;
            mp[v][u] = 1;
        }
        if(!isBigraph()) cout<<"No"<<endl;
        else cout<<hungary()/2<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值