Girls and Boys
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9829 Accepted Submission(s): 4503
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
Source
求一个集合中任意两个元素都无关系,即最大独立集
最大独立集=顶点数-最大匹配数
又因为这个二分图不知道哪个分在哪边,所以把所有顶点都遍历一下,最后把最大匹配数除以二
#include <iostream>
#include <cstring>
#define maxnum 555
using namespace std;
int vis[maxnum];
int link[maxnum];
int head[maxnum];
int cnt;
struct node
{
int v;
int next;
}list[maxnum*maxnum];
void init()
{
cnt = 0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
list[cnt].v = v;
list[cnt].next = head[u];
head[u] = cnt++;
}
int find(int u)
{
int i;
for(i=head[u];~i;i=list[i].next)
{
int v = list[i].v;
if(!vis[v])
{
vis[v] = 1;
if(link[v] == -1 || find(link[v]))
{
link[v] = u;
return 1;
}
}
}
return 0;
}
int match(int N)
{
int i,sum=0;
memset(link,-1,sizeof(link));
for(i=0;i<N;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))
sum++;
}
return sum;
}
int main()
{
int i,j,k,kk;
int a,b;
int len;
int u,v;
int p,n,N;
int flag;
while(~scanf("%d",&N))
{
init();
for(i=1;i<=N;i++)
{
scanf("%d: (%d)",&u,&k);
for(j=1;j<=k;j++)
{
scanf("%d",&v);
add(u,v);
}
}
printf("%d\n",N-(match(N)>>1));
}
return 0;
}