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);
    }

}

 

posted @ 2015-04-10 12:34  qscqesze  阅读(410)  评论(0编辑  收藏  举报