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