网络流(最大流):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为节点的数量 ...
随机推荐
- Hello World深入理解
每个编程人员都知道第一个都是Hello World, 可是只是单知道 用,不知道为何会这样,就一直学的只是皮毛. 学东西,不能知其然而不知其所以然.这样永远达不到境界. 我们用编辑器eclipse 创 ...
- PHP制作简单的日历
在这里分享一个PHP制作的日历 <?php //万年历if($_GET['year']){$year = $_GET['year'];}else{$year = date("Y&quo ...
- Js 的常用方法:页面跳转,Session,类继承
MyApp.Base = function () { } var basePrototype = MyApp.Base["prototype"]; //对象克隆方法 basePro ...
- SOCKET,TCP/UDP,HTTP,FTP
(一)TCP/UDP,SOCKET,HTTP,FTP简析 TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层: 网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议 传 ...
- 线性布局LinearLayout和相对布局RelativeLayout 之间的比较
LinearLayout和RelativeLayout之间: 共有属性:java代码中通过btn1关联次控件android:id="@+id/btn1" 控件宽度android:l ...
- MSSQL生成整个数据库的SQL脚本的工具 scptxfr.exe
scptxfr.exe的路径要正确declare @cMd varchar(1000)set @cmd = 'master.dbo.xp_cmdshell ' + '''c:\"Micros ...
- windows下安装wamp和wordpress
安装wamp WAMP是一个windows上的php开发集成环境,一键安装php,apache和mysql,非常方便. 双击wampserver2.2exxxxxxxxxx.exe文件进行安装,安装过 ...
- deep learning 学习资料
http://deeplearning.net/tutorial/lenet.html
- 171. Excel Sheet Column Number(C++)
171. Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as ...
- Linux下彻底卸载LibreOffice方法
Linux下彻底卸载LibreOffice方法 终端中输入命令: 对所有基于 Debian 的发行版(Debian.Ubuntu.Kubuntu.Xubuntu.*buntu.Sidux 等): su ...