zoukankan      html  css  js  c++  java
  • 网络流(最大流):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

    Input
    3 2
    8 3 8
    1 2
    2 3
    Output
    0
    Input
    3 2
    8 12 8
    1 2
    2 3
    Output
    2
      将点拆成多个素数,然后套用网络流,还有一种思路是建很多次图,分开处理素数的匹配。
      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 using namespace std;
      5 const int INF=1000000000;
      6 const int N=110,M=1010,K=35010;
      7 int P[K],cntx;bool check[K];
      8 void Linear_Shaker(){
      9     for(int i=2;i<K;i++){
     10         if(!check[i])P[++cntx]=i;
     11         for(int j=1;j<=cntx;j++){
     12             if(i*P[j]>=K)break;
     13             check[i*P[j]]=true;
     14             if(i%P[j]==0)break;
     15         }
     16     }
     17 }
     18 int v[N][12],c[N][12],id[N][12],h[N],idx;
     19 int cnt=1,fir[M],to[M*20],nxt[M*20],cap[M*20];
     20 int dis[M],gap[M],path[M],fron[M],q[M],f,b;
     21 void add(int a,int b,int c){
     22     nxt[++cnt]=fir[a];
     23     to[fir[a]=cnt]=b;
     24     cap[cnt]=c;
     25 }
     26 
     27 void addedge(int a,int b,int c){
     28     //printf("%d %d
    ",a,b);
     29     add(a,b,c);add(b,a,0);
     30 }
     31 
     32 bool BFS(int S,int T){
     33     q[f=b]=T;dis[T]=1;
     34     while(f<=b){
     35         int x=q[f++];
     36         for(int i=fir[x];i;i=nxt[i])
     37             if(!dis[to[i]]){
     38                 dis[to[i]]=dis[x]+1;
     39                 q[++b]=to[i];
     40             }
     41     }
     42     return dis[S];
     43 }
     44 
     45 int Max_Flow(int S,int T){
     46     if(!BFS(S,T))return 0;
     47     for(int i=S;i<=T;i++)fron[i]=fir[i];
     48     for(int i=S;i<=T;i++)gap[dis[i]]+=1;
     49     int ret=0,f,p=S;
     50     while(dis[S]<=T+1){
     51         if(p==T){
     52             f=INF;
     53             while(p!=S){
     54                 f=min(f,cap[path[p]]);
     55                 p=to[path[p]^1];
     56             }ret+=f,p=T;
     57             while(p!=S){
     58                 cap[path[p]]-=f;
     59                 cap[path[p]^1]+=f;
     60                 p=to[path[p]^1];
     61             }
     62         }
     63         for(int &i=fron[p];i;i=nxt[i])
     64             if(cap[i]&&dis[to[i]]==dis[p]-1){
     65                 path[p=to[i]]=i;break;
     66             }
     67         if(!fron[p]){
     68             if(!--gap[dis[p]])break;int Min=T+1;
     69             for(int i=fir[p];i;i=nxt[i])
     70                 if(cap[i])Min=min(Min,dis[to[i]]);
     71             gap[dis[p]=Min+1]+=1;fron[p]=fir[p];    
     72             if(p!=S)p=to[path[p]^1];
     73         }    
     74     }
     75     return ret;
     76 }
     77 
     78 int n,m,S,T,num[N];
     79 int G[N][N];
     80 int main(){
     81     Linear_Shaker();
     82     scanf("%d%d",&n,&m);
     83     for(int i=1;i<=n;i++){
     84         scanf("%d",&num[i]);
     85         for(int j=1;j<=cntx;j++){
     86             if(P[j]*P[j]>num[i])break;
     87             if(num[i]%P[j]==0){
     88                 v[i][h[i]]=P[j];
     89                 while(num[i]%P[j]==0){
     90                     c[i][h[i]]+=1;
     91                     num[i]/=P[j];
     92                 }h[i]+=1;
     93             }
     94         }
     95         if(num[i]!=1){
     96             v[i][h[i]]=num[i];
     97             c[i][h[i]++]=1;
     98         }
     99     }
    100     /*
    101     for(int i=1;i<=n;i++){
    102         for(int j=0;j<h[i];j++)
    103             printf("<%d %d> ",v[i][j],c[i][j]);
    104         puts("");
    105     }
    106     */
    107     for(int i=1,a,b;i<=m;i++){
    108         scanf("%d%d",&a,&b);
    109         if(a%2)swap(a,b);
    110         G[a][b]=1;
    111     }
    112     for(int i=1;i<=n;i++)
    113         for(int j=0;j<h[i];j++)
    114             id[i][j]=++idx;
    115     S=0;T=idx+1;
    116     for(int i=1;i<=n;i++)
    117         for(int j=0;j<h[i];j++){
    118             if(i%2==0)addedge(S,id[i][j],c[i][j]);
    119             else addedge(id[i][j],T,c[i][j]);
    120         }
    121     for(int i=2;i<=n;i+=2)
    122         for(int j=1;j<=n;j+=2)if(G[i][j])
    123             for(int x=0;x<h[i];x++)
    124                 for(int y=0;y<h[j];y++)
    125                     if(v[i][x]==v[j][y])
    126                         addedge(id[i][x],id[j][y],INF);
    127     printf("%d
    ",Max_Flow(S,T));                    
    128     return 0;
    129 }
  • 相关阅读:
    CSS设置行高
    微信小程序-水平垂直居中
    CSS布局
    HTML学习笔记—标签
    微信小程序--通过请求网页获取信息并显示
    docker 安装完mysql 后客户端无法访问
    Docker
    docker安装成功启动失败
    嵌入式Servlet容器自动配置和启动原理
    springboot外置的Servlet容器
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5928206.html
Copyright © 2011-2022 走看看