P1345 [USACO5.4] cow's telecom telecowmunication topic description
Farmer John's cows like to keep in touch by email, so they set up a network of dairy computers to communicate with each other. These machines send e-mails as follows: If there is a sequence of C computers a1,a2,..., A (c), and A1 is connected to A2, A2 and A3, and so on, then computer A1 and A (c) can e-mail each other.
Unfortunately, sometimes cows accidentally step on the computer, the farmer John's car may also run over the computer, this bad computer will break down. This means that the computer can no longer be emailed, so the connection to this computer is not available.
Two cows think: if we can't email each other, at least how many computers do we need to break down? Please write a program for them to calculate this minimum value.
Take the following network as an example:
1*
/3-2*
This picture is 3 computers with 2 connections. We want to send information between Computers 1 and 2. Computers 1 and 3, 2 and 3 are directly connected. If the computer 3 is broken, the computer 1 and 2 will not be able to send each other information.
Input/output format
Input format:
The first line consists of four integers separated by spaces: n,m,c1,c2. n is the total number of computers (1<=N<=100) and the computer is numbered 1 to N. M is the total number of connections between computers (1<=m<=600). The last two integers C1 and C2 are the computer numbers used by both cows. The connection is not duplicated and is bidirectional (that is, if C1 is connected to C2, then C2 is connected to C1). There is at most one connection between the two computers. Computer C1 and C2 are not directly connected.
2nd to m+1 the next M-line, each row contains the number of two computers connected directly.
Output format:
An integer representing the minimum number of computers that make computers C1 and C2 Unable to communicate with each other and need to be broken down.
Input and Output sample input sample #: Copy
3 2 1 21 32 3
Output Example # #: Replication
1
The smallest cut see this problem after our first thought should be the smallest cut, but the smallest cut is the edge, the problem is required to cut points, how to do?
Come on, break it! We directly put each point into two points, between the two points with a flow of 1 of the edge, so we skillfully cut the point into the cutting edge,
/* It is not difficult to see that the problem is to find the minimum cut point set size. Obviously, for a point, it can only be deleted once. ~ ~ Nonsense ~ So, for each point I, we have to copy it (set to I+n), and from I to i+n the edge of the 1 (because it can only be deleted once). (Reverse connect 0 do not forget) add (i, i+n, 1); Add (i+n, I, 0); then how do you see the original image of the side that exists? They just have a unicom role, there is no limit to the flow, so make it clear: these side joins the network limit should be infinite. Suppose now to add a forward from X to Y from the original image (the problem is a non-edge, and then add a Y to x according to the following method) to the network, for Point y, the addition of this edge should not affect the flow through its limit (that is, the front of the 1)
Change, so the front of the Y to y+n side should be connected to the edge of the back, so the end of this side of the network to the Y, in contrast, the edge should be X (1) limit. Because, assuming X has been deleted (x to x+n full flow), then this edge
There is no change in the addition. Therefore, this edge in the network starting point should be x+n, so as to guarantee limited. */
#include <queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineN 1000100#defineINF 9999999using namespaceStd;queue<int>Q;intn,m,s,e,x,y,tot=1, ans;intTo[n],cap[n],lev[n],cnt[n],head[n],nextt[n];intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); returnx*F;}intAddintXintYintz) {Tot++;to[tot]=y;cap[tot]=z,nextt[tot]=head[x],head[x]=tot; Tot++,to[tot]=x,cap[tot]=0, nextt[tot]=head[y],head[y]=tot;}intBFs () { while(!q.empty ()) Q.pop (); for(intI=1; i<=n*2; i++) {Lev[i]=-1; Cnt[i]=Head[i]; } q.push (s), Lev[s]=0; while(!Q.empty ()) {x=Q.front (); Q.pop (); for(intI=head[x];i;i=Nextt[i]) { intt=To[i]; if(cap[i]>0&&lev[t]==-1) {Lev[t]=lev[x]+1; Q.push (t); if(t==e)return 1; } } } return 0;}intDinic (intXintflow) { if(x==e)returnflow; intdelta,rest=0; for(int&i=cnt[x];i;i=Nextt[i]) { intt=To[i]; if(cap[i]>0&&lev[t]==lev[x]+1) {Delta=dinic (T,min (cap[i],flow-rest)); if(Delta) {Cap[i]-=Delta; Cap[i^1]+=Delta; Rest+=Delta; if(Rest==flow) Break; } } } if(Rest!=flow) lev[x]=-1; returnrest;}intMain () {n=read (), M=read (), S=read (), e=read (); for(intI=1; i<=m;i++) {x=read (), y=read (); Add (x+n,y,inf), add (y+N,x,inf); } for(intI=1; i<=n;i++) Add (I,i+n,1); while(BFS ()) ans+=dinic (s+N,inf); printf ("%d", ans); return 0;}
Rokua--p1345 [USACO5.4] cow's telecom telecowmunication