• POJ 2771 二分图(最大独立集)


    Guardian of Decency
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 5244   Accepted: 2192

    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

     
    题目意思:
    有n个人,每个人都有自己的身高、性别、喜欢的音乐、喜欢的运动,当满足以上4个条件中至少一个条件的时候则两个人可以同时被选出来,问最多能选出多少人。
     
    思路:
    最大独立集,男生放在左边,女生放在右边,建二分图。ans=n-最大匹配数。
     
    代码:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <vector>
     6 #include <queue>
     7 #include <cmath>
     8 #include <set>
     9 using namespace std;
    10 
    11 #define N 505
    12 
    13 int max(int x,int y){return x>y?x:y;}
    14 int min(int x,int y){return x<y?x:y;}
    15 int abs(int x,int y){return x<0?-x:x;}
    16 
    17 int n, m;
    18 bool visited[N];
    19 vector<int>ve[N];
    20 int from[N];
    21 
    22 struct node{
    23     int h;
    24     char sex[5];
    25     char music[105];
    26     char sport[105];
    27 }a[N];
    28 
    29 int march(int u){
    30     int i, j, k, v;
    31     for(i=0;i<ve[u].size();i++){
    32         v=ve[u][i];
    33         if(!visited[v]){
    34             visited[v]=true;
    35             if(from[v]==-1||march(from[v])){
    36                 from[v]=u;
    37                 return 1;
    38             }
    39         }
    40     }
    41     return 0;
    42 }
    43 
    44 main()
    45 {
    46     int t, i, j, k;
    47     cin>>t;
    48     while(t--){
    49         scanf("%d",&n);
    50         for(i=0;i<n;i++){
    51             scanf("%d%s%s%s",&a[i].h,a[i].sex,a[i].music,a[i].sport);
    52         }
    53         for(i=0;i<=n;i++) ve[i].clear();
    54         for(i=0;i<n;i++){
    55             if(a[i].sex[0]=='F'){
    56                 for(j=0;j<n;j++){
    57                     if(a[j].sex[0]=='M'){
    58                         if(abs(a[i].h-a[j].h)<=40&&strcmp(a[i].music,a[j].music)==0&&strcmp(a[i].sport,a[j].sport)){
    59                             ve[i].push_back(j);
    60                         }
    61                     }
    62                 }
    63             }
    64         }
    65         
    66         int num=0;
    67         memset(from,-1,sizeof(from));
    68         for(i=0;i<n;i++){
    69             memset(visited,false,sizeof(visited));
    70             if(march(i)) num++;
    71         }
    72         printf("%d
    ",n-num);
    73     }
    74 }
  • 相关阅读:
    2017-2018-1 20155330 《信息安全系统设计基础》第11周学习总结
    2017-2018-1 20155321 20155330 《信息安全技术》 实验四 木马及远程控制技术
    2017-2018-1 20155330 《信息安全系统设计基础》第10周课堂测试&课下作业
    2017-2018-1 20155330 《信息安全系统设计基础》第9周学习总结
    2017-2018-1 20155321 20155330 《信息安全系统设计基础》实验三——实时系统
    2017-2018-1 20155330 《信息安全系统设计基础》加分项目--实现mypwd
    Web基础
    附录2
    C++中cout.setf()函数
    C++ 输入输出运算符重载
  • 原文地址:https://www.cnblogs.com/qq1012662902/p/4638512.html
Copyright © 2020-2023  润新知