HDU-2768 Cat vs. Dog

题意一开始是理解错的。。。结果就各种WA啦~

对于两个观众,假如有某只宠物,一个人讨厌另一个人却喜欢,这两个人就是有矛盾的,连边。

最后求最小顶点覆盖。因为把这个覆盖点集去掉的话剩下的图中没有两个点是相连的。

由于不可能有奇数环的出现,总点数减去最大匹配就是答案了。

最小顶点覆盖=总点数-最大匹配(无奇数环)

#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define N 567

using namespace std;
int read()
{
	int x=0, f=0; char ch=getchar();
	while (ch<'0' || ch>'9') { if (ch=='D') f=N; ch=getchar(); }
	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
	return x+f;
}

struct edge{int y, n;} e[N*N*2]; int fir[N], en;
struct node{int x, y;} p[N];
int n, k[N], ans;
bool b[N];

void Add(int x, int y)
{
	en++, e[en].y=y, e[en].n=fir[x], fir[x]=en;
	en++, e[en].y=x, e[en].n=fir[y], fir[y]=en;
}

bool Find(int x)
{
	int o=fir[x], y=e[o].y; 
	while (o)
	{
		if (!b[y]) 
		{ 
			b[y]=1; if (!k[y] || Find(k[y])) { k[y]=x; return true; } 
		}
		o=e[o].n, y=e[o].y;
	}
	return false;
}

int main()
{
	int t=read();
	while (t--)
	{
		rep(i, 1, n) fir[i]=0; en=0; 
		rep(i, 1, n) k[i]=0; ans=0;
		n=read(); n=read(); n=read();
		rep(i, 1, n) { p[i].x=read(), p[i].y=read(); }
		rep(i, 1, n) rep(j, i+1, n) if ((p[i].x==p[j].y || p[i].y==p[j].x)) Add(i, j);
		rep(i, 1, n) if (p[i].x<N)
		{
			rep(j, 1, n) b[j]=0;
			if (Find(i)) ans++;
		}
		printf("%d\n", n-ans);
	}
}

  

转载于:https://www.cnblogs.com/NanoApe/p/4358103.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值