BZOJ4205: Card Pairing

Source: Internet
Author: User

For two cards, you can match if there are two property values that are not coprime.

Consider only the prime number within 200, a total of 46, you can create a new 3*46*46 point to represent a class of attribute values that have both of these prime cards.

Then for each card, enumerate its quality factor, up to only 3, so the map to find the maximum flow.

#include <cstdio>const int n=66500,inf=~0u>>2,p=201;struct edge{int T,f;edge*nxt,*pair;} *g[n],*d[n],pool[3500000],*cur=pool;int N,m,i,j,x,y,z,s,t,h[n],gap[n],maxflow;int V[P],fac[P][4],cnt,id12[P][P], id13[p][p],id23[p][p],all;inline int min (int x,int y) {return x<y?x:y;}  inline void Add (int s,int t) {edge*p=cur++;p->t=t;p->f=1;p->nxt=g[s];g[s]=p;  p=cur++;p->t=s;p->f=0;p->nxt=g[t];g[t]=p; G[s]->pair=g[t];g[t]->pair=g[s];}  int sap (int v,int flow) {if (v==t) return flow;  int rec=0; For (Edge*p=d[v];p; p=p->nxt) if (h[v]==h[p->t]+1&&p->f) {int Ret=sap (p->t,min (flow-rec,p->f)    );    p->f-=ret;p->pair->f+=ret;d[v]=p;  if ((Rec+=ret) ==flow) return flow; } if (! (  --GAP[H[V]]) h[s]=t;  gap[++h[v]]++;d [V]=g[v]; Return rec;}  int main () {for (i=2;i<p;i++) if (!v[i]) for (cnt++,j=i;j<p;j+=i) v[j]=1,fac[j][++fac[j][0]]=cnt;  for (i=1;i<=cnt;i++) for (j=1;j<=cnt;j++) Id12[i][j]=++all,id13[i][j]=++all,id23[i][j]=++all; scanf ("%d%d", &n,&m);  s=all+n+m+1,t=s+1;    while (n--) {Add (S,++all);    scanf ("%d%d%d", &x,&y,&z);    for (i=1;i<=fac[x][0];i++) for (j=1;j<=fac[y][0];j++) Add (All,id12[fac[x][i]][fac[y][j]]);    for (i=1;i<=fac[x][0];i++) for (j=1;j<=fac[z][0];j++) Add (All,id13[fac[x][i]][fac[z][j]]);  for (i=1;i<=fac[y][0];i++) for (j=1;j<=fac[z][0];j++) Add (All,id23[fac[y][i]][fac[z][j]]);    } while (m--) {Add (++all,t);    scanf ("%d%d%d", &x,&y,&z);    for (i=1;i<=fac[x][0];i++) for (j=1;j<=fac[y][0];j++) Add (Id12[fac[x][i]][fac[y][j]],all);    for (i=1;i<=fac[x][0];i++) for (j=1;j<=fac[z][0];j++) Add (Id13[fac[x][i]][fac[z][j]],all);  for (i=1;i<=fac[y][0];i++) for (j=1;j<=fac[z][0];j++) Add (Id23[fac[y][i]][fac[z][j]],all);  } for (gap[maxflow=0]=t,i=1;i<=t;i++) d[i]=g[i];  while (h[s]<t) Maxflow+=sap (S,inf); Return printf ("%d", Maxflow), 0;}

  

BZOJ4205: Card Pairing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.