This problem is actually very good thinking, because divided into positive, deputy pilots. So the pilot as a boy, the copilot as a girl. Then do hungry can.
1#include <bits/stdc++.h>2 using namespacestd;3 intg[131][131],vis[131],girl[131],n,n1;4 intHungry (intBoy )5 {6 inti;7 for(i=n1+1; i<=n;i++)8 {9 if(g[boy][i]==1&&vis[i]==0)Ten { Onevis[i]=1; A if(Hungry (girl[i]) = =1|| girl[i]==0) - { -girl[i]=Boy ; the return 1; - } - } - } + return 0; - } + intMain () A { at inta,b,sum,i; -scanf"%d%d",&n,&N1); - while(SCANF ("%d%d", &a,&b)!=eof) g[a][b]=1; -Memset (Girl,0,sizeof(Girl)); -sum=0; - for(i=1; i<=n1;i++) in { -memset (Vis,0,sizeof(Vis)); to if(Hungry (i) = =1) sum++; + } -printf"%d", sum); the return 0; *}
View Code
But this problem is "network flow 24 questions", so still want to practice a network flow. So the maximum flow can be done directly with Dinic.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <cstdlib>5#include <algorithm>6 using namespacestd;7 intn,s,t,q[131],dis[131],g[131][131];8 intMin (intAaaintBBB) {if(AAA>BBB)returnbbbElse returnAAA;}9 intBfs ()Ten { One inthead,tail,u,v; Amemset (Q,0,sizeof(q)), head=0, tail=1, q[tail]=s; -memset (dis,-1,sizeof(dis)), dis[s]=0; - while(head<=tail) the { -u=q[++Head]; - for(v=1; v<=n;v++) - { + if(g[u][v]>0&&dis[v]<0) - { +dis[v]=dis[u]+1; Aq[++tail]=v; at } - } - } - if(dis[n]<=0)return 0; - Else return 1; - } in intDfs (intUintMinflow) - { to intV,ans; + if(u==n)returnMinflow; - for(v=1; v<=n;v++) the { * if(g[u][v]>0&&dis[v]==dis[u]+1) $ {Panax Notoginsengans=Dfs (V,min (minflow,g[u][v)); - if(ans!=0) the { +g[u][v]-=ans; Ag[v][u]+=ans; the returnans; + } - } $ } $ return 0; - } - intdinic () the { - intmaxflow=0, ans;Wuyi while(BFS ()! =0) the { -Ans=dfs (s),0x7fffffff);if(ans==0) Break; maxflow+=ans; Wu } - returnMaxflow; About } $ intMain () - { - intn1,a,b,i; -scanf"%d%d",&n,&N1); A while(SCANF ("%d%d", &a,&b)!=eof) g[a][b]=1; +n+=2; s=n-1; t=N; the for(i=1; i<=n1;i++) g[s][i]=1; - for(i=n1+1; i<=n-2; i++) g[i][t]=1; $printf"%d", Dinic ()); the return 0; the}
View Code
Cogs 14. [Network flow 24 questions] paired with pilots