POJ2771_Guardian of Decency (bipartite graph/Maximum Independent Set = N-maximum matching), decency
Solution report
Http://blog.csdn.net/juncoder/article/details/38159017
Question Portal
Question:
I laughed when I saw the question ,,,
The teacher thinks that the two students are not a pair:
Height differs by more than 40 (neither age nor distance, and what is height)
Different gender (sad, it is not allowed for kiyou to exist, who has dropped the soap ,,,)
I like different types of songs. (You can't ask them to listen to apple all day ,,,,,,)
Like the same sport (they are likely to be fans of different teams and that wowould result in fighting... what is the reason? I like playing with different teams)
Number of people who are not in the same group
Ideas:
Not together represents independent from the relationship of love, that is, to find the largest independent set, any two people in the set do not love each other
Maximum Independent Set = knots-maximum match
Ps: I don't know if it's a write failure. It's just a rewrite. sad...
#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{ int h; char sex; char mus[110]; char spo[110];}M[510],F[510];int n,m,f,mmap[550][550],pre[550],vis[550];int pd(int i,int j){ if( abs(M[i].h - F[j].h) > 40 ) return 0; if( strcmp(M[i].mus,F[j].mus) ) return 0; if( strcmp(M[i].spo,F[j].spo) == 0 ) return 0; return 1;}int dfs(int x){ for(int i=m+1;i<=m+f;i++){ if(!vis[i]&&mmap[x][i]){ vis[i]=1; if(pre[i]==-1||dfs(pre[i])){ pre[i]=x; return 1; } } } return 0;}int main(){ int i,j,t,h; char str[100]; scanf("%d",&t); while(t--){ scanf("%d",&n); m=f=1; memset(mmap,0,sizeof(mmap)); memset(pre,-1,sizeof(pre)); memset(M,0,sizeof(M)); memset(F,0,sizeof(F)); for(i=1;i<=n;i++){ scanf("%d%s",&h,str); if(str[0]=='M'){ scanf("%s%s",M[m].mus,M[m].spo); M[m].h=h; M[m++].sex='M'; } else { scanf("%s%s",F[f].mus,F[f].spo); F[f].h=h; F[f++].sex='F'; } } for(i=1;i<=m;i++){ for(j=1;j<=f;j++){ if(pd(i,j)) mmap[i][m+j]=1; } } int ans=0; for(i=1;i<=m;i++){ memset(vis,0,sizeof(vis)); ans+=dfs(i); } printf("%d\n",n-ans); }}
Guardian of Decency
Time Limit:3000 MS |
|
Memory Limit:65536 K |
Total Submissions:4876 |
|
Accepted:2056 |
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 favorite sport is the same (they are likely to be fans of different teams and that wowould 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 favorite sport.
No string in the input will contain in more than 100 characters, nor will any string contain any whitespace.
Output
For each test case in the input there shocould be one line with an integer giving the maximum number of eligible pupils.
Sample Input
2435 M classicism programming0 M baroque skiing43 M baroque chess30 F baroque soccer827 M romance programming194 F baroque programming67 M baroque ping-pong51 M classicism programming80 M classicism Paintball35 M baroque ping-pong39 F romance ping-pong110 M romance Paintball
Sample Output
37