zoukankan      html  css  js  c++  java
  • uva 12168 最大匹配 && 最大独立集

    Problem C - Cat vs. Dog

    Time limit: 2 seconds

    The latest reality show has hit the TV: ``Cat vs. Dog''. In this show, a bunch of cats and dogs compete for the very prestigious Best Pet Ever title. In each episode, the cats and dogs get to show themselves off, after which the viewers vote on which pets should stay and which should be forced to leave the show.

    Each viewer gets to cast a vote on two things: one pet which should be kept on the show, and one pet which should be thrown out. Also, based on the universal fact that everyone is either a cat lover (i.e. a dog hater) or a dog lover (i.e. a cat hater), it has been decided that each vote must name exactly one cat and exactly one dog.

    Ingenious as they are, the producers have decided to use an advancement procedure which guarantees that as many viewers as possible will continue watching the show: the pets that get to stay will be chosen so as to maximize the number of viewers who get both their opinions satisfied. Write a program to calculate this maximum number of viewers.

    Input

    On the first line one positive number: the number of testcases, at most 100. After that per testcase:

    • One line with three integers c, d, v (1 ≤ c, d ≤ 100 and 0 ≤ v ≤ 500): the number of cats, dogs, and voters.
    • v lines with two pet identifiers each. The first is the pet that this voter wants to keep, the second is the pet that this voter wants to throw out. A pet identifier starts with one of the characters `C' or `D', indicating whether the pet is a cat or dog, respectively. The remaining part of the identifier is an integer giving the number of the pet (between 1 and c for cats, and between 1 and d for dogs). So for instance, ``D42'' indicates dog number 42.

    Output

    Per testcase:

    • One line with the maximum possible number of satisfied voters for the show.

    Sample Input

    2
    1 1 2
    C1 D1
    D1 C1
    1 2 4
    C1 D1
    C1 D1
    C1 D2
    D2 C1
    

    Sample Output

    1
    3
    
    The 2008 ACM Northwestern European Programming Contest

    参考:http://blog.csdn.net/jayye1994/article/details/10050695

    最大独立集 = 总结点数 - 最大匹配数

    设X集合中人为喜欢狗的voter,Y集合中为喜欢猫的voter,引弧喜欢狗的voter(X)到讨厌的猫的喜欢者voter(Y);

    若某人i喜欢cat ,且讨厌被集合{voters like dog}dog,则从该集合的每个人引弧至i,用邻接表的话,注意判重。

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    #define MAXN 555
    #define PETNUM 111
    
    vector<int> ldog[PETNUM], lcat[PETNUM], g[MAXN];
    vector<int> votedog;
    int hcat[MAXN], hdog[MAXN];
    
    int c, d, v, lft[MAXN];
    bool vis[MAXN];
    
    bool match(int u){                              //最大匹配
        for(int i=0; i<g[u].size(); i++){
            int v = g[u][i];
            if(vis[v]) continue;        vis[v]=true;
            if(lft[v]==-1 || match(lft[v])){
                lft[v]=u;
                return true;
            }
        }
        return false;
    }
    
    bool have[MAXN][MAXN];
    int solve(){
        int i, j, k;
    
        for(i=1; i<=v; i++){
            g[i].clear();
            fill_n(have[i]+1, v, false);
        }
        for(i=1; i<=d; i++)
            for(j=0; j<ldog[i].size(); j++){
                int liker = ldog[i][j];
                int cat = hcat[liker];
                for(k=0; k<lcat[cat].size(); k++){
                    int lit = lcat[cat][k];
                    if(have[liker][lit]) continue;         //判重
                    g[liker].push_back(lit);
                    have[liker][lit]=true;
                }
            }
    
        for(i=1; i<=c; i++)
            for(j=0; j<lcat[i].size(); j++){
                int liker = lcat[i][j];
                int dog = hdog[liker];
                for(k=0; k<ldog[dog].size(); k++){
                    int lig = ldog[dog][k];
                    if(have[lig][liker]) continue;          //--
                    g[lig].push_back(liker);
                    have[lig][liker]=true;
                }
            }
        int ans=0;
        fill_n(lft+1, v, -1);
        for(i=0; i<votedog.size(); i++){
            fill_n(vis+1, v, false);
            if(match(votedog[i])) ans++;
        }
        return v-ans;
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T;
        scanf(" %d", &T);
        while(T--){
            int i, j;
            scanf(" %d %d %d", &c, &d, &v);
            for(i=1; i<=c; i++) lcat[i].clear();
            votedog.clear();
            for(i=1; i<=d; i++) ldog[i].clear();
    
            for(i=1; i<=v; i++){
                char p1, p2;            int n1, n2;
                scanf(" %c%d %c%d", &p1, &n1, &p2, &n2);
    //            printf("%c%d %c%d
    ", p1, n1, p2, n2);
                if(p1=='C'){
                    lcat[n1].push_back(i);              //i喜欢cat n1
                    hdog[i]=n2;
                }
                else{
                    votedog.push_back(i);               //喜欢狗的人们。。。
                    ldog[n1].push_back(i);
                    hcat[i]=n2;
                }
            }
            printf("%d
    ", solve());
        }
        return 0;
    }
    
  • 相关阅读:
    数据库与数据仓库的比较Hbase——Hive
    log4j 配置使用
    hadoop Datanode Uuid unassigned
    kafka相关文章引用
    kafka可靠性
    kafka基本原理
    html
    并发编程
    Python之系统交互(subprocess)
    网络编程
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3362370.html
Copyright © 2011-2022 走看看