HDU 3829 Cat VS Dog(最大独立集)

Cat VS Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 3858    Accepted Submission(s): 1387


Problem Description
The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
 

Input
The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
 

Output
For each case, output a single integer: the maximum number of happy children.
 

Sample Input
 
   
1 1 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1
 

Sample Output
 
   
1 3
Hint
Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
 

Source
2011 Multi-University Training Contest 1 - Host by HNU 
 

Recommend
xubiao

题目大意:

    有一些猫和狗,和一些人,每个人有一个喜欢的动物和不喜欢的动物(它们一定是一只猫和一只狗或一只狗和一只猫)。现在要移走一些动物,如果一个人喜欢的动物没有被移走,但不喜欢的被移走了,那么他就很高兴。问最多能让多少人高兴。


解题思路:

    题面非常强烈的诱导我们把猫狗作为结点,把人作为边,但是这样就成了一个点带权的最大独立集,无法求解。这时我们就要考虑把点和边交换。把人作为点,如果两个人的爱好有冲突就在他们之间连边,这样建图最大独立集就是答案。


AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXV=500+3;
pair like[MAXV];//每个人喜欢和不喜欢的动物
int N,M,P;
vector G[MAXV];
int match[MAXV];
bool used[MAXV];

void init()
{
    for(int i=0;i>N>>M>>P)
    {
        init();
        for(int i=0;i>like[i].fi>>like[i].se;
        for(int i=0;i

你可能感兴趣的:(算法,图论)