Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接:
http://codeforces.com/problemset/problem/498/C
C. Array and Operations
time limit per test1 secondmemory limit per test256 megabytes
#### 问题描述
> 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
> 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.
#### 输入
> 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
> It is guaranteed that all the good pairs are distinct.
#### 输出
> Output the answer for the problem.
####样例输入
> 3 2
> 8 12 8
> 1 2
> 2 3
样例输出
2
题意
给你n个数a[],然后m个顶点对ik,jk,满足ik+jk==odd,每次操作你可以任选一个顶点对,和一个>1的整数,然后有a[ik]=a[ik]/v, a[jk]=a[jk]/v,问你最多能操作几次,v必须是公约数。
题解
首先,它给的所有顶点对都是连接下标为奇偶的,这是在暗示我们它是各二部图!一个贪心的想法,显然我们选的v肯定是质因子,而且不同的质因子是独立的。 我们可以枚举所有的质因子,对每个质因子都建一张图,跑最大流。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef int LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=111;
struct Edge{
int u,v,cap,flow;
Edge(int u,int v,int c,int f):u(u),v(v),cap(c),flow(f){}
};
struct Dinic{
int n,m,s,t;
vector<Edge> egs;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
egs.clear();
}
void addEdge(int u,int v,int c){
egs.pb(Edge(u,v,c,0));
egs.pb(Edge(v,u,0,0));
m=egs.sz();
G[u].pb(m-2);
G[v].pb(m-1);
}
bool BFS(){
clr(vis,0);
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()){
int x=Q.front(); Q.pop();
for(int i=0;i<G[x].sz();i++){
Edge& e=egs[G[x][i]];
if(!vis[e.v]&&e.cap>e.flow){
vis[e.v]=1;
d[e.v]=d[x]+1;
Q.push(e.v);
}
}
}
return vis[t];
}
int DFS(int x,int a){
if(x==t||a==0) return a;
int flow=0,f;
for(int &i=cur[x];i<G[x].sz();i++){
Edge& e=egs[G[x][i]];
if(d[x]+1==d[e.v]&&(f=DFS(e.v,min(a,e.cap-e.flow)))>0){
e.flow+=f;
egs[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxFlow(int s,int t){
this->s=s;
this->t=t;
int flow=0;
while(BFS()){
clr(cur,0);
flow+=DFS(s,INF);
}
return flow;
}
}dinic;
int arr[maxn];
PII nds[maxn];
int main() {
int n,m;
scf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scf("%d",&arr[i]);
VI fac;
for(int i=1;i<=n;i++){
int x=arr[i];
for(int i=2;i*i<=x;i++){
if(x%i==0){
fac.pb(i);
while(x%i==0) x/=i;
}
}
if(x>1) fac.pb(x);
}
sort(all(fac));
fac.erase(unique(all(fac)),fac.end());
rep(i,0,m){
int u,v;
scf("%d%d",&u,&v);
if(u%2) swap(u,v);
nds[i]=mkp(u,v);
}
int ans=0;
rep(i,0,fac.sz()){
dinic.init(n+2);
int k=fac[i];
rep(j,0,m){
dinic.addEdge(nds[j].X,nds[j].Y,INF);
}
for(int j=1;j<=n;j++){
int c=0,x=arr[j];
while(x%k==0) x/=k,c++;
if(j&1){
dinic.addEdge(j,n+1,c);
}else{
dinic.addEdge(0,j,c);
}
}
ans+=dinic.maxFlow(0,n+1);
}
prf("%d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配的更多相关文章
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图匹配
因为只有奇偶之间有操作, 可以看出是二分图, 然后拆质因子, 二分图最大匹配求答案就好啦. #include<bits/stdc++.h> #define LL long long #de ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #284 (Div. 1)
A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- Codeforces Round #284 (Div. 2)
题目链接:http://codeforces.com/contest/499 A. Watching a movie You have decided to watch the best moment ...
- Codeforces Round #284 (Div. 1) A. Crazy Town 计算几何
A. Crazy Town 题目连接: http://codeforces.com/contest/498/problem/A Description Crazy Town is a plane on ...
- Codeforces Round #616 (Div. 2) B. Array Sharpening
t题目链接:http://codeforces.com/contest/1291/problem/B 思路: 用极端的情况去考虑问题,会变得很简单. 无论是单调递增,单调递减,或者中间高两边低的情况都 ...
- Codeforces Round #617 (Div. 3)A. Array with Odd Sum(水题)
You are given an array aa consisting of nn integers. In one move, you can choose two indices 1≤i,j≤n ...
随机推荐
- CentOS7 安装.netcore 2 部署应用出现An assembly specified in the application dependencies manifest (xxx.deps.json)
# dotnet xxx.dll Error: An assembly specified in the application dependencies manifest (xxx.deps.jso ...
- Java基础——注释规范
一.注释格式分类: 1.单行(single-line)注释://…… 2.块(block)注释:/*……*/ 3.文档注释:/**……*/ javadoc有如下: 二.加注释的场景: 1. 基本注释( ...
- Maven Java项目添加Scala语言支持
为了在一个普通的使用Maven构建的Java项目中,增加对Scala语言的支持.使得其能够同时编译Java和Scala语言的文件.其实很简单的一件事情,只需要在pom.xml文件中的build部分中的 ...
- Noip前的大抱佛脚----动态规划
目录 动态规划 序列DP 背包问题 状态压缩以及拆分数 期望概率DP 马尔可夫过程 一类生成树计数问题 平方计数 动态规划 序列DP 有些问题: 求长度为\(l\)的上升子序列个数 形如一个值域的前缀 ...
- 03 - django简介
1.MVC与MTV模型 2.Django的下载与基本命令 pip install django==2.0.1 第三方库安装到哪里了? 创建一个django project C:\Desktop\fir ...
- 【LG3245】[HNOI2016]大数
[LG3245][HNOI2016]大数 题面 洛谷 题解 60pts 拿vector记一下对于以每个位置为右端点符合要求子串的左端点, 则每次对于一个询问,扫一遍右端点在vector里面二分即可, ...
- 3235: [Ahoi2013]好方的蛇
3235: [Ahoi2013]好方的蛇 链接 分析: 可以求出以每个点为顶点的满足条件的矩形有多少个,单调栈求.设为sum. 然后对这个数组进行二维前缀和,可以求出每个矩阵内,以右下角.左下角为端点 ...
- [HNOI2013]比赛 搜索
[HNOI2013]比赛 搜索. LG传送门 直接暴力有60,考场上写的60,结果挂成40. 考虑在暴力的同时加个记忆化,把剩下的球队数和每支球队的得分情况hash一下,每次搜到还剩\(t\)个队的时 ...
- guacamole实现RDP的下载
1. 配置说明 1.1 主要特别配置以下三项 enable-drive 默认情况下禁用文件传输,但启用文件传输后,RDP用户可以将文件传输到持久存在于Guacamole服务器上的虚拟驱动器.通过将此参 ...
- C#数组 修改
今天咱们了解下C#中的数组 后面会讲到集合.泛型集合 咱们分开来讲,免得出现混乱 讲完这三个,咱们再汇总一下,看看有什共同点和不同点 定义一个数组: ]; , , , , , , , , , }; 两 ...