poj 1466 Girls and Boys 二分图的最大匹配

匈牙利算法在图论中的应用
本文探讨了匈牙利算法在解决图论问题中的应用,具体为找到无相互关系的最大点集。通过实例解析,展示了算法的实现过程及求解策略。

Girls and Boys

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://poj.org/problem?id=1466

Description

In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.

Input

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)

The student_identifier is an integer number between 0 and n-1 (n <=500 ), for n subjects.

Output

For each given data set, the program should write to standard output a line containing the result.

Sample Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0

Sample Output

5
2

HINT

题意

 让你找到一个点集合,让这个集合中并没有人互相喜欢

题解:

这道题就是匈牙利算法,最大匹配的简单变形,     答案为n-最大匹配/2;

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff;   //无限大
const int inf=0x3f3f3f3f;
/*

*/
//**************************************************************************************

inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int ma[maxn][maxn];
int vis[maxn];
int match[maxn];
int n,m;
vector<int> e[maxn];
int dfs(int a)
{
    for(int i=0;i<e[a].size();i++)
    {
        if(vis[e[a][i]]==0)
        {
            vis[e[a][i]]=1;
            if(match[e[a][i]]==-1||dfs(match[e[a][i]]))
            {
                match[e[a][i]]=a;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(match,-1,sizeof(match));
        for(int i=0;i<n;i++)
            e[i].clear();
        m=n;
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d: (%d)",&x,&y);
            for(int j=0;j<y;j++)
            {
                int c=read();
                e[x].push_back(c);
            }
        }
        int ans=0;
        for(int i=0;i<m;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i)==1)
                ans++;
        }
        printf("%d\n",n-ans/2);
    }

}

 

转载于:https://www.cnblogs.com/qscqesze/p/4414246.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值