本文主要是介绍hdu1068 Girls and Boys 最大独立集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.
题解:二分图最大独立集=节点数-最大匹配数。
求出来的最大匹配数要除2,因为题目没有给出哪些是男的哪些是女的,也就是说没有明显的二分图,所以将一个人拆成两个人进行最大匹配。由于一个拆成两个,所以最大匹配数应该是求出来的数除以2 。最后再用顶点数减就行了。
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
#define maxn 1001
#define INF 2147483646
using namespace std;
int map[maxn][maxn];
int link[maxn];
int vit[maxn];
int n;
bool dfs(int u)
{for(int i=0;i<n;i++){if(map[u][i]&&!vit[i]){vit[i]=1;if(link[i]==-1||dfs(link[i])){link[i]=u;return true;}}}return false;
}
void solve(int &ans)
{for(int i=0;i<n;i++){memset(vit,0,sizeof(vit));if(dfs(i))ans++;}
}
int main()
{while(cin>>n){int ans=0;memset(map,0,sizeof(map));memset(link,-1,sizeof(link));for(int i=0;i<n;i++){int t,u;char a,b,c;cin>>t>>a>>b>>u>>c;for(int j=0;j<u;j++){int x;cin>>x;map[i][x]=1;}}solve(ans);cout<<n-ans/2<<endl;}
}
这篇关于hdu1068 Girls and Boys 最大独立集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!