【BZOJ】【P1306】【CQOI2009】【match循环赛】【题解】【搜索+剪枝】

传送门:

http://www.lydsy.com/JudgeOnline/problem.php?id=1306

这道题狠狠得告诉了我一句:能剪就得剪!!!

剪枝都非常非常简单,就是得全加上

①n个队伍会打(n*(n-1))/2场比赛,枚举一个队伍赢就是另一个队伍输

②每个队伍枚举到最后一场比赛时能直接计算出来

③如果当前得分大于目标分数就return

④如果以后全赢也到不了目标分数就return

完了,全加上就7s过,蒟蒻难以想象rank1 15ms是怎样过的……

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
int sor[9];
int nowsor[9];
int mp[9][9];
int ans=0;
bool ok(){
	for(int i=1;i<=n;i++)
	if(sor[i]!=nowsor[i])
	return false;
	return true;
}
inline void dfs(int a,int b){	      
	if(a>=n){
		if(sor[n]==nowsor[n])
		ans++;
		return;
	}
	int i=b+1;
	if(i==n){
		int k=sor[a]-nowsor[a];
		if(k==0){
			nowsor[a]+=0;nowsor[i]+=3;
			mp[a][i]=0;mp[i][a]=3;
			if(nowsor[a]<=sor[a]&&nowsor[i]<=sor[i]
				&&nowsor[a]+3*(n-i)>=sor[a]
				&&nowsor[i]+3*(n-a)>=sor[i])
			dfs(a+1,a+1);
			nowsor[a]-=0;nowsor[i]-=3;			
		}else
		if(k==1){
			nowsor[a]+=1;nowsor[i]+=1;
			mp[a][i]=1;mp[i][a]=1;
			if(nowsor[a]<=sor[a]&&nowsor[i]<=sor[i]
				&&nowsor[a]+3*(n-i)>=sor[a]
				&&nowsor[i]+3*(n-a)>=sor[i])
			dfs(a+1,a+1);
			nowsor[a]-=1;nowsor[i]-=1;				
		}else
		if(k==3){
			nowsor[a]+=3; nowsor[i]+=0;
			mp[a][i]=3;   mp[i][a]=0;
			if(nowsor[a]<=sor[a]&&nowsor[i]<=sor[i]
				&&nowsor[a]+3*(n-i)>=sor[a]
				&&nowsor[i]+3*(n-a)>=sor[i])
			dfs(a+1,a+1);
			nowsor[a]-=3; nowsor[i]-=0;			
		}
	
	}else{
		nowsor[a]+=3; nowsor[i]+=0;
		mp[a][i]=3;   mp[i][a]=0;
		if(nowsor[a]<=sor[a]&&nowsor[i]<=sor[i]
			&&nowsor[a]+3*(n-i)>=sor[a]
			&&nowsor[i]+3*(n-a)>=sor[i])
		dfs(a,b+1);
		nowsor[a]-=3; nowsor[i]-=0;	
				
		nowsor[a]+=1;nowsor[i]+=1;
		mp[a][i]=1;mp[i][a]=1;
		if(nowsor[a]<=sor[a]&&nowsor[i]<=sor[i]
			&&nowsor[a]+3*(n-i)>=sor[a]
			&&nowsor[i]+3*(n-a)>=sor[i])
		dfs(a,b+1);
		nowsor[a]-=1;nowsor[i]-=1;	
					

		nowsor[a]+=0;nowsor[i]+=3;
		mp[a][i]=0;mp[i][a]=3;
		if(nowsor[a]<=sor[a]&&nowsor[i]<=sor[i]
			&&nowsor[a]+3*(n-i)>=sor[a]
			&&nowsor[i]+3*(n-a)>=sor[i])
		dfs(a,b+1);
		nowsor[a]-=0;nowsor[i]-=3;
	}
	
	
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&sor[i]);
	dfs(1,1);
	cout<<ans<<endl;
	return 0;
}


你可能感兴趣的:(bzoj,省选)