Codeforces 510E Fox and Dinner

Source: Internet
Author: User

Codeforces 510E

Prime number hit Table + Max stream + Max stream print path

Note ai (2≤ ai ≤104). Then all primes in the title must be an odd number and an even number. So all the tables are even rings.

Connect all the even points to the super source point, and the singularity points to the super sinks, with a capacity of 2. The edge of 1 is built between all the odd points and the odd points, and if full flow, the condition can be satisfied

Draw a picture to know that such a map is the correct →_→, how can you think of this point? Weak and not made

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue >using namespace Std;const int maxn=208;const int maxm=20008;const int inf=0x7f7f7f7f;int Sb[maxn],sx[maxn];bool pri[ Maxm];void shit () {memset (pri,false,sizeof (PRI));p ri[2]=true;int i,j,x;for (i=3;i<=20000;i++) {x=sqrt (i); for (j=2 ; j<=x;j++) if (i%j==0) break;if (j>x) pri[i]=true;}} struct Fuck{int u,v,cap,next;} Edge[maxm];int tol;int head[maxn];void init () {Tol=0;memset (head,-1,sizeof (Head));} void Addedge (int u,int v,int W) {edge[tol].u=u;edge[tol].v=v;edge[tol].next=head[u];edge[tol].cap=w;head[u]=tol++; edge[tol].v=u;edge[tol].u=v;edge[tol].cap=0;edge[tol].next=head[v];head[v]=tol++;} BOOL Vis[maxn];int dep[maxn],gap[maxn];void BFS (int sour,int sink) {Queue<int>q;memset (vis,false,sizeof (VIS)); memset (dep,-1,sizeof (DEP)); Memset (Gap,0,sizeof (GAP)); Q.push (sink);d Ep[sink]=0;gap[0]=1;vis[sink]=true;int u,v,i ; while (!q.empty ()) {U=q.front (); Q.pop (); for (I=head[u];i!=-1;i=edge[i].next{v=edge[i].v;if (!vis[v]&&edge[i].cap==0) {Vis[v]=true;dep[v]=dep[u]+1;q.push (v); gap[dep[v]]++;}}}} int cur[maxn],s[maxm];int last;int SAP (int sour,int sink) {BFS (sour,sink); int max_flow=0,i,u;for (i=0;i<=last;i++) Cur[i]=head[i];int Top=0;u=sour;while (dep[sour]<last) {if (u==sink) {int temp=inf,inser;for (i=0;i<top;i++) {if (edge[s[i]].cap<temp) {temp=edge[s[i]].cap;inser=i;}} for (i=0;i<top;i++) {edge[s[i]].cap-=temp;edge[s[i]^1].cap+=temp;} MAX_FLOW+=TEMP;TOP=INSER;U=EDGE[S[TOP]].U;} if (u!=sink&&gap[dep[u]-1]==0) break;for (I=cur[u];i!=-1;i=edge[i].next) if (edge[i].cap>0&&dep[ u]==dep[edge[i].v]+1) break;if (i!=-1) {cur[u]=i; S[TOP++]=I;U=EDGE[I].V;} Else{int mi=last;for (I=head[u];i!=-1;i=edge[i].next) {if (edge[i].cap==0) continue;if (mi>dep[edge[i].v]) {MI=DEP [Edge[i].v];cur[u]=i;}} --gap[dep[u]];d ep[u]=mi+1;++gap[dep[u]];if (u!=sour) u=edge[s[--top]].u;} return max_flow;} int zhan[maxn];int lin[maxn];int sum;int li[maxn];int dian[maxn];int w[maxn];void dfs (int u,int idx{vis[u]=true;sum++;int i;if (U&LT;=IDX) {for (I=head[u];i!=-1;i=edge[i].next) if (edge[i].cap==0&&edge[i].v !=0&AMP;&AMP;!VIS[EDGE[I].V]) {Lin[u]=edge[i].v;dfs (EDGE[I].V,IDX);}} Else{for (I=head[u];i!=-1;i=edge[i].next) if (edge[i].cap>0&&edge[i].v!=last-1&&!vis[edge[i].v ]) {Lin[u]=edge[i].v;dfs (EDGE[I].V,IDX);}}} int main () {int i,j,n,m,x;shit (); while (scanf ("%d", &n) ==1) {int idx,idb;idx=idb=0;init (); for (i=1;i<=n;i++) { scanf ("%d", &x); if (x&1) {sb[++idb]=x;dian[idb]=i;} Else{sx[++idx]=x;w[idx]=i;}} for (i=1;i<=idx;i++) for (j=1;j<=idb;j++) {if (Pri[sx[i]+sb[j]) Addedge (i,j+idx,1);} int sour,sink;sour=0;sink=n+1;for (i=1;i<=idx;i++) Addedge (sour,i,2); for (i=1;i<=idb;i++) Addedge (I+idx,sink, 2); Last=n+2;int Ans=sap (Sour,sink); if (ans!=n) printf ("impossible\n"); else{ans=0; int Id=0;memset (Vis,false,sizeof ( VIS); Memset (Lin,-1,sizeof (Lin)); for (i=1;i<=idx;i++) {if (!vis[i]) {Sum=0;dfs (I,IDX); li[id]=i;zhan[id++]=sum;}} printf ("%d\n", id); for (i=0;i<id;i++) {printf ("%d", enAn[i]), int pa=li[i];while (pa!=-1) {if (PA&LT;=IDX) printf ("%d", W[pa]), elseprintf ("%d", Dian[pa-idx]);p A=LIN[PA];} printf ("\ n");}}} return 0;}

Codeforces 510E Fox and Dinner

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.