【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】

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.

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

Output

5
2

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

题意:输入n,再输入n行数字,每行第一个数字为学生编号,第二个数字为匹配学生的总数k,再输入k个匹配学生编号,问最小的没有匹配的学生数为多少?

思路:这是一个求最大独立集的问题。最大独立集 = 顶点个数-最小顶点覆盖(最大匹配)。由于题目只给了匹配学生编号,没有分性别给出,所以一定会有重复,比如样例1中0和4匹配了两次,所以我们求出的“最大匹配”除以2后才是实际的最大匹配,去重。

#include<stdio.h>
#include<string.h>
#define N 1000
int n,m;
int e[N][N];
int book[N],match[N];

int dfs(int u)
{
    int i;
    for(i = 0; i < n; i ++)
    {
        if(!book[i]&&e[u][i])
        {
            book[i] = 1;
            if(!match[i]||dfs(match[i]))
            {
                match[i] = u;
                //match[u] = i;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int i,j,t1,t2,sum,d;
    char a,b,c,f;
    while(scanf("%d",&n)!=EOF)
    {
        memset(e,0,sizeof(e));
        memset(match,0,sizeof(match));
        sum = 0;
        for(i = 0; i < n; i ++)
        {
            scanf("%d%c%c",&t1,&a,&f);//printf("t1=%d\n",t1);
            scanf("%c%d%c",&b,&m,&c);//printf("m=%d\n",m);
            for(j = 0; j < m; j ++)
            {
                scanf("%d",&t2);//printf("t2=%d\n",t2);
                e[t1][t2] = 1;
                //e[t2][t1] = 1;
            }
        }
        for(i = 0; i < n; i ++)
        {
            memset(book,0,sizeof(book));
            if(dfs(i))
                sum ++;
        }
        printf("%d\n",n-sum/2);
    }
    return 0;
 }
时间: 2024-11-08 23:25:08

【二分图匹配入门专题1】B - Girls and Boys hdu1068【最大独立集】的相关文章

【二分图匹配入门专题1】D - Matrix hdu2119【最小顶点覆盖】

Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column . Your task is to give out the minimum times of deleting all the '1' in the matrix. InputThere are several test case

【二分图匹配入门专题1】I - Hiding Gold light oj 1152【二分图匹配】-------------------我是终于不那么水的水题分割线------------------------

You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x 1 dominoes such that all gold are covered. You may use the dominoes vertically or horizontally and the dominoes may overlap. All you have to do is to cov

【二分图匹配入门专题1】E - Air Raid hdu1151【最小路径覆盖】

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's

【二分图匹配入门专题1】K - Going Home hdu1533【km匹配】

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, unt

【二分图匹配入门专题1】L - Card Game hdu 3722【km算法】

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of

【二分图匹配入门专题1】M - Cyclic Tour hdu1853【km算法--判断自重边】

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total len

【二分图匹配入门专题1】H - Marriage Media light oj 1184【二分图最大匹配】

You run a marriage media. You take some profiles for men and women, and your task is to arrange as much marriages as you can. But after reading their bio-data you have found the following criteria. No man will marry a woman if their height gap is gre

【二分图匹配入门专题1】F - COURSES poj1469【最大匹配--匈牙利算法模板题】

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: every stude

【二分图匹配入门专题1】 N - Special Fish hdu3395 【km算法】【春风十里,都不如tle~~~】

There is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It's hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to