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
|
#define II int #define IL inline #define R register #define I 123546 using namespace std;
template < typename T > IL void (R T &a) { R char c=getchar (); R T w=1, p=0; while (!isdigit(c)) { if(c=='-') w=-1; c=getchar (); } while (isdigit(c)) { p=p*10+c-'0'; c=getchar (); } a=w*p; }
II tot=1,n,m,s,t,ans,Inf=1e8; II head[I], inq[I], bit[I]; queue <II> Q;
struct UUI { II to,up,flow; } aa[I]; IL void join(R II x,R II y,R II z) { aa[++tot]=(UUI) {y,head[x],z}; head[x]=tot; aa[++tot]=(UUI) {x,head[y],0}; head[y]=tot; }
IL bool bfs() { for(R II i=1;i<=n<<1;i++) inq[i]=0; Q.push(s); inq[s]=1; while (!Q.empty()) { R II x=Q.front(); Q.pop(); for(R II i=head[x],go;i;i=aa[i].up) { go=aa[i].to; if(!inq[go] && aa[i].flow) { inq[go]=inq[x]+1; Q.push(go); } } } return inq[t]; }
IL II dfs(R II x,R II a) { if(x==t || !a) return a; R II now_f=0,f; for(R II &i=bit[x],go;i;i=aa[i].up) { go=aa[i].to; if(inq[go]==inq[x]+1 && (f=dfs(go,min(aa[i].flow,a)))>0) { aa[i].flow-=f; aa[i^1].flow+=f; a-=f; now_f+=f; if(!a) return now_f; } } return now_f; }
int main() {
of(n); of(m); of(s); of(t); s+=n; for(R II i=1;i<=n;i++) join(i,i+n,1); for(R II i=1,x,y;i<=m;i++) { of(x); of(y); join(x+n,y,Inf); join(y+n,x,Inf); } while (bfs()) { for(R II i=1;i<=n<<1;i++) bit[i]=head[i]; ans+=dfs(s,Inf); } printf("%dn",ans); exit(0); }
|
近期评论