TOJ--2021 Cat vs. Dog (二分图最大匹配

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37868325/article/details/88408173

题意:

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

思路:这个题很明显的二分图,但是不会建边,,看了题解恍然大悟,,根据每个人不同的喜好,要对人进行分类,对一定不能同时满足的人建边,就是i喜欢的狗正好是j讨厌的狗或者i喜欢的猫正好是j讨厌的猫。。这样求出所有矛盾的人的对数,直接一减就可了,不过建的双向边,减之前要/2.

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
//二分图最大匹配模板,二分图都是无向图
//调用下面算法前,保证本图是二分图
const int maxn=500+5;
struct Max_Match
{
    int n,m;//左右点集大小,点从1开始编号
    vector<int> g[maxn];//g[i]表示左边第i个点邻接的右边点的集合
    bool vis[maxn];//vis[i]表示右边第i个点是否在本次match中被访问过
    int left[maxn];//left[i]==j表右边第i个点与左边第j个点匹配,为-1表无点匹配
    void init(int n,int m)
    {
        this->n=n;this->m=m;
        for(int i=1;i<=n;i++) g[i].clear();
        memset(left,-1,sizeof(left));
    }
    //判断从左u点是否可以找到一条增广路
    bool match(int u)
    {
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {   vis[v]=true;
                if(left[v]==-1 || match(left[v]))//找到增广路
                {
                    left[v]=u;
                    return true;
                }
            }
        }
        return false;
    }//返回当前二分图的最大匹配数
    int solve()
    {
        int ans=0;//最大匹配数
        for(int i=1;i<=n;i++)//每个左边的节点找一次增广路
        {
            memset(vis,0,sizeof(vis));
            if(match(i)) ans++;//找到一条增广路,形成一个新匹配
        }
        return ans;
    }
}MM;
int T,n,m,c,a,b,num[205][205];
struct AA
{
    char ch[5],chh[5];
}pos[505];

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&c);
        MM.init(c,c);
        for(int i=1;i<=c;i++)
        {
            scanf("%s%s",pos[i].ch,pos[i].chh);
        }
        for(int i=1;i<=c;i++)
        {
            for(int j=1;j<=c;j++)
            {
               // if(pos[i].ch[0]==pos[j].chh[0]&&pos[i].ch[1]==pos[j].chh[1])
                //if(pos[i].ch==pos[j].chh||pos[i].chh==pos[j].ch)
                if(strcmp(pos[i].ch,pos[j].chh)==0||strcmp(pos[j].ch,pos[i].chh)==0)
                {
                    MM.g[i].push_back(j);
                    //cout<<i<<" "<<j<<endl;
                }

            }
        }
        printf("%d\n",c-MM.solve()/2);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37868325/article/details/88408173
今日推荐