您的位置:首页 > 其它

hdu 1068 Girls and Boys(二分图匹配---最大独立点集)

2015-04-12 09:53 417 查看

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8223 Accepted Submission(s): 3768



Problem Description
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.

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, for n subjects.

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




Source
Southeastern Europe 2000

题目分析:

把所有点拆成两个点,然后根据题意建边,做二分图匹配,然后要求的是最大独立点集,也就是所有能够匹配上的都把能匹配的点减去一个,那么只和它相连的点就孤立了,而和其他点相连的点,会在去掉后来的匹配的过程中也变成孤立的点

#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <iostream>
#define MAX 5000

using namespace std;

int n,m;

struct Edge
{
    int v,next;
}e[MAX*50];

int head[MAX];
int cc;

void add ( int u , int v )
{
    e[cc].v = v;
    e[cc].next = head[u];
    head[u] = cc++;
}

void scan ( int &x )
{
    char ch;
    x = 0;
    while ( !isdigit(ch) ) ch = getchar();
    while ( isdigit(ch) )
    {
        x = x*10+ch-48;
        ch = getchar();
    }
}

char s[50];

int linker[MAX];
int used[MAX];

bool dfs ( int u )
{
    for ( int i = head[u]; ~i ; i = e[i].next )
    {
        int v = e[i].v;
        if ( used[v] ) continue;
        used[v] = true;
        if ( linker[v] == -1 || dfs ( linker[v] ) )
        {
            linker[v] = u;
            return true;
        }
    }
    return false;
}

int hungary ( )
{
    int res = 0;
    memset ( linker , -1 , sizeof ( linker ) );
    for ( int u = 0 ; u < n ; u++ )
    {
        memset ( used , 0 , sizeof ( used ) );
        if ( dfs(u) ) res++;
    }
    return res/2;   
}

int main ( )
{
    int  v;
   // freopen ( "a.txt" , "r" , stdin );
    while ( ~scanf ( "%d" , &n ) )
    {
        memset ( head , -1 , sizeof ( head ) );
        cc = 0;
        for ( int i = 0 ; i < n ; i++ )
        {
            scanf ( "%s" , s );
            scan ( m );
            for ( int j = 0 ; j < m ; j++ )
            {
                scanf ( "%d" , &v );
                add ( i , v+n );
            }  
        }
        printf ( "%d\n" ,  n-hungary() );
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: