高维宇宙

 

二分图匹配代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MX=1001;
struct Edge {
    int to,next;
}edge[41*41];
bool ispr[MX*2],vis[41];
int match[41],ser[41],first[41*41],n,cnt;
void add(int from,int to)
{
    edge[++cnt].to=to;
    edge[cnt].next=first[from];
    first[from]=cnt;
}

bool hungry(int from)
{
    for(int i=first[from];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(vis[to]) continue;
        vis[to]=1;
        if(!match[to] || hungry(match[to])) {
            match[to]=from;
            return 1;
        }
    }
    return 0;
}

int main()
{
    memset(ispr,1,sizeof(ispr));
    for(int i=2;i<=2001;++i)
        if(ispr[i])
            for(int j=i;i*j<=2001;++j)
                ispr[i*j]=0;
    
    scanf("%d",&n); 
    for(int i=1;i<=n;++i) {
        scanf("%d",&ser[i]);
    }
    sort(ser+1,ser+1+n); 
    for(int i=1;i<=n;++i) { //奇数 
        if(ser[i]%2)
            for(int j=1;j<=n;++j) //偶数 
                if(!(ser[j]%2) && ispr[ser[i]+ser[j]])
                     add(i,j);    
    }
    int ans=0;
    for(int i=1;i<=n;++i)
    {
        memset(vis,0,sizeof(vis));
        if(hungry(i)) ans++; 
    }
    printf("%d",ans); 
    return 0;
}
/*
5 
2 9 11 12 37
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MX=1001;
struct Edge {
    int to,next;
}edge[41*41];
bool ispr[MX*2],vis[41];
int match[41],ser[41],first[41*41],n,cnt;
void add(int from,int to)
{
    edge[++cnt].to=to;
    edge[cnt].next=first[from];
    first[from]=cnt;
}

bool hungry(int from)
{
    for(int i=first[from];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(vis[to]) continue;
        vis[to]=1;
        if(!match[to] || hungry(match[to])) {
            match[to]=from;
            return 1;
        }
    }
    return 0;
}

int main()
{
    memset(ispr,true,sizeof(ispr));
    for(int i=2;i<=2001;++i)
        if(ispr[i])
            for(int j=i+i;j<=2001;j+=i)
                ispr[j]=false;
    
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d",&ser[i]);
    sort(ser+1,ser+1+n); 
    
    for(int i=1;i<=n;i++) //
    { 
        for(int j=1;j<=n;j++)
        { 
            if(ispr[ser[i]+ser[j]] && i!=j &&ser[i]%2==1&&ser[j]%2==0) 
            {
                add(i,j);
            }
        }
    }
    int ans=0;
    for(int i=1;i<=n;++i)
    {
        memset(vis,0,sizeof(vis));
        if(hungry(i)) ans++; 
    }
    printf("%d",ans);
    return 0;
}
/*
5 
2 9 11 12 37

10
3 11 6 8 17 12 7 4 6 9
*/
从0到1很难,但从1到100很容易
原文地址:https://www.cnblogs.com/qseer/p/9531621.html