先澄清几个概念:
二分图最大独立集 == 点数n - 二分图最大匹配。
二分图最小路径覆盖 == 点数n - 二分图最大匹配
二分图最小点集覆盖 == 二分图最大匹配
Girls and Boys
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4519 Accepted Submission(s): 1965
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.
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
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
vector<int>a[505];
int visit[505] , flag[505];
bool dfs( int x )
{
for( int i=0 ; i<a[x].size() ; i++ )
{
if( !visit[a[x][i]] ){
visit[a[x][i]] = 1;
if( flag[a[x][i]] == -1 || dfs(flag[a[x][i]]) ){
flag[a[x][i]] = x;
return true;
}
}
}
return false;
}
int main( )
{
int n;
while( scanf("%d",&n)!= EOF )
{
char c,s,ss;
int num,m,x;
memset( flag , -1 , sizeof(flag) );
memset( a , 0 , sizeof(a) );
for( int i=0 ; i<n ; i++ )
{
cin >> num >> c >> s >> m >> ss;
for( int j=0 ; j<m ; j++ )
{
scanf("%d",&x);
a[i].push_back(x);
}
}
int sum = 0;
for( int i=0 ; i<n ; i++ ){
memset( visit , 0 , sizeof(visit) );
if( dfs(i) )
sum++;
// printf("%d\n",sum);
}
printf("%d\n",n-sum/2);
}
return 0;
}

本文介绍了一个关于学生间浪漫关系的研究问题,旨在寻找一个最大的学生集合,在该集合中没有两个学生有过浪漫关系。通过构建图模型,并利用二分图最大匹配算法解决此问题。
225

被折叠的 条评论
为什么被折叠?



