二分图最大独立集 二维动态数组 杭电1068

来源:互联网 发布:双卡如何选择网络 编辑:程序博客网 时间:2024/06/12 18:56

点击打开链接

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12171    Accepted Submission(s): 5734


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
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
 

Source
Southeastern Europe 2000
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1150 1151 1281 1507 1528 
做这题需要知道最大独立点集=点数—二分图最大匹配对数,还有一点是这题没有男女分开编号,是混着的所以最后需要除以二才是最大匹配对数
代码如下:
 

#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;int f[505],vis[505];vector<int>ss[505]; //用到二维动态数组 int n,m,k;char s;bool find (int x){for(int j = 0; j<ss[x].size(); j ++){ if(!vis[ss[x][j]]) { vis[ss[x][j]]=1; if(f[ss[x][j]]==-1||find(f[ss[x][j]])) { f[ss[x][j]]=x; return true;}}}return false;}int main(){while(~scanf("%d",&n))//学生人数 {memset(f,-1,sizeof(f));memset(ss,0,sizeof(ss));for(int i = 0; i <n; i ++){scanf("%d%c (%d)",&m,&s,&k);//学号  冒号 喜欢的人数  while(k--){int a;scanf("%d",&a);//喜欢的人的学号 ss[m].push_back(a); }}int sum=0;for(int i = 0; i < n; i ++){memset(vis,0,sizeof(vis));if(find(i))sum++;}printf("%d\n",n-sum/2);}return 0;}



原创粉丝点击