2017CCPC秦皇岛H(二分 + 二分图的最大匹配数)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/creatorx/article/details/78414147
Prime Set

Time Limit: 2 Seconds      Memory Limit: 131072 KB

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.


Author: WENG, Caizhi

Source: The 2017 China Collegiate Programming Contest, Qinhuangdao site

解题思路:我们把这个序列先按奇偶分成两部分,因为相同奇偶性的数相加一定是偶数,一定不是质数,我们就可以把这些图做成一个二分图,然后跑一个最大匹配就行,但是1这个不好处理,因为1 + 1 = 2也是合法的,所以我们建二分图的时候先不考虑1,我们我们考虑把1一个一个的往二分图里面加,如果加一个1,会导致最大匹配数增加1,那么这样就是合法的,如果加一个1不会导致匹配数增加,就不要再加1了,但是这样的话,当1的个数比较多时,要跑很多次匹配,所以我们二分加入1的个数就行,把剩下的1两两自交就行,求出最大匹配数ans之后,如果ans >= k那么答案就是2 * k,反之,接着在二分图中找未匹配点就行,直到k用完。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000000 + 10;
int n, k;
int a[3010];
int odd[3010];
int even[3010];
int oddnum, evennum;
bool valid[maxn];
int prime[maxn];
vector<int> g[3010];
bool visit[3 * 3010];
int Match[3 * 3010];
int One;
int flag;
void initPrime()
{
    memset(valid, true, sizeof(valid));
    int tot = 0;
    for(int i = 2; i <= 2000000; i++)
    {
        if(valid[i])
        {
            prime[++tot] = i;
        }
        for(int j = 1; j <= tot && prime[j] * i <= 2000000; j++)
        {
            valid[i * prime[j]] = false;
            if(i % prime[j] == 0) break;
        }
    }
}
void init()
{
    for(int i = 1; i < 3010; i++) g[i].clear();
    memset(Match, -1, sizeof(Match));
    oddnum = 0;
    evennum = 0;
    One = 0;
}
bool dfs(int u)
{
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(visit[v]) continue;
        if(Match[v] == -1)
        {
            Match[v] = u;
            return true;
        }
        visit[v] = true;
        if(dfs(Match[v]))
        {
            Match[v] = u;
            return true;
        }
    }
    return false;
}
int getMatch(int up)
{
    int x = 0;
    memset(Match, -1, sizeof(Match));
    for(int i = 1; i <= up; i++)
    {
        memset(visit, false, sizeof(visit));
        if(dfs(i)) x++;
    }
    return x;
}
bool judge(int mid, int x)
{
    for(int i = oddnum + 1; i <= oddnum + mid; i++)
    {
        g[i].clear();
        for(int j = 1; j <= evennum; j++)
        {
            if(valid[1 + even[j]]) g[i].push_back(j);
        }
    }
    int num = getMatch(oddnum + mid);
    if(num - x >= mid && num) return true;
    else return false;
}
int solve()
{
    int ori = getMatch(oddnum);//不加1时的匹配数
    int l = 0, r = One;
    int re = 0;
    while(l <= r)
    {
        int mid = (l + r)>>1;
        if(judge(mid, ori))
        {
            re = mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    for(int i = oddnum + 1; i <= oddnum + re; i++)
    {
        g[i].clear();
        for(int j = 1; j <= evennum; j++)
        {
            if(valid[1 + even[j]]) g[i].push_back(j);
        }
    }
    int ans = getMatch(oddnum + re);
    if(ans >= k) return 2 * k;
    else
    {
        One -= re;
        flag = One;
        One /= 2;
        k -= ans;
        ans *= 2;
        if(One >= k) return ans + 2 * k;
        else
        {
            ans += 2 * One;
            k -= One;
            memset(visit, false, sizeof(visit));
            for(int i = 1; i <= evennum; i++)
            {
                if(Match[i] != -1)
                {
                    int v = Match[i];
                    visit[v] = true;
                }
            }
            for(int i = 1; i <= oddnum + re; i++)
            {
                for(int j = 0; j < g[i].size(); j++)
                {
                    if(!visit[i])
                    {
                        if(k)
                        {
                            k--;
                            ans++;
                            visit[i] = true;
                        }
                        else break;
                    }
                    int v = g[i][j];
                    if(Match[v] == -1)
                    {
                        if(k)
                        {
                            k--;
                            ans++;
                            Match[v] = i;
                        }
                        else break;
                    }
                }
                if(k == 0) break;
            }
            if(k)
            {
                if(flag == 1)
                {
                    for(int i = 1; i <= evennum; i++)
                    {
                        if(valid[1 + even[i]])
                        {
                            ans++;
                            break;
                        }
                    }
                }
                else if(flag&1) ans++;
            }
        }
        return ans;
    }
}
int main()
{
    //freopen("C:\\Users\\creator\\Desktop\\in1.txt","r",stdin) ;
    //freopen("C:\\Users\\creator\\Desktop\\out.txt","w",stdout) ;
    int T;
    scanf("%d", &T);
    initPrime();
    while(T--)
    {
        scanf("%d%d", &n, &k);
        One = 0;
        init();
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            if(a[i] == 1) One++;
            else if(a[i]&1) odd[++oddnum] = a[i];
            else even[++evennum] = a[i];
        }
        for(int i = 1; i <= oddnum; i++)
        {
            for(int j = 1; j <= evennum; j++)
            {
                if(valid[odd[i] + even[j]])
                {
                    g[i].push_back(j);
                }
            }
        }
        printf("%d\n", solve());
    }
    return 0;
}


展开阅读全文

没有更多推荐了,返回首页