ZOJ 3988(ccpc秦皇岛H)

Given an array of integers , we say a set is a prime set of the given array, if and is prime.

BaoBao has just found an array of integers in his pocket. He would like to select at most prime set of that array to maximize the size of the union of the selected sets. That is to say, to maximize by carefully selecting and , where and is a prime set of the given array. Please help BaoBao calculate the maximum size of the union set.

Input

There are multiple test cases. The first line of the input is an integer , indicating the number of test cases. For each test case:

The first line contains two integers and (, ), their meanings are described above.

The second line contains integers (), indicating the given array.

It's guaranteed that the sum of over all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the maximum size of the union of at most prime set of the given array.

Sample Input

4
4 2
2 3 4 5
5 3
3 4 12 3 6
6 3
1 3 6 8 1 1
1 0
1
Sample Output

4
3
6
0
Hint

For the first sample test case, there are 3 prime sets: {1, 2}, {1, 4} and {2, 3}. As , we can select {1, 4} and {2, 3} to get the largest union set {1, 2, 3, 4} with a size of 4.

For the second sample test case, there are only 2 prime sets: {1, 2} and {2, 4}. As , we can select both of them to get the largest union set {1, 2, 4} with a size of 3.

For the third sample test case, there are 7 prime sets: {1, 3}, {1, 5}, {1, 6}, {2, 4}, {3, 5}, {3, 6} and {5, 6}. As , we can select {1, 3}, {2, 4} and {5, 6} to get the largest union set {1, 2, 3, 4, 5, 6} with a size of 6.

题意很简单,

分析:首先肯定会想到要求最大匹配,但是怎么建图呢。。。

           问了司机才知道,这是一个基于奇偶的二分图,因为素数一定是奇数/除开2,ai与aj一定是一个奇数一个偶数(除开特殊1+1=2),找出 组队后 以数值奇偶分开,1特殊点 最后考虑,让最大匹配的情况下 1的匹配尽量少。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
const int maxn = 3000;
const int maxm = 3000000;
typedef long long ll;
using namespace std;
int n,m,tot,tot_v,tot_e;
int prime[maxm+5],a[maxn+5],on[maxn+5],num[maxn+5],match[maxn*2+5],used[maxn+5];
vector<int>g[maxn+5];
void Init()
{
    for(int i=2; i*i<=maxm; i++)
    {
        if(!prime[i])
        {
            for(int j=i*i; j<=maxm; j+=i) prime[j] = 1;
        }
    }
}
bool cmp(int x,int y)
{
    return a[x] > a[y];
}
bool dfs(int v)
{
    used[v] = 1;
    for(int i=0,l=g[v].size(); i<l; i++)
    {
        int u = g[v][i], w = match[u];
        if(w<0||(!used[w]&&dfs(w)))
        {
            match[u] = v;
            match[v] = u;
            return true;
        }
    }
    return false;
}
int bip()
{
    int res = 0;
    memset(match,-1,sizeof(match));
    for(int v=1; v<=tot; v++)
    {
        if(match[num[v]]<0)
        {
            memset(used,0,sizeof(used));
            if(dfs(num[v])) res++;
        }
    }
    int cnt = 0;
    for(int v=1; v<=tot; v++) if(match[num[v]]<0&&a[num[v]]==1)  cnt++;
    res+=cnt/2;
    return res;
}
int main()
{
    int T;
    Init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        for(int i=1; i<=n; i++) g[i].clear(), on[i]=0;
        tot_e = 0;
        for(int i=1; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                if(prime[a[i]+a[j]]) continue;
                tot_e++;
                on[i] = on[j] = 1;
                if(a[i]==1&&a[j]==1) continue;
                if(a[i]%2)
                {
                    g[i].push_back(j);
                }
                else
                {
                    g[j].push_back(i);
                }
            }
        }
        tot = tot_v = 0;
        for(int i=1; i<=n; i++) if(on[i]&&a[i]%2) num[++tot] = i;
        for(int i=1; i<=n; i++) if(on[i]) tot_v++;
        sort(num+1,num+tot+1,cmp);//把1放到最后,
        int cnt = bip();
        if(cnt>=m)
        {
            printf("%d\n",m*2);
        }
        else
        {
            int ans = cnt*2;
            int v = tot_v - ans;
            if(v>=m-cnt){
                printf("%d\n",ans+m-cnt);
            }else{
                printf("%d\n",ans+v);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值