网络流(最大流):CodeForces 499E Array and Operations
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 ≤ ik < jk ≤ n.
In one operation you can perform a sequence of actions:
- take one of the good pairs (ik, jk) and some integer v (v > 1), which divides both numbers a[ik] and a[jk];
- divide both numbers by v, i. e. perform the assignments: and .
Determine the maximum number of operations you can sequentially perform on the given array. Note that one pair may be used several times in the described operations.
Input
The first line contains two space-separated integers n, m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100).
The second line contains n space-separated integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — the description of the array.
The following m lines contain the description of good pairs. The k-th line contains two space-separated integers ik, jk (1 ≤ ik < jk ≤ n, ik + jk is an odd number).
It is guaranteed that all the good pairs are distinct.
Output
Output the answer for the problem.
Sample Input
3 2
8 3 8
1 2
2 3
0
3 2
8 12 8
1 2
2 3
2
将点拆成多个素数,然后套用网络流,还有一种思路是建很多次图,分开处理素数的匹配。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF=;
const int N=,M=,K=;
int P[K],cntx;bool check[K];
void Linear_Shaker(){
for(int i=;i<K;i++){
if(!check[i])P[++cntx]=i;
for(int j=;j<=cntx;j++){
if(i*P[j]>=K)break;
check[i*P[j]]=true;
if(i%P[j]==)break;
}
}
}
int v[N][],c[N][],id[N][],h[N],idx;
int cnt=,fir[M],to[M*],nxt[M*],cap[M*];
int dis[M],gap[M],path[M],fron[M],q[M],f,b;
void add(int a,int b,int c){
nxt[++cnt]=fir[a];
to[fir[a]=cnt]=b;
cap[cnt]=c;
} void addedge(int a,int b,int c){
//printf("%d %d\n",a,b);
add(a,b,c);add(b,a,);
} bool BFS(int S,int T){
q[f=b]=T;dis[T]=;
while(f<=b){
int x=q[f++];
for(int i=fir[x];i;i=nxt[i])
if(!dis[to[i]]){
dis[to[i]]=dis[x]+;
q[++b]=to[i];
}
}
return dis[S];
} int Max_Flow(int S,int T){
if(!BFS(S,T))return ;
for(int i=S;i<=T;i++)fron[i]=fir[i];
for(int i=S;i<=T;i++)gap[dis[i]]+=;
int ret=,f,p=S;
while(dis[S]<=T+){
if(p==T){
f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}ret+=f,p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
for(int &i=fron[p];i;i=nxt[i])
if(cap[i]&&dis[to[i]]==dis[p]-){
path[p=to[i]]=i;break;
}
if(!fron[p]){
if(!--gap[dis[p]])break;int Min=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])Min=min(Min,dis[to[i]]);
gap[dis[p]=Min+]+=;fron[p]=fir[p];
if(p!=S)p=to[path[p]^];
}
}
return ret;
} int n,m,S,T,num[N];
int G[N][N];
int main(){
Linear_Shaker();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
for(int j=;j<=cntx;j++){
if(P[j]*P[j]>num[i])break;
if(num[i]%P[j]==){
v[i][h[i]]=P[j];
while(num[i]%P[j]==){
c[i][h[i]]+=;
num[i]/=P[j];
}h[i]+=;
}
}
if(num[i]!=){
v[i][h[i]]=num[i];
c[i][h[i]++]=;
}
}
/*
for(int i=1;i<=n;i++){
for(int j=0;j<h[i];j++)
printf("<%d %d> ",v[i][j],c[i][j]);
puts("");
}
*/
for(int i=,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
if(a%)swap(a,b);
G[a][b]=;
}
for(int i=;i<=n;i++)
for(int j=;j<h[i];j++)
id[i][j]=++idx;
S=;T=idx+;
for(int i=;i<=n;i++)
for(int j=;j<h[i];j++){
if(i%==)addedge(S,id[i][j],c[i][j]);
else addedge(id[i][j],T,c[i][j]);
}
for(int i=;i<=n;i+=)
for(int j=;j<=n;j+=)if(G[i][j])
for(int x=;x<h[i];x++)
for(int y=;y<h[j];y++)
if(v[i][x]==v[j][y])
addedge(id[i][x],id[j][y],INF);
printf("%d\n",Max_Flow(S,T));
return ;
}
网络流(最大流):CodeForces 499E Array and Operations的更多相关文章
- Codeforces 498C Array and Operations(最大流)
题目是给一些数和<数对>的下标,然后进行操作:对某个<数对>中的两个数同时除以一个都能被它们整除且不等于1的数,要求的就是最多能进行多少次操作. 除数一定是素数,就是要决定某素 ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
- cf498C Array and Operations
C. Array and Operations time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Array and Operations
A. Array and Operations Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- POJ 1459-Power Network(网络流-最大流-ISAP)C++
Power Network 时间限制: 1 Sec 内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...
- CF498C. Array and Operations [二分图]
CF498C. Array and Operations 题意: 给定一个长为 n 的数组,以及 m 对下标 (a, b) 且满足 a + b 为奇数,每次操作可以将同一组的两个数同时除以一个公约数 ...
- [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)
题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU1532 网络流最大流【EK算法】(模板题)
<题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...
随机推荐
- PS之火焰铁锈字
效果图 素材一:将下图在PS中打开,选择菜单:编辑>定义图案,命名后关闭图案 素材二 1.新建如下画布 2.将素材二拖入新建好的画布(使用移动工具) 3.先将文字图层复制4次(ctrl+J)并且 ...
- Android 设计随便说说之简单实践(合理组合)
上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...
- 对于百川SDK签名验证的问题
SDK是要在wantu.taobao.com生成的.而生成这个SDK其实是要上传一个apk,而这个上传其实就是取他的签名而已.验证就是那张yw222那张图片.重点是你上传的apk的签名是不是跟你的生成 ...
- 一款jquery小插件:实现轻松获取和绑定编辑表单的值(带源码)
实现目的:通常在项目中,编辑页面在前后台需要一个一个框赋值,取值操作,小伙伴们普遍都会感觉繁琐,麻烦.: 实现思路:利用json对象化键值的思想: 好处:方便快速开发,提高开发效率,减少重复性代码: ...
- Linux同步时间命令ntpdate
转自:http://orgcent.com/linux-ntpdate/ 由于要同步Linux服务器的时间,为了保证时间的高度准确性,放弃date命令而转向ntpdate(同步时间命令). 方案如下: ...
- 深入了解shell
接触linux很久了,但一直没有总线,老是尝鲜,什么都想学,但好多没多没有记住,特的总结了一些基本的东西,查了很多资料,不完善的方面我会慢慢的更新…… 操作系统与外部最主要的接口就叫做shell. ...
- mahout分类
分类看起来比聚类和推荐麻烦多了 分类算法与聚类和推荐算法的不同:必须是有明确结果的,必须是有监督的,主要用于预测和检测 Mahout的优势 mahout的分类算法对资源的要求不会快于训练数据和测试数据 ...
- Chrome退出全屏问题
最近做了一个号称很炫的B/S展示软件,展示所用浏览器为Google Chrome. 要求展示时全屏,但是页面要有退出全屏按钮(液晶屏没有键盘). 搜索实现方式几乎前篇一律,即两个JS函数一个实现全屏一 ...
- Windowsphone 之xml序列化和反序列化的应用(WebService解析返回的数据DataSet )
关于Xml的序列化和反序列化: 可以看这篇文章,http://www.cnblogs.com/Windows-phone/p/3243575.html WebService解析返回的数据DataSet ...
- jQuery插件综合应用(三)发布文章页面
一.使用的插件 一个折叠的功能导航,由Akordeon插件实现.Nanoscroller插件与Tagit插件主要用于美化页面.这里只是测试,其实还可以综合使用其它的插件,例如将Akordeon插件换成 ...