Prime Set ZOJ - 3988

3 篇文章 0 订阅
2 篇文章 0 订阅

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 .

<h4< dd="">
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.

<h4< dd="">
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
<h4< dd="">
Sample Output
4
3
6
0
<h4< dd="">

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.


题意:首先给出一个n,之后一个n个元素的数列,然后规定一种集合s,s里有两个元素,分别为数列中的位置,并且这两个位置的数的和是质数,然后给出一个m,问这m个s集合合并后的最大长度是多少;

思路:因为是两个元素,直接两个两个元素位置建边,进行匹配,当然也可以奇偶匹配讨论1符不符合情况;这个题有一个坑点,边表恶心死人,不知道出题者是什么意思。。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int maxed=3000+10;
const int maxen=2000000+10;
vector<int> G[maxed];
int n,m,ans,head[maxed],p[maxed],A[maxed];
bool vis[maxed],is_prime[maxen];
int main()
{
    for(int i=2;i<maxen;i++)
        if(!is_prime[i])
            for(int j=i+i;j<maxen;j+=i)
                is_prime[j]=true;
    //freopen("shuJu.txt","w",stdout);
    void add_(int,int);
    bool slove(int);
    int N;
    scanf("%d",&N);
    while(N--){
        ans=1;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%d",&p[i]);
            G[i].clear();
        }
        memset(A,-1,sizeof(A));
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                if(!is_prime[p[i]+p[j]]){
                    G[i].push_back(j);
                    G[j].push_back(i);
                    A[i]=A[j]=0;
                }
        int sum_1=0,sum_2=0;
        for(int i=1;i<=n;i++)
            if(A[i]==0){
                memset(vis,false,sizeof(vis));
                if(slove(i))
                    sum_1++;
                //cout<<i<<" "<<A[i]<<endl;
            }
        for(int i=1;i<=n;i++)
            if(A[i]==0)
                sum_2++;
        if(sum_1>=m)
            printf("%d\n",2*m);
        else{
            int w1=m-sum_1;
            printf("%d\n",sum_1*2+min(w1,sum_2));
        }
    }
    return 0;
}
bool slove(int x)
{
    vis[x]=true;
    int len=G[x].size();
    for(int i=0;i<len;i++){
        int v=G[x][i];
        if(!vis[v]){
            vis[v]=true;
            if(A[v]==0||slove(A[v])){
                A[v]=x;
                A[x]=v;
                return true;
            }
        }
    }
    return false;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值