洛谷P1640 [SCOI2010]连续攻击游戏

每件物品只能选一次,每个属性只能选一次

这不是明显附合二分图匹配中两边的点的性质嘛

将装备和属性连边,从属性为1的点开始匹配,不能匹配就输出答案

PS.这题可以用并查集实现

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define For(i,j,k) for(int i=j;i<=k;++i)
using namespace std;
const int N=2000005;
int h[N],cnt;
struct node{
	int next;
	int to;
	}a[N];
void addvage(int x,int y){
	cnt++;a[cnt].next=h[x];a[cnt].to=y;h[x]=cnt;
	}
int w[N],vis[N];
inline int read(){
	char c=getchar();
    while(c<'0'||c>'9')c=getchar();
	int x=c-'0';
	c=getchar();
	while(c<='9'&&c>='0'){x=x*10+c-'0';c=getchar();}
	return x;
	}
int n;
int timee;
int hunger(int x){
	for(int i=h[x];i;i=a[i].next){
		int y=a[i].to;
		if(vis[y]==timee)continue;
		vis[y]=timee;
		if(w[y]==-1||hunger(w[y])){
			w[y]=x;
			return 1;
			}
		}
	return 0;
}
int main(){
	n=read();
	int x,y;
	For(i,1,n){
		x=read();y=read();
		addvage(x,i);
		addvage(y,i);
		}
	For(i,1,n)w[i]=-1;
	int ans=0;
	For(i,1,n){
		timee=i;
		if(hunger(i))ans++;
		else {cout<<ans;return 0;}
		}
	cout<<ans;
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值