每个点拆点,分别向源/汇连a[i]的边,满足条件的相互连INF的边,答案为sum-maxflow*2。

因为若有几个点不能同时被选,我们要贪心地选择其中和尽量大的部分,这可以由最小割来保证。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define INF 2147483647
#define MAXN 6005
#define MAXM 600301
int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM];
int d[MAXN],cur[MAXN],A[3005],sumv;
queue<int>q;
int n,S,T;
void Init_Dinic(){memset(first,-1,sizeof(first)); en=0; S=0; T=(n<<1|1);}
void AddEdge(const int &U,const int &V,const int &W)
{v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++;
v[en]=U; next[en]=first[V]; first[V]=en++;}
bool bfs()
{
memset(d,-1,sizeof(d)); q.push(S); d[S]=0;
while(!q.empty())
{
int U=q.front(); q.pop();
for(int i=first[U];i!=-1;i=next[i])
if(d[v[i]]==-1 && cap[i])
{
d[v[i]]=d[U]+1;
q.push(v[i]);
}
}
return d[T]!=-1;
}
int dfs(int U,int a)
{
if(U==T || !a) return a;
int Flow=0,f;
for(int &i=cur[U];i!=-1;i=next[i])
if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))
{
cap[i]-=f; cap[i^1]+=f;
Flow+=f; a-=f; if(!a) break;
}
if(!Flow) d[U]=-1;
return Flow;
}
int max_flow()
{
int Flow=0,tmp=0;
while(bfs())
{
memcpy(cur,first,((n<<1)+5)*sizeof(int));
while(tmp=dfs(S,INF)) Flow+=tmp;
}
return Flow;
}
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int sqr(const int &x){return x*x;}
bool check(const int &a,const int &b)
{
int t=a*a+b*b;
return (sqr((int)sqrt(t))==t&&(gcd(a,b)==1));
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&A[i]);
sumv+=A[i];
}
Init_Dinic();
for(int i=1;i<=n;++i)
for(int j=1;j<i;++j)
if(check(A[i],A[j]))
AddEdge(i,j+n,INF),
AddEdge(j,i+n,INF);
for(int i=1;i<=n;++i)
AddEdge(S,i,A[i]),
AddEdge(i+n,T,A[i]);
printf("%d\n",sumv-(max_flow()>>1));
return 0;
}

  

最新文章

  1. 30行代码让你理解angular依赖注入:angular 依赖注入原理
  2. linux node安装
  3. OC Runtime
  4. 随鼠标移动tab
  5. jsp页面 列表 展示 ajax异步实现
  6. 随机步法A-Z
  7. javascript第三方组件
  8. 关于python的import
  9. 字体的大小(pt)和像素(px)如何转换?
  10. React-Native post和get请求
  11. Opencv处理鼠标事件-OpenCV步步精深
  12. 第一册:lesson 119.
  13. Python正则表达式很难?一篇文章搞定他,不是我吹!
  14. (二)Git时间--版本控制工具进阶
  15. 2017-2018-1 20155202 张旭 嵌入式C语言——时钟提取时分秒
  16. 数据库同步相关的SQL语句
  17. RabbitMQ文档翻译——Work queues
  18. Hibernate初学
  19. vue-cli 安装失败Failed to download repo vuejs-templates/vuedemo: Response code 404 (Not Found)
  20. 计算机bit是什么意思

热门文章

  1. Clevo P950笔记本加装4G模块
  2. QT QLayout 清空
  3. idea xml 绿背景色 去掉拼写检查
  4. javascript简易下拉菜单效果
  5. jsonp应用
  6. Ubuntu pppoe 拨号上网
  7. java封装的使用
  8. bootstrap row 行间距
  9. Golang在视频直播平台的高性能实践(含PPT下载)
  10. POJ3180(有向图强连通分量结点数&gt;=2的个数)