UVA

来源:互联网 发布:linux sleep 单位 编辑:程序博客网 时间:2024/05/21 15:03

原题:

Frank N. Stein is a very conservative high-scho ol teacher. He wants to take some of his students on an
excursion, but he is afraid that some of them might b ecome couples. While you can never exclude this
p ossibility, he has made some rules that he thinks indicates a low probability two p ersons will b ecome
a couple:
Their height differs by more than 40 cm.
They are of the same sex.
Their preferred music style is different.
Their favourite sp ort is the same (they are likely to b e fans of different teams and that would
result in fighting).
So, for any two persons that he brings on the excursion, they must satisfyat 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 integerT 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 integerh 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
24
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
37


题意:
       
N个学生去旅游,给出四个条件,只要两个学生相互间满足四个条件之一就不可同时去,求最多可以去多少个学生。


思路:

       由于条件二性别只有两个,可将男女分开,如果男生和女生无法满足条件,则连接,则每个连接只能去一人,求最大匹配后,总人数减去匹配的人数就是可以同时去的人。

#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long ll;typedef pair <int,int > P;struct People{    int h;    string sex,mu,sp;} peo[515];int V;vector<int> G[515];int match[515];bool used[515];void add_edge(int u,int v){    G[u].push_back(v);    G[v].push_back(u);}bool dfs(int v){    used[v]=true;    for(int i=0;i<G[v].size();i++)    {        int u=G[v][i],w=match[u];        if(w<0||!used[w]&&dfs(w))        {            match[v]=u;            match[u]=v;            return true;        }    }    return false;}int bipartite_matching(){    int res=0;    memset(match,-1,sizeof(match));    for(int v=0;v<V;v++)    {        if(match[v]<0)        {            memset(used,0,sizeof(used));            if(dfs(v))                res++;        }    }    return res;}bool check(int a,int b){    if(abs(peo[a].h-peo[b].h)>40||peo[a].mu!=peo[b].mu||peo[a].sp==peo[b].sp)        return true;    return false;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        for(int i=0;i<515;i++)            G[i].clear();        scanf("%d",&V);        for(int i=0;i<V;i++)        {            cin>>peo[i].h>>peo[i].sex>>peo[i].mu>>peo[i].sp;        }        for(int i=0;i<V;i++)        {            if(peo[i].sex=="M")//从男生向女生连                for(int j=0;j<V;j++)            {                if(peo[j].sex=="F"&&!check(i,j))                    add_edge(i,j);            }        }        int ans=bipartite_matching();        printf("%d\n",V-ans);    }}



原创粉丝点击