ZOJ - 3988 Prome Set


传送门:ZOJ - 3988

思路分析

很显然求最大匹配,先用筛法预处理素数,然后将能够配对的进行连边
得到最大匹配数为$sum$,如果$sum>k$,那么直接输出$2×k$,否则统计剩下存在边但是没有进行匹配的个数,这些点通过与已经进行匹配的点再进行匹配,对配对数造成+1的贡献,对答案造成+1的贡献
所以答案就是$min(k-sum,cnt)$

AC代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 3e3+10;
const int M = 2e6+10;

vector<int>e[N];
int use[N],link[N],a[N];
int n,k;

bool prim[M];
void solve(){
    mem(prim,true);
    for(int i=2;i<M;i++){
        if(prim[i]){
            for(int j=2*i;j<M;j+=i) prim[j]=false;
        }
    }
}

bool dfs(int u){
    use[u]=1;
    for(auto v:e[u]){
        if(!use[v]){
            use[v]=1;
            if(link[v]==-1 || dfs(link[v])){
                link[u]=v;
                link[v]=u;
                return true;
            }
        }
    }
    return false;
}

int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    solve();
    int T;
    cin>>T;
    while(T--){
        cin>>n>>k;
        mem(link,0);
        for(int i=1;i<=n;i++){
            cin>>a[i];
            e[i].clear();
        }
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(prim[a[i]+a[j]]){
                    link[i]=link[j]=-1;
                    e[i].pb(j);
                    e[j].pb(i);
                }
            }
        }
        
        int sum=0;
        for(int i=1;i<=n;i++){
            if(link[i]==-1){
                mem(use,0);
                if(dfs(i)) sum++;
            }
        }
        int cnt=0;
        for(int i=1;i<=n;i++) if(link[i]==-1) cnt++;
        if(sum>=k) cout<<k+k<<endl;
        else cout<<sum*2+min(k-sum,cnt)<<endl;
        
    }




    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}