Maximum independence set of a bipartite graph = point N-maximum matching of a bipartite graph. After knowing this formula, this question is very easy. It is a bare question. The most basic binary graph is the largest independent set, and the graph is built directly... Question:
Girls and boys
| Time limit:5000 Ms |
|
Memory limit:10000 K |
| Total submissions:7452 |
|
Accepted:3242 |
Description
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.
Input
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 (N <= 500), for N subjects.
Output
For each given data set, the program shocould write to standard output a line containing the result.
Sample Input
70: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0
Sample output
52
AC code:
#include <iostream>#include <cstdio>#include <algorithm>#include <string.h>#include <vector>using namespace std;vector<int> ss[505];int flag[505],visted[505];bool dfs(int x){for(int i=0;i<ss[x].size();++i){if(!visted[ss[x][i]]){ visted[ss[x][i]]=1; if(flag[ss[x][i]]==-1||dfs(flag[ss[x][i]])){ flag[ss[x][i]]=x;return true; }}}return false;}int main(){ int n; while(~scanf("%d",&n)){ memset(ss,0,sizeof(ss));memset(flag,-1,sizeof(flag));int x,num,y,m=n;char ch1,ch2,ch3;while(m--){ cin>>x>>ch1>>ch2>>num>>ch3; while(num--){ scanf("%d",&y); ss[x].push_back(y); }}int sum=0;for(int i=0;i<n;++i){ memset(visted,0,sizeof(visted)); if(dfs(i)) sum++;}printf("%d\n",n-sum/2); } return 0;}