您的位置:首页 > 其它

(beginer) 二分图(最大独立集) UVA 12168 Cat vs. Dog

2014-02-08 09:55 471 查看

Problem C - Cat vs. Dog

Time limit: 2 seconds

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

The 2008 ACM Northwestern European Programming Contest
题意:有v个观众要投票留下一只猫,抛弃一只狗或者留下一只狗,抛弃一只猫,你要尽可能满足他们的要求,问最多能满足多少人的意愿。
思路:二分图的思路感觉都好难想啊。。汗颜。。。。我们把人当做点,如果两个人的喜好有冲突,那么我们就连一条边,最后这些人可以分成两个集合的,分成喜欢狗和喜欢猫两个集合。所以是二分图,然后找最大独立集就行了。然后 最大独立集=点数-最大匹配。。。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 510;
int a , b , m;
int link[maxn];
bool vis[maxn];
vector<int> cat[2][110];
vector<int> dog[2][110];
bool g[maxn][maxn];
vector<int> G[maxn];
vector<int> S;

void input()
{
S.clear();
for (int i = 0 ; i < 2 ; ++i)
{
for (int j = 0 ; j < 110 ; ++j)
{
cat[i][j].clear();
dog[i][j].clear();
}
}
for (int i = 0 ; i < m ; ++i) G[i].clear();
char buffer[20];
char pet; int x;
memset(g,false,sizeof(g));
for (int i = 0 ; i < m ; ++i) {
scanf("%s",buffer);
sscanf(buffer,"%c%d",&pet,&x);
if (pet=='C') {
S.push_back(i);
for (int j = 0 ; j < cat[1][x].size() ; ++j) {
if (g[i][cat[1][x][j]]) continue;
G[i].push_back(cat[1][x][j]);
G[cat[1][x][j]].push_back(i);
g[i][cat[1][x][j]] = g[cat[1][x][j]][i] = true;
}
cat[0][x].push_back(i);
} else {
for (int j = 0 ; j < dog[1][x].size() ; ++j) {
if (g[i][dog[1][x][j]]) continue;
G[i].push_back(dog[1][x][j]);
G[dog[1][x][j]].push_back(i);
g[i][dog[1][x][j]] = g[dog[1][x][j]][i] = true;
}
dog[0][x].push_back(i);
}
scanf("%s",buffer);
sscanf(buffer,"%c%d",&pet,&x);
if (pet=='D') {
for (int j = 0 ; j < dog[0][x].size() ; ++j) {
int y = dog[0][x][j];
if (g[i][y]) continue;
G[i].push_back(y);
G[y].push_back(i);
g[i][y] = g[y][i] = true;
}
dog[1][x].push_back(i);
} else {
for (int j = 0 ; j < cat[0][x].size() ; ++j) {
int y = cat[0][x][j];
if (g[i][y]) continue;
G[i].push_back(y);
G[y].push_back(i);
g[i][y] = g[y][i] = true;
}
cat[1][x].push_back(i);
}
}
}

bool dfs(int x)
{
if (vis[x]) return false;
vis[x] = true;
for (int i = 0 ; i < G[x].size() ; ++i) {
int y = G[x][i];
if (vis[y]) continue;
if (link[y]==-1 || dfs(link[y])) {
link[y] = x;
return true;
}
}
return false;
}

void solve()
{
memset(link,-1,sizeof(link));
int ans = m;
for (int i = 0 ; i < S.size() ; ++i)
{
memset(vis,0,sizeof(vis));
if (dfs(S[i])) --ans;
}
printf("%d\n",ans);
}

int main()
{
int T; cin>>T;
while (T--)
{
scanf("%d%d%d",&a,&b,&m);
input();
solve();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: