POJ-1466 Girls and Boys (二分图最大独立集)
2018-02-23 16:27
447 查看
Girls and Boys
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.InputThe 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.OutputFor 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) 0Sample Output
5 2
题目大概意思: 有n个学生,每个学生都和一些人又关系,找出互相没关系的最多的一群人。
因为题目只需要求一个集合。
最大独立集= 点数 - 最大匹配数。这里最大匹配数需要除以2(看到之后就可以发现,这是一道非常明显的最大独立集的问题,可以转化为二分图来做,还是最经典的拆点建图,然后根据定理,最大独立集=顶点数-最小点覆盖数。 而对于这道题来说,我们可以发现这个浪漫关系是相互的。而我们的建图中,按理来说应该是一边是男的点,一边是女的点这样连边,但是题目中没说性别的问题。只能将每个点拆成两个点,一个当作是男的点,一个当作是女的点了,然后连边。由于关系是相互的,这样就造成了边的重复。也就是边集是刚才的二倍,从而导致了最大匹配变成了二倍。那么 ,最大独立集=顶点数-最大匹配/2,所以最终答案就呼之欲出了。) 括号里面理解: 这里这里点这里
代码如下:/* POJ 1466 */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
#include<set>
#define manx maxn
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn = 500 +10;
using namespace std;
int Laxt[maxn],Next[maxn*200],To[maxn*200],vis[maxn],dis[maxn];
int cnt;
void init()
{
mem(Laxt,0);
mem(vis,0);
mem(dis,-1);
cnt=0;
}
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
bool dfs(int u)
{
int i;
for(i=Laxt[u]; i; i=Next[i])
{
int v=To[i];
if(!vis[v])
{
vis[v]=1;
if(dis[v]==-1||dfs(dis[v]))
{
dis[v]=u;
return true;
}
}
}
return false;
}
int main()
{
int n,m,u,v,ans;
while(~scanf("%d",&n))
{
init();
int ans=0;
for(int j=0; j<n; j++)
{
scanf("%d: (%d)",&u,&m);
while(m--)
{
scanf("%d",&v);
add(u,v);
//add(v,u);
}
}
for(int i=0; i<n; i++)
{
mem(vis,0);//这里wa了一发 注意了!!!
if(dfs(i)) ans++;
}
cout<<n-(ans/2)<<endl;
}
}
相关文章推荐
- POJ 1466:Girls and Boys 二分图的最大点独立集
- POJ 1466 Girls and Boys【二分图_最大独立集】
- POJ 1466 Girls and Boys(二分图最大独立集)
- POJ-1466-Girls and Boys(二分图最大独立集)
- poj 1466 Girls and Boys 二分图 最大独立集 (★★☆☆☆)
- 【二分图|最大点独立集】POJ-1466 Girls and Boys
- POJ - 1466 Girls and Boys 二分图+最大独立集
- POJ 1466 Girls and Boys(二分图最大独立集)
- POJ1466 Girls and Boys【二分图最大独立集】
- POJ 1466 && ZOJ 1137--Girls and Boys【二分图 && 最大点独立集】
- POJ-1466-Girls and Boys(二分图最大独立集)
- POJ - 1466 Girls and Boys 二分图+最大独立集
- poj 1466 Girls and Boys(二分图的最大独立集)
- poj 1466 Girls and Boys (二分图最大独立集)
- POJ 1466 Girls and Boys(二分图最大独立集)
- HDU1068,POJ1466——Girls and Boys(二分图最大独立集)
- POJ 1466 Girls and Boys(二分图最大独立集)
- POJ 1466:Girls and Boys 二分图的最大点独立集
- poj 1466 Girls and Boys(二分匹配之最大独立集)
- POJ 1466 Girls and Boys 最大独立集