Prime Set ZOJ - 3988

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章