POJ 1966 Cable TV Network

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   os   sp   

Cable TV NetworkTime Limit: 1000msMemory Limit: 30000KBThis 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. 

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. InputWrite 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. 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 Input
0 01 03 3 (0,1) (0,2) (1,2)2 05 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)
Sample Output
01302
HintThe 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. SourceSoutheastern Europe 2004 解題:求點連通度,涉及到最大流,獨立軌,無向圖的拆點 
  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

 

POJ 1966 Cable TV Network

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.