首页 技术 正文
技术 2022年11月12日
0 收藏 535 点赞 3,502 浏览 2107 个字

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900