POJ 1966 Cable TV Network

Cable TV Network

Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on  PKU. Original ID: 1966
64-bit integer IO format: %lld      Java class name: Main
 
The interconnection of the relays in a cable TV network is bi-directional. The network is connected if there is 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 removed from the net. 
2. The minimal number of relays that disconnect the network when removed. 
POJ 1966 Cable TV Network

For example, consider the nets from figure 1, where the circles mark the relays and the solid lines correspond to interconnection cables. The network (a) is connected regardless the number of relays that are removed and, according to rule (1), f=n=3. The network (b) is disconnected when 0 relays are removed, hence f=0 by rule (2). The network (c) is disconnected when the relays 1 and 2 or 1 and 3 are removed. The safety factor is 2.
 

Input

Write a program that reads several data sets from the standard input and computes the safety factor for the cable networks encoded by the data sets. Each data set starts with two 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 are 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 in 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 are correct.
 

Output

For each data set, the program prints on the standard output, from the beginning of a line, the safety factor of the encoded net.
 

Sample Input

0 0

1 0

3 3 (0,1) (0,2) (1,2)

2 0

5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)

Sample Output

0

1

3

0

2

Hint

The first data set encodes an empty 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.
 

Source

 
解题:求点连通度,涉及到最大流,独立轨,无向图的拆点
 
  1 #include <iostream>

  2 #include <cstdio>

  3 #include <cstring>

  4 #include <cmath>

  5 #include <algorithm>

  6 #include <climits>

  7 #include <vector>

  8 #include <queue>

  9 #include <cstdlib>

 10 #include <string>

 11 #include <set>

 12 #include <stack>

 13 #define LL long long

 14 #define pii pair<int,int>

 15 #define INF 0x3f3f3f3f

 16 using namespace std;

 17 const int maxn = 200;

 18 struct arc{

 19     int to,flow,next;

 20     arc(int x = 0,int y = 0,int z = -1){

 21         to = x;

 22         flow = y;

 23         next = z;

 24     }

 25 };

 26 arc e[maxn*maxn];

 27 int head[maxn],d[maxn],cur[maxn];

 28 int S,T,n,m,tot,cnt;

 29 pii rec[maxn*maxn];

 30 void add(int u,int v,int flow){

 31     e[tot] = arc(v,flow,head[u]);

 32     head[u] = tot++;

 33     e[tot] = arc(u,0,head[v]);

 34     head[v] = tot++;

 35 }

 36 bool bfs(){

 37     memset(d,-1,sizeof(d));

 38     d[S] = 1;

 39     queue<int>q;

 40     q.push(S);

 41     while(!q.empty()){

 42         int u = q.front();

 43         q.pop();

 44         for(int i = head[u]; ~i; i = e[i].next){

 45             if(e[i].flow > 0 && d[e[i].to] == -1){

 46                 d[e[i].to] = d[u] + 1;

 47                 q.push(e[i].to);

 48             }

 49         }

 50     }

 51     return d[T] > -1;

 52 }

 53 int dfs(int u,int low){

 54     if(u == T) return low;

 55     int tmp = 0,a;

 56     for(int &i = cur[u]; ~i; i = e[i].next){

 57         if(e[i].flow > 0&& d[e[i].to] == d[u] + 1&&(a=dfs(e[i].to,min(e[i].flow,low)))){

 58             e[i].flow -= a;

 59             e[i^1].flow += a;

 60             rec[cnt++] = make_pair(i,a);

 61             rec[cnt++] = make_pair(i^1,-a);

 62             low -= a;

 63             tmp += a;

 64             if(!low) break;

 65         }

 66     }

 67     if(!tmp) d[u] = -1;

 68     return tmp;

 69 }

 70 int dinic(){

 71     int ans = cnt = 0;

 72     while(bfs()){

 73         memcpy(cur,head,sizeof(head));

 74         ans += dfs(S,INF);

 75     }

 76     return ans;

 77 }

 78 void release(){

 79     for(int i = 0; i < cnt; ++i)

 80         e[rec[i].first].flow += rec[i].second;

 81 }

 82 int main(){

 83     int u,v;

 84     while(~scanf("%d %d",&n,&m)){

 85         memset(head,-1,sizeof(head));

 86         if(!m){

 87             if(n == 1) puts("1");

 88             else puts("0");

 89             continue;

 90         }

 91         for(int i = tot = 0; i < m; ++i){

 92             scanf(" (%d,%d)",&u,&v);

 93             add(u+n,v,INF);

 94             add(v+n,u,INF);

 95         }

 96         for(int i = 0; i < n; ++i)

 97             add(i,i+n,1);

 98         S = n;

 99         int ans = INF;

100         for(int i = 1; i < n; ++i){

101             T = i;

102             ans = min(ans,dinic());

103             release();

104         }

105         printf("%d\n",ans == INF?n:ans);

106     }

107     return 0;

108 }
View Code

 

你可能感兴趣的:(NetWork)