poj——2771 Guardian of Decency
阿新 • • 發佈:2017-07-03
ever ram person 個學生 不能 info 算法 else som poj——2771 Guardian of Decency
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.
No string in the input will contain more than 100 characters, nor will any string contain any whitespace.
2>兩人性別相同;
3>兩人所喜歡的音樂風格不同;
4>兩人最喜愛的運動相同;
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5916 | Accepted: 2458 |
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
Source
Northwestern Europe 2005 題目大意:題意:現在有一個老師想帶領他的學生出去郊遊,但是他非常擔心在郊遊的過程中有些學生會發生戀愛關系,而他認為發生戀愛關系的可能性比較小的判斷標準有以下四個,如果滿足四個條件中的任何一個,即被他認為可能發生戀愛關系的可能性比較小:
1>兩人身高的差距超過40cm;2>兩人性別相同;
3>兩人所喜歡的音樂風格不同;
4>兩人最喜愛的運動相同;
現在給出n個學生,並給出每個學生的信息(信息為:身高 性別 所喜歡的音樂風格 喜愛的運動),要求求解最大可以帶出去郊遊的學生數.
思路: 由於兩個女生一定不會發生戀愛關系(此處我們不考慮性取向有問題的情況(ORZ)),所以我們可以以該同學的性別為根據來建立二分圖。 然後我們在判斷兩邊的同學是否滿足上述情況,若滿足則連邊。然後對於我們建出的這個圖在求最大匹配。這樣我們就轉化成了裸地匈牙利算法。最多可以帶的學生數=總學生數-最大匹配數。 註意:char不能用==直接比,這樣比出來的只是第一個字符,而非全部,我們這裏要用strcmp比較兩個字符的大小,這兩個字符的字典序如果相同則返回0,如果第一個字符的字典序大返回1,反之,返回-1。 代碼:#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 510 using namespace std; char ch; bool vis[N]; int t,n,x,ans,gnum,bnum,pre[N],map[N][N]; struct nn { int h; char fm[10000]; char fs[10000]; }girl[N],boy[N]; int read() { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘) f=-1; ch=getchar();} while(ch<=‘9‘&&ch>=‘0‘){x=x*10+ch-‘0‘; ch=getchar();} return x*f; } void add_edge() { for(int i=1;i<=bnum;i++) for(int j=1;j<=gnum;j++) if(!strcmp(boy[i].fm,girl[j].fm)&&strcmp(boy[i].fs,girl[j].fs)&&abs(boy[i].h-girl[j].h)<=40) map[i][j]=1; } int find(int x) { for(int i=1;i<=gnum;i++) if(!vis[i]&&map[x][i]) { vis[i]=true; if(pre[i]==0||find(pre[i])) { pre[i]=x; return 1; } } return 0; } int main() { t=read(); while(t--) { ans=0;bnum=0,gnum=0; memset(boy,0,sizeof(boy)); memset(girl,0,sizeof(girl)); memset(pre,0,sizeof(pre)); memset(map,0,sizeof(map)); n=read(); for(int i=1;i<=n;i++) { x=read(); scanf("%c",&ch); if(ch==‘F‘) { girl[++gnum].h=x; cin>>girl[gnum].fm; cin>>girl[gnum].fs; } else { boy[++bnum].h=x; cin>>boy[bnum].fm; cin>>boy[bnum].fs; } } add_edge(); for(int i=1;i<=bnum;i++) { memset(vis,0,sizeof(vis)); if(find(i)) ans++; } printf("%d\n",n-ans); } return 0; }
poj——2771 Guardian of Decency