Description
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.
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
Southeastern Europe 2004
你萌猜我memset超时多少次……
有向图求边连通度:就是求最小割
有向图求点连通度:化点为边,拆点求最小割。拆的点之间的边流量为1,原边流量INF。
无向图求边连通度:等同于有向图求边连通度
无向图求点连通度:等同于有向图求点连通度
还需要枚举源汇点
读入坑爹,善用scanf
2016.3.8修正:
求无向图边连通度:任取一点为源,枚举汇点。
求无向图点连通度:取度最小的点为源,枚举汇点。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 1000000010;
const int SZ = 1010;
int head[SZ],nxt[SZ],tot = 1,s,e,n,m;
struct edge{
int f,t,d;
}l[SZ];
void build(int f,int t,int d)
{
l[++ tot].t = t;
l[tot].d = d;
l[tot].f = f;
nxt[tot] = head[f];
head[f] = tot;
}
void insert(int f,int t,int d)
{
build(f,t,d); build(t,f,0);
}
int deep[SZ];
queue<int> q;
bool bfs()
{
memset(deep,0,sizeof(deep));
while(q.size()) q.pop();
deep[s] = 1;
q.push(s);
while(q.size())
{
int f = q.front(); q.pop();
for(int i = head[f];i;i = nxt[i])
{
int v = l[i].t;
if(l[i].d && !deep[v])
{
deep[v] = deep[f] + 1;
q.push(v);
if(v == e) return true;
}
}
}
return false;
}
bool vis[233][233];
int dfs(int u,int flow)
{
if(u == e || flow == 0) return flow;
int rest = flow;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i].t;
if(l[i].d && deep[v] == deep[u] + 1)
{
int f = dfs(v,min(rest,l[i].d));
if(f > 0)
{
l[i].d -= f;
l[i ^ 1].d += f;
rest -= f;
vis[u][v] = 0;
if(rest == 0) break;
}
else deep[v] = 0;
}
}
if(flow - rest == 0) deep[u] = 0;
return flow - rest;
}
int ff[SZ],tt[SZ];
void build_graph()
{
memset(head,0,sizeof(head)); tot = 1;
for(int i = 1;i <= n;i ++)
{
if(i == s || i == e) insert(i,i + n,INF),insert(i + n,i,INF);
else insert(i,i + n,1),insert(i + n,i,1);
}
for(int i = 1;i <= m;i ++)
{
insert(tt[i] + n,ff[i],INF);
insert(ff[i] + n,tt[i],INF);
}
}
int dinic()
{
int ans = 0;
while(bfs())
{
int tmp = dfs(s,INF);
if(!tmp) break;
ans += tmp;
}
return ans;
}
int main()
{
while(scanf("%d %d",&n,&m) == 2)
{
for(int i = 1;i <= m;i ++)
{
int x,y;
scanf(" (%d,%d)",&x,&y);
x ++; y ++;
ff[i] = x; tt[i] = y;
}
int ans = INF;
for(int i = 1;i <= n;i ++)
{
for(int j = i + 1;j <= n;j ++)
{
s = i,e = j;
build_graph();
int tmp = dinic();
ans = min(ans,tmp);
}
}
printf("%d\n",ans == INF || n <= 1 ? n : ans);
}
return 0;
}
/*
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)
*/