HDU 5943 Kingdom of Obsession (二分图)

Kingdom of Obsession

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 448 Accepted Submission(s): 127

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

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

xmody=0

Is there any way to satisfy everyone’s requirement?

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

Every test case contains one line with two integers n, s.

Limits
1≤T≤100.
1≤n≤109.
0≤s≤109.

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 y equals ‘No’.

Sample Input
2
5 14
4 11

Sample Output
Case #1: No
Case #2: Yes

Source
2016年中国大学生程序设计竞赛(杭州)

利用素数之间距离比较密的原理,再套个kuangbin的hungry模板就过了。

#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
using namespace std;

const int MAXN=510;
int uN,vN;//u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//从左边开始找增广路径
{
    int v;
    for(v=1;v<=vN;v++)//这个顶点编号从0开始,若要从1开始需要修改
        if(g[u][v]&&!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {//找增广路,反向
                linker[v]=u;
                return true;
            }
        }
    return false;//这个不要忘了,经常忘记这句
}
int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1;u<=uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}

int main()
{
    int t;
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        int n,s;
        scanf("%d%d",&n,&s);
        if(s<n)swap(n,s);//抹掉相交区间 画个图很显然
        uN=vN=n;
        if(n>500)
        {
            printf("Case #%d: No\n",cas++);
            continue;
        }
        memset(g,0,sizeof(g));
        for(int i=s+1;i<=s+n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i%j==0)
                {
                    g[i-s][j]=1;
                }
            }
        }
        if(hungary()==n)
            printf("Case #%d: Yes\n",cas++);
        else
            printf("Case #%d: No\n",cas++);


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值