Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5513 | Accepted: 2319 |
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:
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.
- 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:
No string in the input will contain more than 100 characters, nor will any string contain any whitespace.
- 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
Source
求二分图最大独立集。给字符串都做好映射,然后根据题目中条件,把可能组CP的人之间连边,跑匈牙利算法就行。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<cstring> 6 #include<string> 7 #include<vector> 8 #include<map> 9 using namespace std; 10 const int mxn=2400; 11 struct stu{ 12 int h;//高度 13 bool se;//性别 14 int m,sp; 15 }a[mxn]; 16 map<string,int> mpm,mpsp; 17 vector<int>e[mxn]; 18 int mcnt=0,spcnt=0; 19 int n; 20 // 21 int link[mxn],vis[mxn]; 22 // 23 void clear(){ 24 mpm.clear(); 25 mpsp.clear(); 26 memset(a,0,sizeof a); 27 memset(link,0,sizeof link); 28 for(int i=0;i<=n;i++) e[i].clear(); 29 mcnt=spcnt=0; 30 } 31 bool dfs(int s){//匈牙利算法 32 int i,j; 33 for(i=0;i<e[s].size();i++){ 34 int v=e[s][i]; 35 if(!vis[v]){ 36 vis[v]=1; 37 if(!link[v] || dfs(link[v])){ 38 link[v]=s; 39 return 1; 40 } 41 } 42 } 43 return 0; 44 } 45 void calc(){ 46 int i,j; 47 int ans=n; 48 for(i=1;i<=n;i++){ 49 if(a[i].se){ 50 memset(vis,0,sizeof vis); 51 if(dfs(i))ans--; 52 } 53 } 54 printf("%d ",ans); 55 } 56 int main(){ 57 int T; 58 scanf("%d",&T); 59 char sex[5],music[60],sport[60]; 60 while(T--){ 61 scanf("%d",&n); 62 clear(); 63 int i,j; 64 for(i=1;i<=n;i++){ 65 cin>>a[i].h>>sex>>music>>sport; 66 if(sex[0]=='M') a[i].se=1; 67 else a[i].se=0; 68 if(!mpm.count(music)){//映射音乐 69 mcnt++; 70 mpm[music]=mcnt; 71 } 72 a[i].m=mpm[music]; 73 if(!mpsp.count(sport)){//映射运动 74 spcnt++; 75 mpsp[sport]=spcnt; 76 } 77 a[i].sp=mpsp[sport]; 78 // printf("test : %d %d %d ",a[i].se,a[i].m,a[i].sp); 79 } 80 for(i=1;i<n;i++) 81 for(j=i+1;j<=n;j++){ 82 if((abs(a[i].h-a[j].h)<=40)&&(a[i].se!=a[j].se)&&(a[i].m==a[j].m)&&(a[i].sp!=a[j].sp)){//连边条件 83 e[i].push_back(j); 84 e[j].push_back(i); 85 } 86 } 87 calc(); 88 } 89 return 0; 90 }