HDU 3729 Hungarian algorithm

http://acm.hdu.edu.cn/showproblem.php?pid=3729

Obviously,the maxmun matching of bipartite graph

then I use the algorithm of Hungarian algorithm

if you memset before every dfs

the time will 900+ms

but if you just using the the number of Ans

as the lable of visit the time will be 100+ms with the cin and cout

AC code:

#include
using namespace std;
const int N=2e5+10;
int head[N],Next[N],ver[N],ans[N],v[N],match[N];
int Ans;
int tot=0;
void add(int x,int y){
	ver[++tot]=y;
	Next[tot]=head[x];
	head[x]=tot;
}
bool A(int x){
	for(int i=head[x];i;i=Next[i]){
		int y=ver[i];
		if(v[y]==Ans) continue;
		v[y]=Ans;
		if(match[y]==0||A(match[y])){
			match[y]=x;
			return 1;
		}
	}
	return 0;
}
int main(){
	int T;
	cin>>T;
	while(T--){
		tot=0;
		for(int i=0;i>n;
		for(int i=1;i<=n;++i){
			int a,b;
			cin>>a>>b;
			for(int j=a;j<=b;++j){
				add(i,j);
			}
		}
		for(int i=n;i>=1;--i){
			if(A(i)){
				ans[i]=1;
				++Ans;
			}
		}
		cout<

你可能感兴趣的:(图论)