Girls and Boys(匈牙利)

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9245    Accepted Submission(s): 4240

Problem Description

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.

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, for n subjects.
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

题解:男生女生可能会相互吸引,就爱上了彼此,所以让你写一个程序,让找出不让他们相互喜欢的集合数目;

代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<vector>
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 using namespace std;
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=1010;
11 vector<int>vec[MAXN];
12 int usd[MAXN],vis[MAXN];
13 bool dfs(int x){
14     for(int i=0;i<vec[x].size();i++){
15         int v=vec[x][i];
16             if(!vis[v]){
17                 vis[v]=1;
18             if(usd[v]==-1||dfs(usd[v])){
19                 usd[v]=x;return true;
20             }
21         }
22     }
23     return false;
24 }
25 int main(){
26     int N,a,t;
27     while(~scanf("%d",&N)){
28         for(int i=0;i<MAXN;i++)vec[i].clear();
29         for(int i=0;i<N;i++){
30             scanf("%*d: (%d)",&t);
31         //    printf("t=%d\n",t);
32             while(t--){
33                 scanf("%d",&a);
34                 vec[i].push_back(a);
35             }
36         }mem(usd,-1);mem(vis,0);
37         int ans=0;
38         for(int i=0;i<N;i++){
39             mem(vis,0);
40             if(dfs(i))ans++;
41         }
42         printf("%d\n",N-ans/2);
43     }
44     return 0;
45 }
时间: 2024-08-26 04:24:56

Girls and Boys(匈牙利)的相关文章

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

hdu 1068 Girls and Boys(匈牙利算法求最大独立集)

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

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

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 Girls and Boys (最大独立点集)

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

HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)

HDU 1068 :题目链接 题意:一些男孩和女孩,给出一些人物关系,然后问能找到最多有多少个人都互不认识. 转换一下:就是大家都不认识的人,即最大独立集合 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define init(a) memset(a,

hduoj-----(1068)Girls and Boys(二分匹配)

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

【HDOJ 1068】Girls and Boys

[HDOJ 1068]Girls and Boys 二分图最大独立集问题 原本想用最大独立集 = 补图的最大团来做..结果超时 后来改用二分图最大独立集 = 顶点数 - 二分图最大匹配来做,.vis数组没用好 又超 最后改来改去改对了-- 由于输入建图是双向的 所以要减两倍的最大匹配 代码如下: #include <cstdlib> #include <cstdio> #include <cstring> #include <iostream> #inclu

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