网络流(最大流):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为节点的数量 ...
随机推荐
- wordpress密码生成与登录密码验证
一.研究wordpress时wordpess的密码密码生成与登录密码验证方式很重要 WordPress密码已成为整合的首要目标,如何征服整合,就得了解WordPress密码算法. WordPress系 ...
- bootstrap3.0 模态框显示的文字超出怎么办
版本:bootstrap3.3 模态框文字超出 解决方案: 在html中控制自动换行 其实只要在表格控制中添加一句<div style="word-break:break-all& ...
- dynamic 和var
dynamic,编译后被转换成带有 dynamicAttribute的object对象,可用在方法参数,返回值活或者局部变量上 执行过程: 运行时绑定首先会检查是否继承IDynamicMetaObje ...
- EF收集
http://www.cnblogs.com/end/archive/2011/08/18/2144250.html http://www.cnblogs.com/zzdfc/archive/2009 ...
- WPF学习(一)控件的公共属性
Visiblity控件是否可见:枚举类型:Visible表示可见.Collapsed不可见. IsEnabled:控件是否可用:bool类型. Background:背景色. FontSize:字体大 ...
- 转:Ext GridPanel根据条件显示复选框
Ext GridPanel实现复选框选择框: var selectModel = new Ext.grid.CheckboxSelectionModel({ singleSelect : false ...
- fedora22 无法联网的情况下rpm安装gcc5.1
前天发生件很不幸的事.我在给ubuntu14.04安装NVIDIA显卡驱动的时候,想清空下一个目录,什么目录我也忘了,当时我正好切到root身份(平常我很少切root的),命令格式如下 rm -fr ...
- paramiko学习
1. ssh简介 2. ssh私有key/共有key的区别 3. paramiko的常规使用
- 转】VB中Set的用法
Set 语句 将对象引用赋给变量或属性. 语法 Set objectvar = {[New] objectexpression | Nothing} Set 语句的语法包含下面部分: 部分 描述 ob ...
- 当xcode里点运行出现treating unicode character as whites
可能是由于粘贴网页上的代码的时候两行之间的回车引起的,两行之间重新输入回车就行......删掉重新写一遍就ok了 引入网页上的回车时 可能 网页对其格式做了处理,所以Xcode 不认识了