Poj(1466),最大独立集,匈牙利算法

题目链接:http://poj.org/problem?id=1466

Girls and Boys

Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 12026   Accepted: 5355

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

Source

Southeastern Europe 2000

题意:

就是有n个人,每个人与其他的某几个人有关系,这个关系且称为浪漫关系,然后最后求一个最大的集合,使得集合中所有的人两两之间都不存在浪漫关系。

分析:

1、最大独立集 = 顶点数 - 最大匹配数

2、匈牙利算法

3、这里的男女性别没有给出,匹配关系放大一倍,那么实际的匹配数就是/2了。

#include <stdio.h>
#include <string.h>

#define MAX 505

int n;
bool maps[MAX][MAX];
bool use[MAX];
int match[MAX];

bool DFS(int stu)
{
    int num = 0;
    for(int i=0; i<n; i++)
    {
        if(!use[i]&&maps[stu][i])
        {
            use[i] = true;
            if(match[i]==-1||DFS(match[i]))
            {
                match[i] = stu;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(match,-1,sizeof(match));
        memset(maps,false,sizeof(maps));
        for(int i=0; i<n; i++)
        {
            int u,num;
            scanf("%d: (%d)",&u,&num);
            for(int i=0; i<num; i++)
            {
                int v;
                scanf("%d",&v);
                maps[u][v] = true;
            }
        }
        int num = 0;
        for(int i=0; i<n; i++)
        {
            memset(use,false,sizeof(use));
            if(DFS(i)) num++;
        }
        printf("%d\n",n-num/2);
    }
    return 0;
}
时间: 2024-10-13 21:21:33

Poj(1466),最大独立集,匈牙利算法的相关文章

POJ 3041 Asteroids (匈牙利算法)

Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14388 Accepted: 7828 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K astero

poj 1469 COURSES(匈牙利算法模板)

http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19419   Accepted: 7642 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is

poj 1274最大匹配匈牙利算法

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <cmath> #include <cstring> #include <stack> #include <set> #include <map> #include <vector> using namespace st

FZU 星系碰撞 最大独立集 匈牙利算法

Problem 2194 星系碰撞 Accept: 29    Submit: 102 Time Limit: 30000 mSec    Memory Limit : 327680 KB Problem Description 据预测,大约在100亿年后,狮子座星系将与银河系发生碰撞,两个星系的碰撞将会合并两个星系,但是没有2个星球会相撞.现在某科学家得到两个星系合并后的结果,一些二维平面上的点,但是不知道那些星球属于银河系,已知如果两个星球属于同一个星系,那么他们之间的距离大于5光年,这边的

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as

POJ 3020 Antenna Placement(二分图 匈牙利算法)

题目网址:  http://poj.org/problem?id=3020 题意: 用椭圆形去覆盖给出所有环(即图上的小圆点),有两种类型的椭圆形,左右朝向和上下朝向的,一个椭圆形最多可以覆盖相邻的两个小圆点.   思路: 将每个小圆点看作是一个顶点,因为一个椭圆只能覆盖两个小圆点,我们就可以把这个图看成一个二分图.将相邻的两个点,一个看作是X集合内顶点,另一个看成是Y集合内顶点.但要注意的是一个顶点可能不止和一个顶点想连(如上图情况),所以我们要把上述情况看作是一个无向图,而不是有向图.无向图

POJ 1325 Machine Schedule (二分图最小点集覆盖 匈牙利算法)

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 5399 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

poj 1466 Girls and Boys (最大独立集)

链接:poj 1466 题意:有n个学生,每个学生都和一些人有关系,现在要你找出最大的人数,使得这些人之间没关系 思路:求最大独立集,最大独立集=点数-最大匹配数 分析:建图时应该是一边是男生的点,一边是女生的点连边,但是题目中没说性别的问题,只能将每个点拆成两个点,一个当作是男的点,一个当作是女的点了,然后连边.由于关系是相互的,这样就造成了边的重复.也就是边集是刚才的二倍,从而导致了最大匹配变成了原本的二倍,因此,此时最大独立集=点数-最大匹配数/2. #include<stdio.h>

POJ 1466 Girls and Boys(最大独立集)

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 fin