POJ 1466 Girls and Boys(二分图匹配)

本文提供了一种解决 POJ 1466 问题的方法,通过二分图匹配算法来确定最少数量的未配对个体。介绍了完整的 C++ 代码实现,包括读取输入数据、构建图结构、执行匹配算法并输出结果。

 

【题目链接】 http://poj.org/problem?id=1466

 

【题目大意】

  给出一些人和他们所喜欢的人,两个人相互喜欢就能配成一对,
  问最后没有配对的人的最少数量

 

【题解】

  求最少数量,就是最多匹配的补集,因此做一遍二分图匹配即可。

 

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector> 
using namespace std;
const int MAX_V=1000;
const int INF=0x3f3f3f3f;
int V,match[MAX_V];
vector<int> G[MAX_V];
bool used[MAX_V];
void add_edge(int u,int v){
    G[u].push_back(v);
    G[v].push_back(u);
}
bool dfs(int v){
    used[v]=1;
    for(int i=0;i<G[v].size();i++){
        int u=G[v][i],w=match[u];
        if(w<0||!used[w]&&dfs(w)){
            match[v]=u;
            match[u]=v;
            return 1;
        }
    }return 0;
}
int bipartite_matching(){
    int res=0;
    memset(match,-1,sizeof(match));
    for(int v=0;v<V;v++){
        if(match[v]<0){
            memset(used,0,sizeof(used));
            if(dfs(v))res++;
        }
    }return res;
}
int N,x,k,y;
void init(){
    V=N;
    for(int i=0;i<V;i++)G[i].clear();
    for(int i=0;i<N;i++){
        scanf("%d: (%d)",&x,&k);
        for(int i=0;i<k;i++){
            scanf("%d",&y);
            add_edge(x,y);
        }
    }
}
void solve(){
    printf("%d\n",N-bipartite_matching());
}
int main(){
    while(~scanf("%d",&N)){
        init();
        solve();
    }return 0;
}

转载于:https://www.cnblogs.com/forever97/p/poj1466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值