POJ1466 Girls and Boys【二分图最大独立集】

题目链接:

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

题目大意:

有N个学生,他们之间的某些人比较暧昧,只有认识的人能组成一个集合。问:最多能组成

多少个集合,使得这几个集合之间的学生都没有任何关系。

思路:

从N个图中选出M个点,使得这M个点两两之间没有边,求最大的M是多少。二分图最大独立

集问题。本来应该以男生、女生各一边建二分图求最大独立集,但是这里只有N个点,没有告

诉男生、女生的编号。那么以N个学生为一边、再以N个学生为另一边。将相互联系的人之间

建边。然后求最大匹配数。因为如果u和v有联系的话,边(u,v)和(v,u)都加入了二分图中,

被重复计算了两遍。又因为二分图最大独立集 = N - 二分图最大匹配数。所以最终答案就是

N - 最大匹配数/2。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 550;

bool Map[MAXN][MAXN];
bool Mask[MAXN];

int NX,NY;
int cx[MAXN],cy[MAXN];

int FindPath(int u)
{
    for(int i = 0; i < NX; ++i)
    {
        if(Map[u][i] && !Mask[i])
        {
            Mask[i] = 1;
            if(cy[i] == -1 || FindPath(cy[i]))
            {
                cy[i] = u;
                cx[u] = i;
                return 1;
            }
        }
    }
    return 0;
}

int MaxMatch()
{
    for(int i = 0; i < NX; ++i)
        cx[i] = -1;
    for(int i = 0; i < NY; ++i)
        cy[i] = -1;

    int res = 0;
    for(int i = 0; i < NX; ++i)
    {
        if(cx[i] == -1)
        {
            for(int j = 0; j < NY; ++j)
                Mask[j] = 0;
            res += FindPath(i);
        }
    }
    return res;
}

int main()
{
    int N,K,u,v;
    while(~scanf("%d",&N))
    {
        memset(Map,0,sizeof(Map));
        NX = NY = N;
        for(int i = 0; i < N; ++i)
        {
            scanf("%d: (%d)",&u,&K);
            for(int j = 0; j < K; ++j)
            {
                scanf("%d",&v);
                Map[u][v] = 1;
                Map[v][u] = 1;
            }
        }
        printf("%d\n",N-MaxMatch()/2);
    }

    return 0;
}
时间: 2024-08-12 23:17:44

POJ1466 Girls and Boys【二分图最大独立集】的相关文章

HDU 1068 Girls and Boys (二分图最大独立集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 有n个同学,格式ni:(m) n1 n2 n3表示同学ni有缘与n1,n2,n3成为情侣,求集合中不存在有缘成为情侣的同学的最大同学数. 独立集(图的顶点集的子集,其中任意两点不相邻) 二分图中 最大独立集 = 顶点个数 - 最大匹配数 因为男女不知道,将一个人拆成两个性别,求最大匹配后,除以2就行了. 这种做法比较难理解. 1 #include <iostream> 2 #include

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

http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) 2U=2V-2M  所以 U=n-M'/2. (没怎么看明白)  但是不这样会wa. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #inc

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(二分图的最大独立集)

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

(hdu step 6.3.2)Girls and Boys(求最大独立集)

题目: Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 189 Accepted Submission(s): 127   Problem Description the second year of the university somebody started a study on the romanti

HDU1068 Girls and Boys 【最大独立集】

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7710    Accepted Submission(s): 3535 Problem Description the second year of the university somebody started a study on the roman

POJ1466_Girls and Boys(二分图/最大独立集=N-最大匹配)

解题报告 http://blog.csdn.net/juncoder/article/details/38160591 题目传送门 题意: 求满足条件的最大集合:集合内任何两个人都没有浪漫关系 思路: 跟POJ2771一样的题,变的简单多了.POJ2771解题报告 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std;

HDU1068/POJ1466_Girls and Boys(二分图/最大独立集=N-最大匹配)

解题报告 http://blog.csdn.net/juncoder/article/details/38160591 题目传送门(POJ) 题目传送门(HDU) 题意: 求满足条件的最大集合:集合内不论什么两个人都没有浪漫关系 思路: 跟POJ2771一样的题,变的简单多了.POJ2771解题报告 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> us

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

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