Girls and Boys

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
题意:搞基,求最大的非基友的集合数量
题解:最大独立团=定点数-最大匹配,其实要除以二因为匹配应为对数。
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int p[500][500];
int link[500];
int used[500];
int n,m;
int x,y;
bool dfs(int u)
{
 int v;
 for(v=0;v<n;v++)
 {
  if(p[u][v]&&!used[v])
  {
   used[v]=true;
   if(link[v]==-1||dfs(link[v]))
   {
    link[v]=u;
    return true;
   }
  }
 }
 return false;
}
int cnt;
int hungry()
{
 int ret=0;
 int u;
 memset(link,-1,sizeof(link));
 for(u=0;u<n;u++)
 {
  memset(used,0,sizeof(used));
  if(dfs(u)) ret++;
 }
 return ret;
}
int main()
{
 int i,m,xp;
 string s;
 int num,j,x;
   while(scanf("%d",&n)!=EOF)
   {
   getchar();
   memset(p,0,sizeof(p));
     for(i=0;i<n;i++)
  {
   scanf("%d: (%d)",&i,&num);
   for(j=0;j<num;j++)
   {
   scanf(" %d",&x);
    p[i][x]=1;
   }
  }
     cout<<n-hungry()/2<<endl;
   }
 return 0;
}
posted @ 2013-09-18 17:48  forevermemory  阅读(277)  评论(0编辑  收藏  举报