poj 1466 Girls and Boys(二分图的最大独立集)

http://poj.org/problem?id=1466

Girls and Boys

Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 11085   Accepted: 4956

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

最大独立集 = 总点数 - 最大匹配数因为本题将人数扩大2倍,所以最大匹配数应除以2

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define N 510
#define INF 0x3f3f3f3f

using namespace std;

int G[N][N], vis[N], used[N];
int n;

bool Find(int u)
{
    int i;
    for(i = 0 ; i < n ; i++)
    {
        if(!vis[i] && G[u][i])
        {
            vis[i] = 1;
            if(!used[i] || Find(used[i]))
            {
                used[i] = u;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int i, a, b, m, ans;
    while(~scanf("%d", &n))
    {
        ans = 0;
        memset(G, 0, sizeof(G));
        for(i = 1 ; i <= n ; i++)
        {
            scanf("%d: (%d)", &a, &m);
            while(m--)
            {
                scanf("%d", &b);
                G[a][b] = 1;
            }
        }
        memset(used, 0, sizeof(used));
        for(i = 0 ; i < n ; i++)
        {
            memset(vis, 0, sizeof(vis));
            if(Find(i))
                ans++;
        }
        printf("%d\n", n - ans / 2);
    }
    return 0;
}

时间: 2024-08-03 04:18:40

poj 1466 Girls and Boys(二分图的最大独立集)的相关文章

POJ - 1466 Girls and Boys 二分图+最大独立集

题目大意:有n个学生,某些学生之间存在着一种特殊的关系...现在要找出m个学生,要求这m个学生之间的任意两人不存在这种特殊的关系 解题思路:二分图问题,因为没办法划分成相应的两个集合且特殊关系是对称的,所以可以将两个点集都设置为n个点,求出最大匹配后再除以2即可得到(因为关系是对称的,所以所求得的最大匹配是双倍的) 得到最大匹配了,可以由定理得到 最大独立集 = n - 最大匹配数 #include<cstdio> #include<vector> #include<cstr

POJ 1466 Girls and Boys(二分图匹配+拆点+最大独立集)

POJ 1466 Girls and Boys 题目链接 题意:n个人,每个人有一个爱慕的集合,现在要挑出一些人,使得集合中没有人两两爱慕,问这个集合最大人数是多少 思路:每个人拆成两点,爱慕和被爱慕,然后建图,跑二分图最大匹配,由于爱慕关系是相互的,所以匹配数会多2倍,然后人数n - 最大匹配数 / 2就是最大独立集 代码: #include <cstdio> #include <cstring> #include <vector> #include <algo

POJ 1466 Girls and Boys (匈牙利算法 最大独立集)

Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10912   Accepted: 4887 Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically in

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&q

POJ 1466 Girls and Boys 求最大独立点集

最大独立点集 = 点数 - 最大匹配数 注意这题因为是两两匹配,A匹配B B匹配A算两个,所以最大匹配数要除以2 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #in

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

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

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&q

POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题

有n个人, 其中有男生和女生,接着有n行,分别给出了每一个人暗恋的对象(不止暗恋一个) 现在要从这n个人中找出一个最大集合,满足这个集合中的任意2个人,都没有暗恋这种关系. 输出集合的元素个数. 刚开始想,把人看成顶点,若有暗恋的关系,就连一条边,构成一个图 独立集的概念:一个图中两两互不相连的顶点集合 所以这道题,就是要求最大独立集 有:最大独立集+最小顶点覆盖=|V|(顶点的总个数) 那就求最小顶点覆盖了 根据题意: 暗恋的对象性别不同,所以a暗恋b,b暗恋c,c暗恋a这种关系不可能存在 也

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