POJ 1966 Cable TV Network graph connectivity, net flow minimum cut
Cable TV Network
Cable TV networktime limit:1000ms Memory limit:30000ktotal submissions:5047 accepted:2347descriptionthe Inter Connection of the relays in a cable TV network is bi-directional. The network is connected if there are at least one interconnection path between each pair of relays present in the network. Otherwise the network is disconnected. An empty network or a network with a single relay is considered connected. The safety factor F of a network with n relays is:1. N, if the net remains connected regardless the number of relays Remo Ved from the net. 2. The minimal number of relays that disconnect the network when removed. For example, consider the Nets from Figure 1, where the circles mark the relays and the solid lines correspond to Intercon Nection cables. The network (a) is connected regardless the number of relays that was removed and, according to rule (1), f=n=3. The network (b) is disconnected when 0 relays was removed, hence f=0 by rule (2). The network (c) is DisconnecTed when the relays 1 and 2 or 1 and 3 is removed. The safety factor is 2.InputWrite a program this reads several data sets from the standard input and computes the safety F Actor for the cable networks encoded by the data sets. Each data set starts with a integers:0<=n<=50,the number of relays in the net, and M, the number of cables in the Net. Follow m data pairs (u,v), u < V, where u and V is relay identifiers (integers in the range 0..n-1). The pair (U,V) designates the cable that interconnects the relays U and v. The pairs may occur on any order. Except the (u,v) pairs, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and is correct. Outputfor each data set, the program prints on the standard output, from the beginning of a line, the safety factor of the Encoded NET. Sample Input0 3 (0,1) (0,2) 2 7 (0,1) (0,2) (1,3) (1,4) (2,3) (3,4) sample output01302hintthe First dat A set encodes anEmpty network, the second data set corresponds to a network with a single relay, and the following three data sets encode The nets shown in Figure 1.
Ask to delete a few points after making the diagram is not connected, we can split into edges, for I split into I and I+n, the right is 1. The right to Yuanhui points is inf so that these edges are not added to the cut. So we enumerate the source point, the sink point, the maximum flow, i.e. the minimum cut. It is calculated that the s,t point of the enumeration is the source point, and the map of the meeting point is not connected to the point that needs to be deleted. All immediate answers with minimal enumeration conditions.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Vector> #include <queue>using namespace std;struct dinic {static const int MAXN = 500 + 7; static const int MAXM = MAXN * MAXN; static const int INF = 0X3F3F3F3F; int N, m, S, t; int FIRST[MAXN], CUR[MAXN], DIST[MAXN], sign; struct Node {int to, flow, next; } EDGE[MAXM * 4]; inline void init (int start, int vertex, int ss, int tt) {n = vertex, s = ss, t = TT; for (int i = start; I <= n; i++) {First[i] =-1; } sign = 0; } inline void Addedge (int u, int v, int flow) {edge[sign].to = V, edge[sign].flow = flow, Edge[sign].next = Fir St[u]; First[u] = sign++; } inline void Add_edge (int u, int v, int flow) {Addedge (U, V, flow); Addedge (V, u, 0); } inline int dinic () {int max_flow = 0; while (BFS (s, t)) {for (int i = 0; I <= N; i++) {Cur[i] = First[i]; } Max_flow + = DFS (s, INF); } return Max_flow; } bool BFs (int s, int t) {memset (dist,-1, sizeof (Dist)); queue<int>que; Que.push (s), dist[s] = 0; while (!que.empty ()) {Int. now = Que.front (); Que.pop (); if (now = = t) {return 1; } for (int i = first[now]; ~i; i = edge[i].next) {int to = edge[i].to, flow = Edge[i].flow; if (dist[to] = =-1 && flow > 0) {dist[to] = Dist[now] + 1; Que.push (to); }}} return 0; } int dfs (int now, int. max_flow) {if (now = = t) {return max_flow; } int ans = 0, next_flow = 0; for (int &i = Cur[now]; ~i; i = edge[i].next) {int to = edge[i].to, flow = Edge[i].flow; if (dist[to] = = Dist[now] +1 && Flow > 0) {next_flow = DFS (To, min (Max_flow-ans, flow)); Ans + = Next_flow; Edge[i].flow-= Next_flow; edge[i ^ 1].flow + = Next_flow; if (ans = = Max_flow) {return max_flow; }}} if (ans = = 0) {return dist[now] = 0; } return ans; }} cwl;int N, m;int main () {while (~SCANF ("%d%d", &n, &m)) {vector<pair<int, int> >vec; for (int i = 1; I <= m; i++) {int u, v; scanf ("(%d,%d)", &u, &v); u++, v++; Vec.push_back (Make_pair (U, v)); } int ans = CWL. INF; for (int s = 1; s <= N; s++) {for (int t = s + 1; t <= N; t++) {cwl.init (0, 2 * n + 1, s , t); for (int k = 1; k <= N; k++) {if (k! = s && k! = t) {Cwl.add_edge (k, k + N, 1); } else {Cwl.add_edge (k, k + N, CWL. INF); }} for (int k = 0; k < vec.size (); k++) {int x = vec[k].first, y = V Ec[k].second; Cwl.add_edge (x + N, y, CWL. INF); Cwl.add_edge (y + N, x, CWL. INF); } ans = min (ans, cwl.dinic ()); }} if (Ans >= cwl. INF) {ans = n;} printf ("%d\n", ans); } return 0;}
POJ 1966 Cable TV Network