湖南大学ACM程序设计新生杯大赛 - E Permutation (匈牙利算法)

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述 

A mod-dot product between two arrays with length n produce a new array with length n. If array A is a1,a2,...,an and array B is b1,b2,...bn, then A mod-dot B produce an array C c1,c2,...,cn such that c1 =  a1*b1%n, c2 = a2*b2%n,...,ci = ai*bi%n,..., cn = an*bn%n. 
i.e. A = [2,3,4] and B = [5,2,2] then A mod-dot B = [1,0,2].
A permutation of n is an array with length n and every number from 0 to n-1 appears in the array by exactly one time. 
i.e. A = [2,0,1] is a permutation of 3, and B = [3,4,1,2,0] is a permutation of 5, but C = [1,2,2,3] is NOT a permutation of 4. 
Now comes the problem: Are there two permutaion of n such that their mod-dot product is also a permutation of n?

输入描述:

The only line with the number n (1 <= n <= 1000)

输出描述:

If there are such two permutation of n that their mod-dot product is also a permutation of n, print "Yes" (without the quote). Otherwise print "No" (without the quote).
示例1

输入

2

输出

Yes

说明

A = [0,1] and B = [0,1]. Then A mod-dot B = [0,1]
示例2

输入

997

输出

No

备注:

1 <= n <= 1000

POINT:

类似匈牙利算法。不过是一对二。

也可以乱搞。答案很简单。测试样例是很水的。错误的代码也可以AC。


#include <set>
#include <map>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define  LL long long
const double pi = acos(-1);
const int maxn = 1000+55;
const int mod =  100000007;
int use1[maxn];
int use2[maxn];
struct node
{
	int a,b;
};
vector<node>G[maxn];
int mp[maxn][maxn];
int aim1[maxn];
int aim2[maxn];
bool dfs(int x)
{
	if(x==0) return true;
	for(int i=0;i<G[x].size();i++){
		int a=G[x][i].a;
		int b=G[x][i].b;
		if(use1[a]==0&&use2[b]==0){
			use1[a]=use2[b]=1;
			if((aim1[a]==0&&aim2[b]==0)||(dfs(aim1[a])&&dfs(aim2[b]))){
				aim1[a]=x;
				aim2[b]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int x=i*j%n;
			G[x].push_back((node){i,j});
		}
	}
	int flag=1;
	memset(aim1,0,sizeof(aim1));
	memset(aim2,0,sizeof(aim2));
	for(int i=1;i<n;i++){
		memset(use1,0,sizeof use1);
		memset(use2,0,sizeof use2);
		if(!dfs(i)){
			flag=0;
			break;
		}
	}
	if(flag){
		printf("Yes\n");
	}else{
		printf("No\n");
	}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值