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

描述

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.

输入

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line with three integers cdv (1 ≤ cd ≤ 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.

输出

Per testcase:

One line with the maximum possible number of satisfied voters for the show.

样例输入

2

1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2

D2 C1

样例输出

1

3

题还是二分图模板题..就是输入的时候是%s,然后题意绕了一点,是输入一个想看的和不想看的,所以之后有一步骤是匹配,要找到两个人的想看和不想看是完全相悖的,这样就可以在二分图相连了。然后最大匹配之后,输出要处理一下。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
int ma[505][505],vis[505],dx[505],c,d,k;
int find(int i)
{
	for(int j=1;j<=k;j++)
	{
		if(vis[j]==0&&ma[i][j])
		{
			vis[j]=1;
			if(dx[j]==0||find(dx[j]))
			{
				dx[j]=i;
				return 1;
			}
		}
	}
	return 0; 
}
int main()
{
	int t,i,j;
	char x[505][10],y[505][10];
	scanf("%d",&t);
	while(t--)
	{
		//ma[i][j]是第i/j个人 
		memset(ma,0,sizeof ma);
		memset(vis,0,sizeof vis);
		memset(dx,0,sizeof dx);
		scanf("%d%d%d",&c,&d,&k);
		for(i=1;i<=k;i++)
		scanf("%s%s",x[i],y[i]);  
        for(i=1;i<=k;i++)  
        {  
            for(j=1;j<=k;j++)  
            {  
                if(strcmp(x[i],y[j])==0||strcmp(x[j],y[i])==0) 
                ma[i][j]=1;//两个人支持的猫狗完全相反,则连接 
            }  
        }  
		int s=0;
		for(i=1;i<=k;i++)
		{
			memset(vis,0,sizeof vis);
			s+=find(i);
		}
		//s是最大匹配,就是选择都是相悖的人
		//总人数-相悖的人的一半 
		printf("%d\n",k-s/2);
	}
}

猜你喜欢

转载自blog.csdn.net/thewise_lzy/article/details/79857806