Problem Description
Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might become couples. While you can never exclude this possibility, he has made some rules that he thinks indicates a low probability two persons will become a couple:
Their height differs by more than 40 cm.
They are of the same sex.
Their preferred music style is different.
Their favourite sport is the same (they are likely to be fans of different teams and that would result in fighting).
So, for any two persons that he brings on the excursion, they must satisfy at least one of the requirements above. Help him find the maximum number of persons he can take, given their vital information.
Input
The first line of the input consists of an integer T ≤ 100 giving the number of test cases. The first line of each test case consists of an integer N ≤ 500 giving the number of pupils. Next there will be one line for each pupil consisting of four space-separated data items:
an integer h giving the height in cm;
a character 'F' for female or 'M' for male;
a string describing the preferred music style;
a string with the name of the favourite sport.
No string in the input will contain more than 100 characters, nor will any string contain any whitespace.
Output
For each test case in the input there should be one line with an integer giving the maximum number of eligible pupils.
Sample Input
2
4
35 M classicism programming
0 M baroque skiing
43 M baroque chess
30 F baroque soccer
8
27 M romance programming
194 F baroque programming
67 M baroque ping-pong
51 M classicism programming
80 M classicism Paintball
35 M baroque ping-pong
39 F romance ping-pong
110 M romance Paintball
Sample Output
3
7
题意:老师带学生去旅游,但是担心学生会发生恋爱关系,因此带出去的学生彼此间要满足以下条件中的一个:(1.身高相差 40cm 以上、2.同性、3.喜欢音乐风格不同、4.喜欢运动相同),求最多能带多少学生出去
思路:通过公式:二分图最大独立集=顶点数-二分图最大匹配。先求相爱的最大匹配数。
二分图最大独立集=顶点数-二分图最大匹配
独立集:图中任意两个顶点都不相连的顶点集合。
AC代码:
1 #include<algorithm> 2 #include<iostream> 3 #include<vector> 4 #include<stdio.h> 5 #include<string.h> 6 7 using namespace std; 8 #define maxn 888 9 int n,m; 10 vector<int> v[maxn]; 11 int vis[maxn]; 12 int match[maxn]; 13 struct str{ 14 int h; 15 string shengfen; 16 string mus; 17 string sport; 18 }st[maxn]; 19 bool ok(str a,str b){ 20 if(abs(a.h-b.h)<=40&&a.shengfen!=b.shengfen&&a.mus==b.mus&&a.sport!=b.sport) 21 return 1; 22 else 23 return 0; 24 } 25 int dfs(int u){ 26 for(int i=0;i<v[u].size();i++){ 27 int temp=v[u][i]; 28 if(vis[temp]==0){ 29 vis[temp]=1; 30 if(match[temp]==0||dfs(match[temp])){ 31 match[temp]=u; 32 return 1; 33 } 34 } 35 } 36 return 0; 37 } 38 int main(){ 39 int _;cin>>_; 40 while(_--){ 41 scanf("%d",&n); 42 for(int i=0;i<=n;i++) 43 v[i].clear(); 44 for(int i=1;i<=n;i++){ 45 // cin>>st[i].h 46 scanf("%d",&st[i].h); 47 cin>>st[i].shengfen>>st[i].mus>>st[i].sport; 48 //scanf("%s%s%s",st[i].shengfen,st[i].mus,st[i].sport); 49 } 50 for(int i=1;i<=n;i++){ 51 for(int j=1+i;j<=n;j++){ 52 if(ok(st[i],st[j])){ 53 v[i].push_back(j); 54 v[j].push_back(i); 55 } 56 } 57 } 58 for(int i=0;i<=n;i++) 59 match[i]=0; 60 // memset(match,0,sizeof(match)); 61 int ans=0; 62 for(int i=1;i<=n;i++){ 63 //memset(vis,0,sizeof(vis)); 64 for(int i=0;i<=n;i++) 65 vis[i]=0; 66 if(dfs(i)) 67 ans++; 68 } 69 //cout<<ans<<endl; 70 int res=n-ans/2; 71 printf("%d ",res); 72 73 } 74 return 0; 75 }