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" 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

HINT

要求找到一个点集合,让这个集合中并没有人互相喜欢

找相互喜欢的人的最大匹配,答案为总人数-匹配数/2

做的时候又忘了数据初始化,悲伤

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<queue>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=800;
10 int n;
11 vector<int>mp[mxn];
12 int vis[mxn];
13 int mc[mxn];
14 int x;
15 inline int read(){
16     int x=0;
17     char ch=getchar();
18     while(ch<‘0‘||ch>‘9‘)ch=getchar();
19     while(ch>=‘0‘ && ch<=‘9‘){ x=x*10+ch-‘0‘;ch=getchar(); }
20     return x;
21 }
22 int dfs(int u){
23     for(int i=0;i<mp[u].size();i++){
24         int v=mp[u][i];
25         if(!vis[v]){
26             vis[v]=1;
27             if(mc[v]==-1 || dfs(mc[v])){
28                 mc[v]=u;
29                 return 1;
30             }
31         }
32     }
33     return 0;
34 }
35 int Maxmatch(){
36     int res=0;
37     memset(mc,-1,sizeof(mc));
38     int i;
39     for(i=0;i<n;i++){
40         {
41             memset(vis,0,sizeof(vis));
42             res+=dfs(i);
43         }
44     }
45     return res;
46 }
47 int main(){
48     while(scanf("%d",&n)!=EOF){
49         //read
50         for(int i=0;i<=n;i++) mp[i].clear();
51         for(int L=1;L<=n;L++){
52             int u,v;
53             u=read();
54             int i,j;
55             x=read();
56             for(i=0;i<x;i++){
57                 v=read();
58                 mp[u].push_back(v);
59             }
60         }
61         //read finished
62         int ans=Maxmatch();
63         printf("%d\n",n-ans/2);
64     }
65     return 0;
66 }
时间: 2024-08-07 08:38:00

POJ 1466 Girls and Boys的相关文章

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

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

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

POJ 1466 Girls and Boys【最大点独立集】

大意:最大点独立集 分析:最大点独立集 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 505; 8 9 int n; 10 int Link[maxn]; 11 int vis[maxn]; 12 vector<int> G[maxn

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