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 .
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.
题目大意:
给出一堆数,问其中最多有多少数可与其他的数相加形成素数。在这其中,素数对的数量不能超过k对
思路,在可以相加得到素数的的两个数之间连上一条边并为所有可以形成素数的数打上标记。
求出最大匹配就是无重复的数的组数了,而剩余的数总能和这些已经选出来的匹配形成素数对,那么只要组数还允许就加上即可
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int size=3e3+5;
const int maxn=2e6+5;
const int MAXN=2e6+5;
int Cnt;
int n;
int oval[size];
vector<int> G[size];
int color[size];
int vis[size];
int march[size];
bool used[size];
bool check[maxn+10];
long long prime[maxn+10];
void init()
{
memset(check,false,sizeof(check));
long long tot = 0;
for(long long i = 2; i < MAXN; i++)
{
if( !check[i] ){
prime[tot++] = i;
}
for(long long j = 0; j < tot; j++)
{
if(i * prime[j] > MAXN) break;
check[i * prime[j]] = true;
if( i % prime[j] == 0)
break;
}
}
}
bool dfs(int u)
{
used[u]=true;
for(int v=0;v<G[u].size();v++)
{
if(!used[G[u][v]]){
used[G[u][v]]=true;
if(march[G[u][v]]==-1||dfs(march[G[u][v]])){
march[G[u][v]]=u;
march[u]=G[u][v];
return true;
}
}
}
return false;
}
int hungary()
{
int ans=0;
for(int u=1;u<=n;u++)
{
if(march[u]==-1) {
memset(used , 0 , sizeof(used));
if(dfs(u)) ans++;
}
}
return ans;
}
int main()
{
int t ;
init();
scanf("%d",&t);
while(t--)
{
int k;
memset(march,0,sizeof(march));
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&oval[i]);
G[i].clear();
}
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(!check[oval[i]+oval[j]])
{
G[i].push_back(j);
G[j].push_back(i);
march[i]=march[j]=-1;
}
}
}
int p=hungary();
int unp=0;
for(int i=1;i<=n;i++)
{
if(march[i]==-1) unp++;
}
int singlep=n-2*p-unp;
if(p>=k) printf("%d\n",2*k);
else{
printf("%d\n",p*2+min(k-p,unp));
}
}
}