您的位置:首页 > 其它

2016杭州ccpc

2016-10-30 12:38 302 查看
 


Kingdom of Obsession

标签: 二分图最大匹配
2016-10-29 16:23 51人阅读 评论(2) 收藏 举报


 分类:

二分图和最大匹配(2) 


版权声明:本文为棒(xian)棒(yu)博主原创文章,转载请注明出处哦~


Kingdom of Obsession

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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

题意:我想来到这的都不用讲什么题意的吧。

思路:估计也都能想到利用二分图最大匹配来确定是不是能够全匹配,重点是n很大啊。

不过我们知道素数有个特性就是因数只有1和它本身。所以素数只能待在位置1或者标号为本身的位置上

1.如果m>n



如果n大于等于两个素数之间的距离,那么肯定是不可能匹配的。

如果小于直接二分图匹配一下。

2.如果m<=n



黄色部分长度为m如果m的长度大于等于两个素数的间隔那肯定是不符合题意。

如果小于那么就把m部分二分图匹配,匹配的话肯定符合题意,不匹配的话不符合题意,至于为什么不和重合部分匹配,看评论。

这两个一综合就会发现第二种如果mn互换和第一种是一样的。

至于二分图匹配:

就是n个点之间的匹配,如果两个点之间满足等式就连一条边,最后看看能不能全匹配上就可以了。

[cpp] view
plain copy

 print?





#include <iostream>  

#include <cstdio>  

#include <algorithm>  

#include <cmath>  

#include <queue>  

#include <vector>  

#include <cstring>  

#include <string>  

using namespace std;  

const int MAXN=300+7;  

  

int n,m;  

int vis[MAXN],match[MAXN];  

int num[MAXN];  

int head[MAXN];  

int cnt;  

struct node  

{  

    int v;  

    int next;  

} edge[MAXN*MAXN];  

  

void add(int u,int v);  

int dfs(int u);  

int build()  

{  

    cnt=0;  

    memset(match,-1,sizeof(match));  

    int i,j,v;  

    for(i=1; i<=n; ++i)head[i]=-1;  

    for(i=1; i<=n; ++i)  

    {  

        match[i]=-1;  

        for(j=1; j<=n; ++j)  

        {  

            v=j+m;  

            if(v%i==0)add(i,j);  

        }  

    }  

    int sum=0;  

    for(i=1; i<=n; ++i)  

    {  

        memset(vis,0,sizeof(vis));  

        if(dfs(i))sum++;  

    }  

    return sum;  

}  

void add(int u,int v)  

{  

    edge[cnt].v=v;  

    edge[cnt].next=head[u];  

    head[u]=cnt++;  

}  

int dfs(int u)  

{  

    int i;  

    for(i=head[u]; i!=-1; i=edge[i].next)  

    {  

        int v=edge[i].v;  

        if(!vis[v])  

        {  

            vis[v]=1;  

            if(match[v]==-1||dfs(match[v]))  

            {  

                match[v]=u;  

                return 1;  

            }  

        }  

    }  

    return 0;  

}  

int main()  

{  

    int t;  

    scanf("%d",&t);  

    for(int t1=1; t1<=t; ++t1)  

    {  

        scanf("%d%d",&n,&m);  

        printf("Case #%d: ",t1);  

        if(m<n)swap(m,n);  

        if(n>300)puts("No");  

        else  

        {  

            int ans=build();  

            if(ans==n)puts("Yes");  

            else puts("No");  

        }  

  

    }  

    return 0;  

}  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: