HDU——5943 Kingdom of Obsession(二分图匹配)

There is a kindom of obsession, so people in this kingdom do things very strictly. 

They name themselves in integer, and there are nn people with their id continuous (s+1,s+2,⋯,s+n) standing in a line in arbitrary order, be more obsessively, people with id xx wants to stand at ythyth position which satisfy 

xmody=0



Is there any way to satisfy everyone's requirement?

Input

First line contains an integer TT, which indicates the number of test cases. 

Every test case contains one line with two integers nn, ss. 

Limits 
1≤T≤100. 
1≤n≤10^9. 
0≤s≤10^9.

Output

For every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the result string. 

If there is any way to satisfy everyone's requirement, y equals 'Yes', otherwise yequals 'No'.

Sample Input

2
5 14
4 11

Sample Output

Case #1: No
Case #2: Yes

题意:给定s,n,把s+1,s+2,..s+n这n个数填到1,2,...,n里,要求X只能填到X的因子的位置。(即X%Y=0,那么X才能放在Y位置)问是否能够放满。就是s+1%(1~n)==0,中的一个数,然后全部对应起来~~

题解:二分图匹配,自己太菜,接触的少,没做出来,打表可以发现n,s互换位置结果一样,那么叫n尽量小,就保证数据量小了,而且1~n中不能有两个质数,因为质数只有1可以匹配,两个就不行了,可以发现两个质数最大距离约是不超过300,我保险取600~~即大于600就是No,这样就不超时了~

上代码:

#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 1e3+100;
struct hh{
	int v,nt;
}a[MAX*MAX];
int head[MAX],tot,match[MAX];
bool vis[MAX];
void init(){
	memset(head,-1,sizeof(head));
	tot=0;
}
void add(int u,int v){
	a[tot].v=v;
	a[tot].nt=head[u];
	head[u]=tot++;
}
bool dfs(int u){//匈牙利算法
	for (int i = head[u];~i;i=a[i].nt){
		int v=a[i].v;
		if(!vis[v]){
			vis[v]=1;
			if(match[v]==-1||dfs(match[v])){
				match[v]=u;
				return 1;
			}
		}
	}
	return 0;
}
int bm(int n){//匈牙利算法
	int res=0;
	memset(match,-1,sizeof(match));
	for (int u = 1; u <= n;u++){
		memset(vis,0,sizeof(vis));
		if(dfs(u)) res++;
	}
	return res;
}
int main(){
	int t,n,s;
	int cas=1;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&s);
		if(n>s) swap(n,s);
		if(n>600) {//大于600直接是No
			printf("Case #%d: No\n",cas++);
			continue;
		}
		init();
		for (int i = s+1; i <= s+n;i++){
			for (int j = 1; j <= n;j++){
				if(i%j==0){
					add(i-s+n,j);
					add(j,i-s+n);
				}
			}
		}
		if(bm(2*n)==2*n) printf("Case #%d: Yes\n",cas++);//看是否全部匹配
		else printf("Case #%d: No\n",cas++);
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值