0)
题意:有人互相喜欢,则被认为是恋爱关系,如果我们去掉一些人,使得剩下的人都找不出互相喜欢的,那么剩下的人最多是多少呢?
分析:去掉一些人,去掉的是,经过选择,能形成最多的互相喜欢的情侣对,并从这些情侣对中每对去掉一个人,剩下的单身人数自然最多。这是个求最大独立点集的问题,最大独立点集=节点数-最大匹配数。
注意:之前总是错,是因为在全局中int n,而在局部中,又int n,结果出现了错误,因为局部变量有优先性!
1)
#include <iostream>
#include <stdio.h>
#include <string.h>
//cur 从0开始样例不过!!??
using namespace std;
const int maxn=1010;
//int mat[maxn][maxn];
int node[maxn][maxn];
int used[maxn];
int head[maxn];
int link[maxn*maxn];
int n;
int dfs(int a){
for(int i=0;i<n;i++){
if(!used[i]&&node[a][i]){
used[i]=1;
if(link[i]==-1||dfs(link[i])){
link[i]=a;
return 1;
}
}
}
return 0;
}
int hungary(){
int sum=0;
memset(link,-1,sizeof(link));//从-1开始就是避免数据是从0开始的!!!
for(int i=0;i<n;i++){
memset(used,0,sizeof(used));
if(dfs(i)){
sum++;
}
}
return sum;
}
int main(){
//int n; 局部变量优先!!!
int i,j,num,a,b;
while(~scanf("%d",&n)){
//memset(mat,-1,sizeof(mat));
//int id,num,id2;
//char str;
//cur=1;
memset(node,0,sizeof(node));
for(i=1;i<=n;i++){
scanf("%d: (%d)",&a,&num);
for(int j=0;j<num;j++){
scanf("%d",&b);
node[a][b]=1;
}
}
//cout<<"s"<<endl;
int cnt=hungary();
//cout<<cnt<<endl;
cout<<n-cnt/2<<endl;
}
return 0;
}
2)
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