HDU 5943 Kingdom of Obsession 二分图的匹配

题目链接→

s,n 1e9的范围直接暴力匹配二分图肯定会T。。。

对于区间[1,n]和[s+1,s+n],如果存在重叠部分,

比如1,2,3......,n-2,   n-1 ,  n

↓      ↓      ↓

s+1,s+2,s+3......s+n

s+1,s+2,s+2直接放在s+1,s+2,s+3的位置,只需要去匹配[1,s]和[n,s+n]

这样,我们需要去匹配的区间内就没有s+i可以放在s+i位置的了,如果区间内存在>=2个素数,这些素数只能放在第一个位置,这种情况肯定是No

对于2e9内的数据,打个表可以发现,两个素数间的最大距离为220?还是多少来着,也就是说,如果需要匹配的区间范围超过>440(这里用的450,没啥区别感觉)

的情况肯定也是No,这样我们进行二分图匹配的最大区间范围就缩小到了450,然后就直接用匈牙利算法去进行二分图匹配   get√

边的话……就暴力,可以整除就加边,嗯。

顺便补一补二分图匹配的相关知识,戳这些→二分图的最大匹配、完美匹配和匈牙利算法

好多博客写的都很棒啊,这里码几篇        二分图匹配(这里有匈牙利和km)

万能度娘                                              二分图及匹配算法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=450; //<--匈牙利-->   二分图匹配
int t,n,s,cas=1;
vector<int>e[MAX];
map<int,int>match; //存储求解结果
map<int,int>check;
bool dfs(int u)
{
    for(auto v:e[u])
    {
        if(!check[v])
        {
            check[v]=1;
            if(!match[v]||dfs(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungarian()
{
    int ans=0;
    match.clear();
    for(int u=1;u<=n;u++)
    {
        if(!match[u])
        {
            check.clear();
            if(dfs(u))
            ans++;
        }
    }
    return ans;
}
int main()
{
    cin>>t;
    while(t--)
    {
        for(int i=0;i<MAX;i++)
            e[i].clear();
        cin>>n>>s;
        if(s<n)swap(n,s);  //若s<n,[s+1,n]放在原位,二分匹配[n+1,s+n]与[1,s]
        if(n>MAX){
            cout<<"Case #"<<cas++<<": No"<<endl;
            continue;
        }

        for(int i=1;i<=n;i++)
        {
            int temp=i+s;
            for(int j=1;j<=n;j++)
            if(temp%j==0)
             {
              e[j].push_back(temp);
             }
        }
        if(hungarian()==n)
            cout<<"Case #"<<cas++<<": Yes"<<endl;
        else
            cout<<"Case #"<<cas++<<": No"<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Egoist-/p/8227706.html

时间: 2024-10-20 22:45:27

HDU 5943 Kingdom of Obsession 二分图的匹配的相关文章

hdu 5943 Kingdom of Obsession 二分图匹配+素数定理

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

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very

HDU 5943 Kingdom of Obsession

题意:n个人编号为[s+1, s+n],有n个座位编号为[1,n],编号为 i 的人只能坐到编号为它的约数的座位,问每个人是否都有位置坐. 题解:由于质数只能坐到1或者它本身的位置上,所以如果[n+1,n+s]区间内如果有多于一个质数时肯定无解, 有解时s 一定很小因为1e9以内,最远的两个素数相差282 (打表得出), 可以证明 [s+1,n]这一段数肯定坐到自己编号的位置上要更好所以剩下的用匈牙利匹配一下即可 简单证明一下" [s+1,n]这一段数肯定坐到自己编号的位置上要更好"

hdu 5943(素数间隔+二分图匹配)

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 200    Accepted Submission(s): 64 Problem Description There is a kindom of obsession, so people in this kingdom do things ver

HDU 3605 Escape【二分图多重匹配】

题意: 有n个人去m个星球  告诉你每个人想去哪些星球和每个星球最多容纳多少人,问能不能让所有人都满足 分析: 二分图多重匹配 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 100005; 8 const int maxm = 15; 9 10

hdu 3605 Escape (二分图多重匹配)

Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4298    Accepted Submission(s): 1129 Problem Description 2012 If this is the end of the world how to do? I do not know how. But now scient

HDU 5352——MZL&#39;s City——————【二分图多重匹配、拆点】

MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 710    Accepted Submission(s): 245 Problem Description MZL is an active girl who has her own country. Her big country has N cities numb

HDU 3605 Escape(二分图多重匹配问题)

Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 10382    Accepted Submission(s): 2485 Problem Description 2012 If this is the end of the world how to do? I do not know how. But now scient

HDU 1669 二分图多重匹配+二分

Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 747    Accepted Submission(s): 303 Problem Description Jamie is a very popular girl and has quite a lot of friends, so she