Prime Set ZOJ
来源:互联网 发布:淘宝延迟收货在哪里 编辑:程序博客网 时间:2024/04/30 06:50
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.
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="">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="">44 22 3 4 55 33 4 12 3 66 31 3 6 8 1 11 01<h4< dd="">
4360<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;}
- Prime Set ZOJ
- ZOJ 3988 Prime Set [二分图匹配]
- ZOJ 3988 Prime Set (最大匹配)
- ZOJ 3988 Prime Set(二分图)
- ZOJ 2723 Semi-Prime(set容器使用)
- zoj 3988 Prime Set 【二分图最大匹配 + 思维】
- ZOJ Problem Set - 2723 Semi-Prime(ac,但有一点想不通)
- ZOJ 3988 && 2017CCPC秦皇岛 H:Prime Set(二分匹配)
- ZOJ 3988 && 2017CCPC秦皇岛 H:Prime Set(二分匹配)
- ZOJ 3988 && 2017CCPC秦皇岛 H:Prime Set(二分匹配)
- ZOJ 3483 Gaussian Prime
- zoj - 2723 - Semi-Prime
- ZOJ 2723 Semi-Prime
- ZOJ 2723 Semi-Prime
- ZOJ 3483 Gaussian Prime
- zoj 1312 Prime Cuts
- ZOJ 3911 Prime Query
- ZOJ 3911Prime Query
- 现代软件工程_团队项目_阿尔法阶段_第二次会议记录_2017.11.13
- __attribute__((packed))详解
- 纯CSS3超酷3D旋转立方体动画特效效果演示
- 一级列表的购物车
- 数据库知识基本概念
- Prime Set ZOJ
- easyui datagrid 取消checkbox和行的关联
- “函数”过来过来,我不打你!
- Android——关键字搜索商品
- git基本命令
- 关于iframe的一些小问题(一些信息)
- 现代软件工程_团队项目_阿尔法阶段_需求分析文档_2017.11.13
- Banner
- HDU2063(记录二分图匹配的板子)