Prime Set ZOJ

来源:互联网 发布:淘宝延迟收货在哪里 编辑:程序博客网 时间:2024/04/30 06:50

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
44 22 3 4 55 33 4 12 3 66 31 3 6 8 1 11 01
<h4< dd="">
Sample Output
4360
<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;}