hdu3829最大独立集

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. 

InputThe 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) 
OutputFor 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.
题意:动物园有猫和狗,一个孩子有喜欢和不喜欢的动物,喜欢猫就一定讨厌狗,反之亦然,现可拿走动物使孩子最高兴
题解:最大独立集,二分图的最大独立集=顶点数-二分图最大匹配,接下来就是建图,把孩子当作点,如果某个孩子喜欢的动物是另一个讨厌的,那么就连边
我们希望减少最小的孩子数来满足所有人的喜好,这就是最小点覆盖。
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const int N=500+5,maxn=100+5,inf=0x3f3f3f3f;

int n,m,p,color[N];
bool used[N],ok[N][N];
struct edge{
   string a,b;
}e[N];

bool match(int x)
{
    for(int i=1;i<=p;i++)
    {
        if(!used[i]&&ok[x][i])
        {
            used[i]=1;
            if(color[i]==-1||match(color[i]))
            {
                color[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>n>>m>>p){
        memset(ok,0,sizeof ok);
        for(int i=1;i<=p;i++)
        {
            cin>>e[i].a>>e[i].b;
            e[i].id=i;
        }
        for(int i=1;i<=p;i++)
            for(int j=1;j<=p;j++)
                if(e[i].a==e[j].b||e[i].b==e[j].a)
                    ok[i][j]=ok[j][i]=1;
 /*       for(int i=1;i<=p;i++)
        {
            for(int j=1;j<=p;j++)
                cout<<ok[i][j];
            cout<<endl;
        }*/
        int ans=0;
        memset(color,-1,sizeof color);
        for(int i=1;i<=p;i++)
        {
            memset(used,0,sizeof used);
            ans+=match(i);
        }
        cout<<p-ans/2<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/acjiumeng/p/6749858.html