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<bits/stdc++.h>
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;++i){
			head[i]=Next[i]=ver[i]=ans[i]=v[i]=match[i]=0;
		}
		int n;Ans=1;
		cin>>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<<Ans-1<<endl;
		for(int i=1;i<=n;++i){
			if(ans[i]){
				cout<<i;
				if(i<n)
					cout<<" ";
			}
		}
		cout<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/gipsy_danger/article/details/80272771
今日推荐