国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU2768-二分圖最大獨立集

2019-11-08 19:26:30
字體:
來源:轉載
供稿:網友

Cat vs. Dog

Time Limit: 2000/1000 MS (java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2113    Accepted Submission(s): 817PRoblem DescriptionThe 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. InputOn 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. OutputPer testcase:* One line with the maximum possible number of satisfied voters for the show. Sample Input
21 1 2C1 D1D1 C11 2 4C1 D1C1 D1C1 D2D2 C1 Sample Output
13 

題目大意:

                  給你n只貓和m條狗,編號分別為1-n和1-m,然后有一場演出由一只貓和一條狗組成,有k個觀眾,他們希望哪一個應該出演,不希望看到哪一個出演,具體見題目描述,問怎么安排演出才能滿足大多數的觀眾并輸出最多的滿足的觀眾數

題目思路:

                  剛開始看到時沒有啥思路,然后想想題目要求最多的觀眾,如果我們貓和狗為集合建立二分圖的話,最后得到的是貓和狗的最大匹配而不是最多滿足的觀眾數,所以我們換著方式,以觀眾為集合建立二分圖,然后我們考慮如何來建圖,首先每個觀眾有自己喜歡的和不喜歡的,如果兩個觀眾一個喜歡貓1一個不喜歡貓1或者是一個不喜歡狗1一個喜歡狗1,那么必然這兩個觀眾的觀點存在矛盾,他們兩個只能滿足一個,既然這樣,我們就很好想到讓有矛盾的觀眾連邊,這樣最后就轉換成求最大獨立集了!

AC代碼:

#include<cstring>#include<cstdio>const int maxn = 1005;bool vis[maxn],mp[maxn][maxn];int link[maxn];int n,m,k;bool dfs(int u){    for(int i=1;i<=k;i++){        if(!vis[i]&&mp[u][i]){            vis[i]=true;            if(link[i]==-1||dfs(link[i])){                link[i]=u;                return true;            }        }    }    return false;}int main(){    int t;scanf("%d",&t);    while(t--){        scanf("%d%d%d",&n,&m,&k);        memset(link,-1,sizeof(link));        memset(mp,false,sizeof(mp));        int num1[1005],num2[1005];        char s1[1005][10],s2[1005][10];        for(int j=1;j<=k;j++){            scanf("%s %s",s1[j],s2[j]);            num1[j]=0,num2[j]=0;            int i=1;            while(s1[j][i])num1[j]=num1[j]*10+s1[j][i]-'0',i++;            i=1;            while(s2[j][i])num2[j]=num2[j]*10 +s2[j][i]-'0',i++;            for(int l=1;l<j;l++){                if(num1[l]==num2[j]&&s1[l][0]==s2[j][0]||num2[l]==num1[j]&&s2[l][0]==s1[j][0])                    mp[l][j]=mp[j][l]=true;            }        }        int ans = 0;        for(int i=1;i<=k;i++){            memset(vis,false,sizeof(vis));            if(dfs(i))ans++;        }        printf("%d/n",k-ans/2);    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 祁门县| 汝南县| 绵阳市| 全州县| 山丹县| 祁连县| 金沙县| 金堂县| 平顺县| 永兴县| 江口县| 巧家县| 美姑县| 荥经县| 东城区| 花莲市| 兴安县| 阜新| 渝中区| 蒲江县| 平谷区| 高雄县| 鄯善县| 肥东县| 徐闻县| 六盘水市| 东港市| 阿拉善右旗| 阳朔县| 凤庆县| 西峡县| 普兰县| 邻水| 镇平县| 德令哈市| 博罗县| 万州区| 红原县| 确山县| 东乌珠穆沁旗| 西充县|