zoukankan      html  css  js  c++  java
  • POJ 2771 Guardian of Decency 【最大独立集】

    传送门:http://poj.org/problem?id=2771

    Guardian of Decency
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 6456   Accepted: 2668

    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 个学生,依次给出他们的身高、性别、喜欢音乐的类型、喜欢的运动。

    老师给出了 四条 不可能成为亲密关系的条件。老师想带尽可能多的学生,但是有不想他们发展成为亲密恋人(两两至少满足一条上述的条件)。

    问最多能带多少学生?

    解题思路:

    我们把条件反过来建图,即有可能成为亲密关系的连边。

    那么就转换为求他们的最大独立集了。

    AC code:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 #define INF 0x3f3f3f3f
     6 using namespace std;
     7 const int MAXN = 505;
     8 struct date
     9 {
    10     int h;
    11     char mf[2], music[101], spo[101];
    12 }pp[MAXN];
    13 
    14 struct Edge
    15 {
    16     int v, nxt;
    17 }edge[MAXN*MAXN];
    18 
    19 int head[MAXN],cnt;
    20 int linker[MAXN];
    21 bool used[MAXN];
    22 int N;
    23 
    24 void init()
    25 {
    26     memset(head, -1, sizeof(head));
    27     memset(linker, -1, sizeof(linker));
    28     memset(edge, 0, sizeof(edge));
    29     cnt = 0;
    30 }
    31 
    32 void add(int from, int to)
    33 {
    34     edge[cnt].v = to;
    35     edge[cnt].nxt = head[from];
    36     head[from] = cnt++;
    37 }
    38 
    39 bool Find(int x)
    40 {
    41     int v;
    42     for(int i = head[x]; i != -1; i = edge[i].nxt){
    43         v = edge[i].v;
    44         if(!used[v]){
    45             used[v] = true;
    46             if(linker[v] == -1 || Find(linker[v])){
    47                 linker[v] = x;
    48                 return true;
    49             }
    50         }
    51     }
    52     return false;
    53 }
    54 
    55 bool chk(date a, date b)
    56 {
    57     if(abs(a.h-b.h) <= 40){
    58         if(a.mf[0] != b.mf[0]){
    59             if(strcmp(a.music, b.music) == 0){
    60                 if(strcmp(a.spo, b.spo) != 0){
    61                     return true;
    62                 }
    63             }
    64         }
    65     }
    66     return false;
    67 }
    68 
    69 int main()
    70 {
    71     int T_case;
    72     scanf("%d", &T_case);
    73     while(T_case--){
    74         init();
    75         scanf("%d", &N);
    76         for(int i = 1; i <= N; i++){
    77             scanf("%d %s %s %s", &pp[i].h, &pp[i].mf, &pp[i].music, &pp[i].spo);
    78             for(int j = 1; j < i; j++){
    79                 if(chk(pp[i], pp[j])){
    80                     add(i, j);
    81                     add(j, i);
    82                 }
    83             }
    84         }
    85         int ans = 0;
    86         for(int i = 1; i <= N; i++){
    87             memset(used, 0, sizeof(used));
    88             if(Find(i)) ans++;
    89         }
    90         printf("%d
    ", N-ans/2);
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    第2课 C 到 C++ 的升级
    第1课 学习 C++ 的意义
    归并排序
    插入排序与希尔排序
    选择排序
    冒泡排序
    CodeSignal 刷题 —— almostIncreasingSequence
    CodeSignal 刷题 —— matrixElementSum
    Python3 序列解包
    单星号变量(*)和双星号变量(**)的用法
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9996134.html
Copyright © 2011-2022 走看看