2017 CCPC秦皇岛 H题 Prime set

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个数,然后让你找满足<x,y> x+y为素数这样的二元集合元素的交集,且集合的数量不超过m个;

我们可以先筛选出素数,然后暴力匹配,跑出每一个数字可以和哪些其他的数字组合成素数;

然后我们跑二分图最大匹配ans,得到的这些元素对<a,b> <c,d>中的元素各部相等,判断一下,集合的数量是否大于等于m;如果是,则输出2*m;

否则,我们统计没有匹配的数的数量ans2,然后答案等于 ans*2+min(ans2,m-ans);

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=3000+10;
 4 const int masn=2e6+10;
 5 
 6 int t,n,m,ans,head[maxn],p[maxn],temp[maxn];
 7 bool vis[maxn],prime[masn];
 8 vector<int> vec[maxn];
 9 
10 void Not_Prime()
11 {
12     for(int i=2;i<masn;i++)
13         if(!prime[i])
14           for(int j=i+i;j<masn;j+=i)
15             prime[j]=true;
16 }
17 
18 bool dfs(int x)
19 {
20     vis[x]=true;
21     int len=vec[x].size();
22     for(int i=0;i<len;i++)
23     {
24         int v=vec[x][i];
25         if(!vis[v])
26         {
27             vis[v]=true;
28             if(temp[v]==0||dfs(temp[v]))
29             {
30                 temp[v]=x; temp[x]=v;
31                 return true;
32             }
33         }
34     }
35     return false;
36 }
37 
38 int main()
39 {
40     Not_Prime();
41     cin>>t;
42     while(t--)
43     {
44         ans=1;
45         memset(head,-1,sizeof(head));
46         cin>>n>>m;
47         for(int i=1;i<=n;i++) cin>>p[i],vec[i].clear();
48         memset(temp,-1,sizeof(temp));
49         for(int i=1;i<=n;i++)
50             for(int j=i+1;j<=n;j++)
51                 if(!prime[p[i]+p[j]])
52                 {
53                     vec[i].push_back(j);
54                     vec[j].push_back(i);
55                     temp[i]=temp[j]=0;
56                 }
57         int ans1=0,ans2=0;
58         for(int i=1;i<=n;i++)
59             if(temp[i]==0)
60             {
61                 memset(vis,false,sizeof(vis));
62                 if(dfs(i)) ans1++;
63             }
64         for(int i=1;i<=n;i++) if(temp[i]==0) ans2++;
65         if(ans1>=m) cout<< 2*m <<endl;
66         else cout<< ans1*2+min(m-ans1,ans2) <<endl;
67     }
68     return 0;
69 }
70   
View Code

 

posted @ 2018-10-14 20:25  StarHai  阅读(625)  评论(0编辑  收藏  举报