CF498C. Array and Operations

题意:

给定一个长为 n 的数组,以及 m 对下标 (a, b) 且满足 a + b 为奇数,每次操作可以将同一组的两个数同时除以一个公约数

问最多能进行多少次操作

\[1≤n,m ≤100,1≤ai ≤10^9
\]


根据奇偶性二分图定理此题必定考二分图

贪心,每次除一个质数

质数之间是独立的,可以分开考虑每一个质因子

建图:s -x中质因子p数量-> x -inf-> y -y中质因子p数量-> t

最大权匹配就是这个质因子能带来的最多操作数

注意质因子分解别写错了,最后判x>1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <map> #define fir first
#define sec second
using namespace std;
const int N = 105, inf = 1e9; int n, m, a[N];
pair<int, int> b[N];
map<int, int> li[N];
set<int> s; void fac(int p) {
int x = a[p], sx = sqrt(x) + 1; //printf("fac %d\n", a[p]);
for(int i=2; i<=sx; i++) if(x % i == 0) { //printf("iii %d\n", i);
int cnt = 0;
while(x%i == 0) cnt++, x/=i; //printf("x %d\n", x);
//li[p].push_back(make_pair(i, cnt));
li[p][i] = cnt;
s.insert(i);
}
if(x > 1) li[p][x] = 1, s.insert(x);
} namespace mf {
int s, t;
struct edge {int v, ne, c, f;} e[1005];
int cnt=1, h[N];
void ins(int u, int v, int c) { //printf("ins %d %d %d\n", u, v, c);
e[++cnt] = (edge) {v, h[u], c, 0}; h[u] = cnt;
e[++cnt] = (edge) {u, h[v], 0, 0}; h[v] = cnt;
}
int cur[N], vis[N], d[N], head, tail, q[N];
bool bfs() {
memset(vis, 0, sizeof(vis));
head = tail = 1;
q[tail++] = s; d[s] = 0; vis[s] = 1;
while(head != tail) {
int u = q[head++];
for(int i=h[u]; i; i=e[i].ne) {
int v = e[i].v;
if(!vis[v] && e[i].c > e[i].f) {
vis[v] = 1;
d[v] = d[u] + 1;
q[tail++] = v;
if(v == t) return true;
}
}
}
return false;
}
int dfs(int u, int a) {
if(u==t || a==0) return a;
int flow = 0, f;
for(int &i=cur[u]; i; i=e[i].ne) {
int v = e[i].v;
if(d[v] == d[u]+1 && (f = dfs(v, min(a, e[i].c-e[i].f))) > 0) {
flow += f;
e[i].f += f;
e[i^1].f -= f;
a -= f;
if(a==0) break;
}
}
if(a) d[u] = -1;
return flow;
} void build(int p) {
cnt = 1;
memset(h, 0, sizeof(h));
s=0; t=n+1;
for(int i=1; i<=m; i++) ins(b[i].fir, b[i].sec, inf);
for(int i=1; i<=n; i++) if(li[i].count(p)) {
if(i & 1) ins(s, i, li[i][p]);
else ins(i, t, li[i][p]);
}
} int solve(int p) { //printf("\nsolve %d\n", p);
build(p);
int flow = 0;
while(bfs()) {
for(int i=s; i<=t; i++) cur[i] = h[i];
flow += dfs(s, inf);
}
return flow;
} }
int main() {
//freopen("in", "r", stdin);
ios::sync_with_stdio(false); cin.tie(); cout.tie(); cin >> n >> m;
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=1; i<=m; i++) {
cin >> b[i].fir >> b[i].sec;
if(b[i].sec & 1) swap(b[i].fir, b[i].sec);
} for(int i=1; i<=n; i++) fac(i);
int ans = 0; for(set<int>::iterator it = s.begin(); it != s.end(); it++) ans += mf::solve(*it);
cout << ans;
}

CF498C. Array and Operations [二分图]的更多相关文章

  1. cf498C Array and Operations

    C. Array and Operations time limit per test 1 second memory limit per test 256 megabytes input stand ...

  2. 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 ...

  3. Codeforces Round #284 (Div. 1) C. Array and Operations 二分图匹配

    因为只有奇偶之间有操作, 可以看出是二分图, 然后拆质因子, 二分图最大匹配求答案就好啦. #include<bits/stdc++.h> #define LL long long #de ...

  4. Array and Operations

    A. Array and Operations Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d    ...

  5. 网络流(最大流):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 goo ...

  6. Codeforces 498C Array and Operations(最大流)

    题目是给一些数和<数对>的下标,然后进行操作:对某个<数对>中的两个数同时除以一个都能被它们整除且不等于1的数,要求的就是最多能进行多少次操作. 除数一定是素数,就是要决定某素 ...

  7. codeforcese 498C. Array and Operations 网络流

    题目链接 给n个数, m个数对, 每个数对是两个下标加起来为奇数的两个数.每次操作可以使一个数对中的两个数同时除某个数, 除的这个数是这两个数的任意约数, 问这种操作最多可以做几次.n<100, ...

  8. ZOJ 3427 Array Slicing (scanf使用)

    题意  Watashi发明了一种蛋疼(eggache) 语言  你要为这个语言实现一个 array slicing 函数  这个函数的功能是 有一个数组初始为空  每次给你一个区间[ l, r)  和 ...

  9. Codeforces Round #284 (Div. 1)

    A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...

随机推荐

  1. docker自定制镜像

    概述 很多情况下我们需要自定制镜像,如果自定制过程中需要下载配置很多包,而且这些包之间还有依赖关系,那么如果我们手动去操作的话就会很麻烦,正确的做法是把操作的命令封装到一个文件里,然后直接执行这个文件 ...

  2. IDEA中debug启动tomcat报错。Error running t8:Unable to open debugger port(127.0.0.1:49225):java.net.BindException"Address alread in use:JVM_Bind"

    解决办法: 1,如下图打开项目配置的tomcat的“Edit Configurations...” 2,打开“Startup/Connection”--------"Debug"- ...

  3. jmeter使用csv传参进行并发测试验证

    1.获取到注册接口,添加HTTP信息头管理器.HTTP请求,设置好入参,且检查使用csv文件传参的入参 2.创建csv文件,写入需要传的入参 3.添加CSV Data Set Config 设置配置 ...

  4. Luogu CF451E Devu and Flowers 题解报告

    题目传送门 [题目大意] 有n种颜色的花,第i种颜色的花有a[i]朵,从这些花中选m朵出来,问有多少种方案?答案对109+7取模 [思路分析] 这是一个多重集的组合数问题,答案就是:$$C_{n+m- ...

  5. day10 函数的定义及函数语法详解

    """ 今日内容: (1)函数的定义及特点 (2)函数的语法及函数的四部分 (3)函数的分类 (4)函数的调用 (5)函数的return详解 一.函数的定义 1.什么是函 ...

  6. day09 详解内存管理机制

    """ 今日内容:详解内存管理 1.引用计数 在内存中为了对变量的值进行标记从而方便管理,采用引用计数的方式对变量进行标记. (1)如果变量的值被引用一次,那么该变量的引 ...

  7. Mac本地搭建kubernetes环境

    前言:之前在windows上面的虚拟机上面手工搭建了kubernetes集群,但是环境被破坏了,最近想要继续学习k8s,手工搭建太费事,所以选择了minikube,完全能够满足个人的需求,其实在Win ...

  8. VMware虚拟机配置内网电脑能访问

    关键字:内网访问虚拟机.内网访问Linux虚拟机.虚拟机访问外部网络 1.vmware虚拟机网络设置为桥接模式 2.虚拟机配置iP地址,以linux系统为示例. 勾选自动连接,选择手动配置ip,然后配 ...

  9. sqlserver 获取汉字拼音的首字母(大写)函数

    1:创建函数: USE [test] GO /****** 对象: UserDefinedFunction [dbo].[GetFirstChar] 脚本日期: 02/22/2019 16:39:06 ...

  10. asp.net core 发布到iis session无法传递的问题

    网站是用asp.net core 的Razor Pages开发的,其中用户登录用到了session,调试运行没有问题,但是发布到iis之后出现session无法记录的问题. 我用log记录查看了一下, ...