LG1640 「SCOI2010」连续攻击游戏 二分图最大匹配

问题描述

LG1640


题解

一开始以为是把\((a,b)\)作为左右部点,发现\(n \le 1000000\),建图是\(O(n^2)\)的,会爆掉

属性值向\(i\)建边。


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

template <typename Tp>
void read(Tp &x){
	x=0;char ch=1;int fh;
	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
	if(ch=='-'){
		fh=-1;ch=getchar();
	}
	else fh=1;
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	x*=fh;
}

const int maxn=10000000;

bool vis[maxn];
int Head[maxn],Next[maxn*2+7],to[maxn*2+7],tot=1;
int n,zj[maxn];


void add(int x,int y){
	to[++tot]=y,Next[tot]=Head[x],Head[x]=tot;
}

bool dfs(int x){
	if(vis[x]) return false;
	vis[x]=1;
	for(int i=Head[x];i;i=Next[i]){
		int y=to[i];
		if(!zj[y]||dfs(zj[y])){
			zj[y]=x;return true;
		}
	}
	return false;
}

int ans;

int main(){
	read(n);
	for(int i=1,x,y;i<=n;i++){
		read(x);read(y);
		add(x,i);add(y,i);
	}
	for(int i=1;i<=10000;i++){
		memset(vis,0,sizeof(vis));
		if(dfs(i)) ans++;
		else break;
	}
	printf("%d\n",ans);
	return 0;
}
posted @ 2019-09-16 23:56  览遍千秋  阅读(156)  评论(0编辑  收藏  举报