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

    K - Guardian of Decency
    Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Appoint description: 

    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



    今天通过这个题来总结一类题:二分图的最大点独立集

    首先是最大点独立集的概念:在二分图中选取一些点使其能够覆盖所有的边
    另外还有一个概念叫最大点权独立集
    顾名思义就是每个点上赋予了一个权值

    公式: 最大点独立集 = 总点数 - 最大匹配 = 总点数 - 最大流
    最大点权独立集 = 总权值 - 最大流
    有一些题需要拆点来做
    那么用 2n 当做总点数 求出来的最大匹配自然也是2倍 此时的最大点独立集 = (2 * n - 最大匹配) / 2


    题意:
    有一些同学外出游玩
    只要满足一下任何一个条件两个同学就能结伴出去玩
    1身高相差大于40cm
    2性别相同
    3喜欢不同的音乐
    4喜欢不同的体育
    问最多能选出多少个学生外出游玩使其中任意两个学生都满足以上任意一条条件

    分析:
    逆向建边,那么任意两个相连的点都不能够结伴出去
    那么点独立集中的点就代表选出的点之间没有边相连,也就是符合条件的点集
    那么最大点独立集就是所求的结果

    代码:
     1 #include <iostream>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <queue>
     6 using namespace std;
     7 
     8 const int maxn = 505;
     9 
    10 vector<int> G[maxn];
    11 int vis[maxn];
    12 int Link[maxn];
    13 bool Find(int u) {
    14     for(int i = 0; i < G[u].size(); i++) {
    15         int v = G[u][i];
    16         if(!vis[v]) {
    17             vis[v] = 1;
    18             if(Link[v] == -1 || Find(Link[v]) ) {
    19                 Link[v] = u;
    20                 return true;
    21             }
    22         }
    23     }
    24     return false;
    25 }
    26 
    27 int n;
    28 int solve() {
    29     int cnt = 0; memset(Link, -1, sizeof(Link));
    30     for(int i = 1; i <= n; i++) {
    31         if(G[i].size()) {
    32             memset(vis, 0, sizeof(vis));
    33             if(Find(i)) cnt++;
    34         }
    35     }
    36     return cnt;
    37 }
    38 
    39 struct Student {
    40     int high;
    41     char sex;
    42     char music[105];
    43     char sport[105];
    44 }stu[maxn];
    45 
    46 bool check(int i, int j) {
    47     if(fabs(stu[i].high - stu[j].high) > 40) return false;
    48     if(stu[i].sex == stu[j].sex) return false;
    49     if(strcmp(stu[i].music, stu[j].music) != 0) return false;
    50     if(strcmp(stu[i].sport, stu[j].sport) == 0) return false;
    51     return true;
    52 }
    53 
    54 int main() {
    55     int t;
    56     scanf("%d",&t);
    57     while(t--) {
    58         scanf("%d",&n);
    59         for(int i = 1; i <= n; i++) {
    60             G[i].clear();
    61             scanf("%d %c %s %s",&stu[i].high, &stu[i].sex, stu[i].music, stu[i].sport);
    62         }
    63         for(int i = 1; i <= n; i++) {
    64             for(int j = 1; j <= n; j++) {
    65                 if(i == j) continue;
    66                 if(check(i, j)) G[i].push_back(j);
    67             }
    68         }
    69         int ans = solve();
    70 //        printf("solve == %d
    ",ans);
    71         printf("%d
    ", (2 * n - ans) / 2);
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    BIP_开发案例08_BI Publisher图表示例 饼状图/直方图/折线图(案例)
    BIP_开发案例07_将原有Report Builer报表全部转为XML Publisher形式(案例)
    Form_Form Builder Export导出为Excel(案例)
    BIP_开发案例06_以RB.RDF为数据源BIP.RTF为模板的简单例子(案例)
    Form_Form Builder开发基于视图页面和自动代码生成包(案例)
    BIP_开发案例05_BI Pubisher标准做法以BIP.XML为数据源以BIP.RTF为模板的简单例子(案例)
    BIP_开发案例04_通过BI Publisher实现打印报表的二维码(案例)(待整理)
    BIP_开发案例03_将原有Report Builer报表全部转为XML Publisher形式(案例)
    BIP_开发案例02_BI Publisher中复杂案例实现代码(案例)
    BIP_开发案例01_BI Publisher报表手工提交和控制(案例)
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/3924040.html
Copyright © 2011-2022 走看看