UVa live3415Guardian of Decency(二分最大匹配之最大独立点集)

来源:互联网 发布:医院预约管理系统php 编辑:程序博客网 时间:2024/05/01 13:42

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1416

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 ghting).
So, for any two persons that he brings on the excursion, they must satisfy at least one of the
requirements above. Help him nd the maximum number of persons he can take, given their vital
information.
Input
The rst line of the input consists of an integer T  100 giving the number of test cases. The rst 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

这题是大白书上的习题,一道最大独立点集。建图药反建,不符合要求的的连一条边,最终求得的最大独立点集就是满足要求的最多的。代码如下:

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <algorithm>using namespace std;struct node{    int u, v;    int next;}edge[500000];struct N{    int high;    char c;    char s1[200];    char s2[200];}ren[5000];int head[1000], link[1000], vis[1000], n, cnt;void add(int u, int v){    edge[cnt].v=v;    edge[cnt].next=head[u];    head[u]=cnt++;}int dfs(int u){    int i;    for(i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(!vis[v])        {            vis[v]=1;            if(link[v]==-1||dfs(link[v]))            {                link[v]=u;                return 1;            }        }    }    return 0;}void hungary(){    int i, ans=0;    memset(link,-1,sizeof(link));    for(i=0;i<n;i++)    {        memset(vis,0,sizeof(vis));        if(dfs(i))            ans++;    }    printf("%d\n",n-ans/2);}int main(){    int i, j, t;    scanf("%d",&t);    while(t--)    {        cnt=0;        memset(head,-1,sizeof(head));        scanf("%d",&n);        for(i=0;i<n;i++)        {            scanf("%d %c %s %s",&ren[i].high,&ren[i].c,ren[i].s1,ren[i].s2);            for(j=0;j<i;j++)            {                if(abs(ren[i].high-ren[j].high)<=40&&ren[j].c!=ren[i].c&&!strcmp(ren[i].s1,ren[j].s1)&&strcmp(ren[i].s2,ren[j].s2))                {                    add(i,j);                    add(j,i);                }            }        }        hungary();    }    return 0;}


0 0
原创粉丝点击