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

题目大意:有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示。每种装备最多只能使用一次,且只能使用其中一种属性。装备所使用的属性值必须从1开始连续。问最多能攻击多少次?

题解:每个装备从属性像编号连边,匈牙利算法,跑出匹配,若一个匹配不了就结束

卡点:

C++ Code:

#include<cstdio>
using namespace std;
int n,idx,ans;
int head[10010],cnt;
struct Edge{
	int to,nxt;
}e[1000010<<1];
int v[10010],res[1000010];
void add(int a,int b){
	e[++cnt]=(Edge){b,head[a]};head[a]=cnt;
}
bool dfs(int x){
	if (v[x]==idx)return false;
	v[x]=idx;
	for (int i=head[x];i;i=e[i].nxt){
		int to=e[i].to;
		if (!res[to]||dfs(res[to])){
			res[to]=x;return true;
		}
	}
	return false;
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,i),add(y,i);
	}
	for (int i=1;i<=10000;i++){
		idx++;
		if (dfs(i))ans++;else break;
	}
	printf("%d
",ans);
	return 0;
}

  

原文地址:https://www.cnblogs.com/Memory-of-winter/p/9256966.html