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

Problem 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
题意:有n个学生,每个学生都和一些人又关系,找出互相没关系的最多的一群人。

思路:这是一道二分图最大独立集模板题【最大独立集= 点数 - 最大匹配数】注意:最大匹配数需要除以2

【参考博客】

看到之后就可以发现,这是一道非常明显的最大独立集的问题,可以转化为二分图来做,还是最经典的拆点建图,然后根据定理,最大独立集=顶点数-最小点覆盖数。  而对于这道题来说,我们可以发现这个浪漫关系是相互的。

而我们的建图中,按理来说应该是一边是男的点,一边是女的点这样连边,但是题目中没说性别的问题。

只能将每个点拆成两个点,一个当作是男的点,一个当作是女的点了,然后连边。由于关系是相互的,这样就造成了边的重复。也就是边集是刚才的二倍,从而导致了最大匹配变成了二倍。

那么 ,最大独立集=顶点数-最大匹配/2,所以最终答案就呼之欲出了。

AC代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<vector>
 6 using namespace std;
 7
 8 #define maxn 666
 9 vector<int> v[maxn];
10 int vis[maxn];
11 int match[maxn];
12 int n;
13 int dfs(int u){
14     for(int i=0;i<v[u].size();i++){
15         int temp=v[u][i];
16         if(vis[temp]==0){
17             vis[temp]=1;
18             if(match[temp]==0||dfs(match[temp])){
19                 match[temp]=u;
20                 return 1;
21             }
22         }
23     }
24     return 0;
25 }
26 int main(){
27     while(~scanf("%d",&n)){
28         for(int i=0;i<n;i++)
29             v[i].clear();
30         int x,m,y;
31         for(int i=1;i<=n;i++){
32             scanf("%d: (%d)",&x,&m);
33             for(int j=0;j<m;j++){
34                 scanf("%d",&y);
35                 v[x].push_back(y);
36                 //v[y].push_back(x);
37             }
38         }
39         memset(match,0,sizeof(match));
40         int ans=0;
41         for(int i=0;i<n;i++){
42             for(int j=0;j<=n;j++)
43                 vis[j]=0;
44             if(dfs(i))
45                 ans++;
46         }
47         printf("%d\n",n-ans/2);
48     }
49     return 0;
50 } 

原文地址:https://www.cnblogs.com/pengge666/p/11625982.html

时间: 2024-08-29 11:08:15

Girls and Boys POJ - 1466 【(二分图最大独立集)】的相关文章

Girls and Boys(poj 1466)

题目描述: 给出一系列男女配对意愿信息.求一个集合中的最大人数,满足这个集合中两两的人不能配对. /* 二分图的最大独立集 因为没有给出具体的男生和女生,所以可以将数据扩大一倍,即n个男生,n个女生, 根据定理,最大独立集=总数-匹配数(本题应该除以2) */ #include<cstdio> #include<iostream> #include<cstring> #define N 510 using namespace std; int a[N][N],used[

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 stu

hdoj 1068 Girls and Boys【匈牙利算法+最大独立集】

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

Girls and Boys HDU - 1068 二分图匹配(匈牙利)+最大独立集证明

最大独立集证明参考:https://blog.csdn.net/qq_34564984/article/details/52778763 最大独立集证明: 上图,我们用两个红色的点覆盖了所有边.我们证明的前提条件是已经达到最小覆盖. 即条件1.已经覆盖所有边,条件2.所用的点数最小 首先我们来证明蓝色点组成的是一个独立集:如果有两个蓝色点间有边相连,那么这条 边则没有被覆盖,则与条件1矛盾.因此是独立集. 再来证明这个独立集最大: 如果我们要再增加这个独立集中的点,则需要把某个红点变 成蓝点.而

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

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 (最大独立集)

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