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>
using namespace std;
int n,mmap[550][550],pre[550],vis[550];
int dfs(int x)
{
    for(int i=0; i<n; i++) {
        if(!vis[i]&&mmap[x][i]) {
            vis[i]=1;
            if(pre[i]==-1||dfs(pre[i])) {
                pre[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,a,b,k;
    while(~scanf("%d",&n)) {
        memset(mmap,0,sizeof(mmap));
        memset(pre,-1,sizeof(pre));
        for(i=1; i<=n; i++) {
            scanf("%d: (%d) ",&a,&k);
            for(j=1; j<=k; j++) {
                scanf("%d",&b);
                mmap[a][b]=1;
            }
        }
        int ans=0;
        for(i=0; i<n; i++) {
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        printf("%d\n",n-ans/2);
    }
    return 0;
}

Girls and Boys

Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10348   Accepted: 4608

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
时间: 2024-11-12 05:57:32

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

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;

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

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

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

hdu1068 Girls and Boys,二分图最大独立集

点击打开链接 二分图最大独立集 = 顶点数 - 最大匹配数 #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1005; int g[maxn][maxn]; int n; int link[maxn]; bool used[maxn

hdu1068 Girls and Boys --- 最大独立集

有一个集合男和一个集合女,给出两集合间一些一一对应关系,问该两集合中的最大独立集的点数. 最大独立集=顶点总数-最大匹配数 此题中,若(a,b)有关,则(b,a)有关,每一个关系算了两次,相当于二分图的两边集合没有分男女,两边都是总人数, 所以此题中答案应该是 顶点总数-最大匹配数/2 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algor

杭电1068--Girls and Boys(二分图最大独立集)

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

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

题目链接: http://poj.org/problem?id=1466 题目大意: 有N个学生,他们之间的某些人比较暧昧,只有认识的人能组成一个集合.问:最多能组成 多少个集合,使得这几个集合之间的学生都没有任何关系. 思路: 从N个图中选出M个点,使得这M个点两两之间没有边,求最大的M是多少.二分图最大独立 集问题.本来应该以男生.女生各一边建二分图求最大独立集,但是这里只有N个点,没有告 诉男生.女生的编号.那么以N个学生为一边.再以N个学生为另一边.将相互联系的人之间 建边.然后求最大匹

Girls and Boys POJ - 1466 【(二分图最大独立集)】

Problem DescriptionIn 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