hdu 2768 Cat vs. Dog(二分图最大匹配--最大独立点集)
2015-04-13 23:46
375 查看
Cat vs. Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1692 Accepted Submission(s): 639
Problem Description
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
Source
NWERC 2008
题目分析:
把投票人当作点,当一个点的首等于另一个点的尾,那么两点的矛盾,建边,要求不矛盾的最多人数,就是求最大独立点集
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> #define MAX 507 using namespace std; int t,n,m,k; int type[2][MAX]; int num[2][MAX]; char s[100]; int get ( char ch ) { if ( ch == 'C' ) return 0; else return 1; } struct Edge { int v,next; }e[MAX*MAX]; int head[MAX]; int cc; void add ( int u , int v ) { e[cc].v = v; e[cc].next = head[u]; head[u] = cc++; } int linker[MAX]; int used[MAX]; bool dfs ( int u ) { for ( int i = head[u] ; ~i ; i = e[i].next ) { int v = e[i].v; if ( used[v] ) continue; used[v] = true; if ( linker[v] == -1 || dfs ( linker[v] ) ) { linker[v] = u; return true; } } return false; } int hungary ( ) { int res = 0; memset ( linker , -1 , sizeof ( linker ) ); for ( int i = 1 ; i <= k ; i++ ) { memset ( used , 0 , sizeof ( used ) ); if ( dfs ( i ) ) res++; } return res/2; } int main ( ) { scanf ( "%d" , &t ); while ( t-- ) { memset ( head , -1 , sizeof ( head ) ); cc = 0; scanf ( "%d%d%d" , &n , &m , &k ); for ( int i = 1 ; i <= k ; i++ ) { scanf ( "%s" , s ); type[0][i] = get(s[0]); int j = 1; int sum = 0; while ( s[j] ) sum = sum*10 + s[j++] - 48; num[0][i] = sum; scanf ( "%s" , s ); type[1][i] = get(s[0]); j = 1; sum = 0; while ( s[j] ) sum = sum*10 + s[j++] - 48; num[1][i] = sum; } for ( int i = 1 ; i <= k ; i++ ) for ( int j = 1 ; j <= k ; j++ ) { if ( type[1][i] != type[0][j] ) continue; if ( num[1][i] != num[0][j] ) continue; add ( i , j ); add ( j , i ); } printf ( "%d\n" , k-hungary() ); } }
相关文章推荐
- hdu-2768-Cat vs. Dog(二分图-最大匹配数)
- HDU 3829 Cat VS Dog(最大独立集|二分图最大匹配)
- hdu 2768 Cat vs. Dog (最大独立)
- HDU 2768 Cat vs. Dog 【二分匹配之最大独立集】
- HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)
- hdu 2768 Cat vs. Dog (最大独立)
- HDU 3829 Cat VS Dog (最大独立点集)
- HDU 3829 - Cat VS Dog【二分图最大匹配最大独立集】
- hdu 2768 Cat vs. Dog 最大独立集(最大匹配)
- HDU 3829 Cat VS Dog-二分匹配&最大点集
- HDOJ 2768 - Cat vs. Dog 构图解二分图的最大独立集
- hdu 2768 Cat vs. Dog(最大独立集)
- HDU 3829 Cat VS Dog(二分图最大独立集)
- hdu 2768 Cat vs. Dog【最大独立集】
- Hdu3829 Cat VS Dog(最大独立点集)
- HDU 2768 Cat vs Dog(最大独立集)
- hdu 2768 Cat vs. Dog (二分匹配)
- hdu 3829 Cat VS Dog (二分图求最大独立集)
- hdu 2768 Cat vs. Dog (二分匹配)
- hdu 3829 Cat VS Dog 【二分图求最大独立集】