Usaco Section 5.4 telecowmunication (min cut)

Source: Internet
Author: User

Pretty bare a minimal cut. Split each computer into a 1-capacity edge and run the maximum flow. From small to large enumeration of each computer, if removed after the maximum stream = before the maximum stream +1, then this computer is one of the answer.

--------------------------------------------------------------------------------------

#include <cstdio>#include <vector>#include <cstring>#define REP (i,r) for (int i=0;i<r;i++)#define CLR (x,c) memset (x,c,sizeof (x) )#define REP (I,L,R) for (int i=l;i<r;i++)using namespace std;const int maxn=100*2+5;const int inf=1<<30;int x[maxn][2];struct Edge {int from,to,cap,flow;Edge (int u,int v,int c,int f): From (U), to (v), Cap (c), Flow (f) {}};struct ISAP {int n,m,s,t;int P[MAXN];int CUR[MAXN];int NUM[MAXN];int D[MAXN];vector<int> G[MAXN];vector<edge> edges;void init (int n) {this->n=n;Rep (I,n) g[i].clear ();edges.clear ();}int Addedge (int from,int to,int cap) {Edges.push_back (Edge) {from,to,cap,0});Edges.push_back (Edge) {to,from,0,0});m=edges.size ();G[from].push_back (m-2);G[to].push_back (m-1);return m-2;}int augment () {int x=t,a=inf;While (x!=s) {Edge &e=edges[p[x]];a=min (a,e.cap-e.flow);X=edges[p[x]].from;}x=t;While (x!=s) {Edges[p[x]].flow+=a;Edges[p[x]^1].flow-=a;X=edges[p[x]].from;}return A;}int maxflow (int s,int t) {int flow=0;this->s=s; this->t=t;CLR (d,0); CLR (num,0); CLR (cur,0); CLR (p,0) ;Rep (I,edges.size ()) edges[i].flow=0;Rep (i,n) num[d[i]]++;int x=s;While (d[s]<n) {if (x==t) {flow+=augment (); x=s;}int ok=0;Rep (I,cur[x],g[x].size ()) {Edge &e=edges[g[x][i]];if (e.cap>e.flow && d[x]==d[e.to]+1) {ok=1;P[e.to]=g[x][i];cur[x]=i;x=e.to;Break ;}}if (!ok) {int m=n-1;Rep (I,g[x].size ()) {Edge &e=edges[g[x][i]];if (e.cap>e.flow) m=min (m,d[e.to]);}if (--num[d[x]]==0) break;num[d[x]=m+1]++;cur[x]=0;if (x!=s) X=edges[p[x]].from;}}return flow;}} ISAP;int main (){freopen ("telecow.in", "R", stdin);freopen ("Telecow.out", "w", stdout);CLR (x,-1);int n,m,s,t,a,b;scanf ("%d%d%d%d", &n,&m,&s,&t);--s;--t;Isap.init (2*n);Rep (i,n) {X[i][0]=isap.addedge (i,i+n,1);X[i][1]=isap.addedge (i+n,i,1);}Rep (i,m) {scanf ("%d%d", &a,&b);--a;--b;Isap.addedge (a+n,b,m+1);Isap.addedge (b+n,a,m+1);}int Maxflow=isap.maxflow (s+n,t);vector<int> ans; ans.clear ();Rep (i,n) if (i!=s && i!=t) {isap.edges[x[i][0]].cap=0;isap.edges[x[i][1]].cap=0;if (Isap.maxflow (s+n,t) ==maxflow-1) {Ans.push_back (i+1);maxflow--;} else {isap.edges[x[i][0]].cap=1;isap.edges[x[i][1]].cap=1;}if (!maxflow) break;}printf ("%d\n", Ans.size ());printf ("%d", ans[0]);Rep (I,1,ans.size ()) printf ("%d", ans[i]);printf ("\ n");return 0;}

--------------------------------------------------------------------------------------

Telecowmunication

Farmer John ' s cows like-to-keep in touch via email so they has created a network of cowputers so that they can INTERCOWMU Nicate. These machines route email so if there exists a sequence of C Cowputers A1, A2, ..., A (c) such that A1 was connected T o A2, A2 is connected to A3, and so on then A1 and a (c) can send mail to one another.

Unfortunately, a cow would occasionally step on a cowputer or Farmer John would drive over it, and the machine would stop wor King. This means is the cowputer can no longer route email, so connections to and from that Cowputer is no longer usable.

Cows is pondering the minimum number of these accidents that can occur before they can no. longer use their both favori Te cowputers to send e-mail to each of the other. Write a program to calculate the minimal value for them, and the calculate a set of machines that corresponds to the this mini Mum.

For example the network:

               1*               /                
shows 3 cowputers connected with 2 lines. We want to send messages between 1 with 2. Direct lines connect 1-3 and 2-3. If Cowputer 3 is off, them there is no-to-get a message from 1 to 2.Program Name:telecowinput FORMAT
Line 1 Four space-separated integers:n, M, C1, and C2. N is the number of computers (1 <= N <=), which was numbered 1..N. M is the number of connections between pairs of Cowputers (1 <= M <= 600). The last of the numbers, C1 and C2, is the ID numbers of the cowputers that the questioning cows is using. Each connection are unique and bidirectional (if C1 is connected to C2, then C2 are connected to C1). There can is at the most one wire between any and given cowputers. Computer C1 and C2 won't have a direction connection.
Lines 2..m+1 The subsequent M lines contain pairs of Cowputers ID numbers that has connections between them.
SAMPLE INPUT (file telecow.in)
OUTPUT FORMAT

Generate lines of output. The first line was the minimum number of cowputers that can was down before terminals C1 & C2 are no longer connected. The second line was a minimal-length sorted list of cowputers that would cause C1 & C2 to no longer be connected. Note that neither C1 nor C2 can go down. In case of ties, the program should output the set of computers this, if interpreted as a base N number, is the smallest O Ne.

SAMPLE OUTPUT (file telecow.out)
1 3

Usaco Section 5.4 telecowmunication (min cut)

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.