"CodeForces-498C" Array and Operations (number theory + network streams)

Array and Operations "CodeForces-498C"
given n points m and a set of edges, each operation can be connected to two sides of the same time point value divided by a divisor, ask the maximum number of operations

The meaning of problems

Given an array of length $ n-$ and $ m $ of the labeled $ (a, b) $ of points, and satisfies the subscripts a + b is an odd number (i.e., the odd dot matched only with even numbers), each operation can be two numbers divided by the same group at the same time a number of conventions, the most asked how many operations can be performed.

solution

Apparently the subject is given a bipartite graph.

For each prime factor considered separately. For odd dot, a source connected to the capacity factor for the number of sides; for even point is established with a capacity factor of the number of sides of the sink; edged connected to a point of establishing a capacity of $ $ INF side.

For topic and array $ a [i] $, the obtained solution is calculated for each prime factor, by way of built FIG decomposition of the quality factor of maximum flow, is the summation.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int MAX_V=200+10;
const int INF=0x3f3f3f3f;


struct {int to,cap,rev;};

Vector <Edge> G [max_v]; // adjacency list representation of FIG
int Level [max_v]; from the source vertex to the reference point //
int ITER [max_v]; // current arc

void add(int from,int to,int cap)
{
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,0,G[from].size()-1});
}

// Calculate the distance from the source point of reference
void BFS ( int S)
{
memset(level,-1,sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty())
{
int v=que.front();que.pop();
for(int i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}

// DFS by looking augmenting path
int the DFS ( int v, int t, int f)
{
if(v==t) return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0 && level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}

// for Maximal Flow from s to t
int max_flow ( int s, int t)
{
int flow=0;
for(;;)
{
bfs(s);
if(level[t]<0) return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,INF))>0) flow+=f;
}
}

int n,m,a[105],u[105],v[105],s,t,ans=0;

void solve(int x)
{
for(int i=0;i<MAX_V;i++) G[i].clear();
for(int i=1;i<=n;i++)
{
int tot=0;
while(a[i]%x==0) a[i]/=x,tot++;
if(i%2) add(s,i,tot);
else add(i,t,tot);
}
for(int i=0;i<m;i++) add(u[i],v[i],INF);
ans+=max_flow(s,t);
}

int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
{
scanf("%d%d",&u[i],&v[i]);
if(v[i]&1) swap(u[i],v[i]);
}
s=0,t=n+1;
for(int i=1;i<=n;i++)
{
for(int j=2;j*j<=a[i];j++)
if(a[i]%j==0) solve(j);
if(a[i]>1) solve(a[i]);
}
printf("%dn",ans);
return 0;
}

Original: Big Box  "CodeForces-498C" Array and Operations (number theory + network streams)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11597073.html