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

本文探讨了在大学研究中关于学生浪漫关系的分析,通过将问题转化为二分图匹配,并求解最大独立点集,最终输出研究结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题目分析:
把所有点拆成两个点,然后根据题意建边,做二分图匹配,然后要求的是最大独立点集,也就是所有能够匹配上的都把能匹配的点减去一个,那么只和它相连的点就孤立了,而和其他点相连的点,会在去掉后来的匹配的过程中也变成孤立的点
 
#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() );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值