POJ 2771 二分图(最大独立集)
2015-07-11 13:48
316 查看
Guardian of Decency
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
Sample Output
Source
Northwestern Europe 2005
题目意思:
有n个人,每个人都有自己的身高、性别、喜欢的音乐、喜欢的运动,当满足以上4个条件中至少一个条件的时候则两个人可以同时被选出来,问最多能选出多少人。
思路:
最大独立集,男生放在左边,女生放在右边,建二分图。ans=n-最大匹配数。
代码:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5244 | Accepted: 2192 |
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
题目意思:
有n个人,每个人都有自己的身高、性别、喜欢的音乐、喜欢的运动,当满足以上4个条件中至少一个条件的时候则两个人可以同时被选出来,问最多能选出多少人。
思路:
最大独立集,男生放在左边,女生放在右边,建二分图。ans=n-最大匹配数。
代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> #include <queue> #include <cmath> #include <set> using namespace std; #define N 505 int max(int x,int y){return x>y?x:y;} int min(int x,int y){return x<y?x:y;} int abs(int x,int y){return x<0?-x:x;} int n, m; bool visited ; vector<int>ve ; int from ; struct node{ int h; char sex[5]; char music[105]; char sport[105]; }a ; int march(int u){ int i, j, k, v; for(i=0;i<ve[u].size();i++){ v=ve[u][i]; if(!visited[v]){ visited[v]=true; if(from[v]==-1||march(from[v])){ from[v]=u; return 1; } } } return 0; } main() { int t, i, j, k; cin>>t; while(t--){ scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d%s%s%s",&a[i].h,a[i].sex,a[i].music,a[i].sport); } for(i=0;i<=n;i++) ve[i].clear(); for(i=0;i<n;i++){ if(a[i].sex[0]=='F'){ for(j=0;j<n;j++){ if(a[j].sex[0]=='M'){ if(abs(a[i].h-a[j].h)<=40&&strcmp(a[i].music,a[j].music)==0&&strcmp(a[i].sport,a[j].sport)){ ve[i].push_back(j); } } } } } int num=0; memset(from,-1,sizeof(from)); for(i=0;i<n;i++){ memset(visited,false,sizeof(visited)); if(march(i)) num++; } printf("%d\n",n-num); } }
相关文章推荐
- Exception testing
- OpenSessionInView的作用
- .net 漏洞处理方法总结
- Android动态调试so 使用 gdb + gdbserver 启动调试的过程
- (转)使用UTL_SMTP包发送邮件
- Matchers and assertthat
- web tips——radio单击区域
- 551 成绩转换
- Android第四十四期 - 登录界 4000 面的动画
- Android第四十四期 - 登录界面的动画
- LeetCode---(107)Binary Tree Level Order Traversal II
- 生成1~n的全排列
- Ruby on Rails Tutorial 第四章 Rails背后的Ruby 之 其他数据类型(二)
- Ignoring a Test
- mysql 分页存储过程
- 【软件】Eclipse 下载
- POJ 3630 Phone List
- 使用UIDataDetectorTypes自动检测电话、网址和邮箱
- sscanf
- Timeout for tests