poj_3352 Road Construction(求桥+边双连通分量)

来源:互联网 发布:男士抓绒外衣淘宝 编辑:程序博客网 时间:2024/05/16 10:10
Road Construction
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11450 Accepted: 5715

Description

It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 ton. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelledv and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 110 121 21 31 42 52 65 63 73 87 84 94 109 10Sample Input 23 31 22 31 3

Sample Output

Output for Sample Input 12Output for Sample Input 20
给出一个连通图求最少加上几条边后使其为边双连通图。
可以先求出桥后,再除去桥dfs一次,得到边双连通分量,
用桥将边双连通分量连接起来,可得到一棵树。
任意一棵树变成边双连通图至少需要的边数为:(树的叶子数+1)/ 2
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;vector<int> G[maxn];int pre[maxn], iscut[maxn], low[maxn], dfs_clock;bool cut_edge[maxn][maxn];int dfs(int u, int fa) //u在DFS树中的父节点是fa{    int lowu = pre[u] = ++dfs_clock;    for(int i = 0; i < G[u].size(); i++)    {        int v = G[u][i];        if(!pre[v]) //没有访问过v        {            int lowv = dfs(v, u);            lowu = min(lowu, lowv); //用后代的low函数更新u的low函数            if(lowv > pre[u])            {                cut_edge[u][v] = cut_edge[v][u] = 1;            }        }        else if(pre[v] < pre[u] && v != fa)        {            lowu = min(lowu, pre[v]);  //用反向边更新u的low函数        }    }    low[u] = lowu;    return lowu;}void find_cut(int n){    memset(cut_edge, 0, sizeof(cut_edge));    memset(pre, 0, sizeof(pre));    for(int i = 1; i <= n; i++)    {        if(!pre[i]) dfs(i, -1);    }}int ebcc_cnt;int vis[maxn];void dfs2(int u){    vis[u] = ebcc_cnt;    for(int i = 0; i < G[u].size(); i++)    {        int v = G[u][i];        if(!cut_edge[u][v] && !cut_edge[v][u] && !vis[v]) dfs2(v);    }}void find_ebcc(int n){    find_cut(n);    ebcc_cnt = 0;    memset(vis, 0, sizeof(vis));    for(int u = 1; u <= n; u++)    {        if(!vis[u]) ebcc_cnt++, dfs2(u);    }}int n, m;int degree[maxn];int main(){    //FOP;    while(~scanf("%d%d", &n, &m))    {        for(int i = 1; i <= n; i++) G[i].clear();        for(int i = 1; i <= m; i++)        {            int u, v;            scanf("%d%d", &u, &v);            G[u].push_back(v), G[v].push_back(u);        }        find_ebcc(n);        int ans = 0;        memset(degree, 0, sizeof(degree));        for(int u = 1; u < n; u++)        {            for(int v = u+1; v <= n; v++)            {                if(cut_edge[u][v])                {                    int a = vis[u], b = vis[v];                    degree[a]++, degree[b]++;                }            }        }        for(int i = 1; i <= ebcc_cnt; i++) if(degree[i] == 1) ans++;        printf("%d\n", ans+1>>1);    }    return 0;}


0 0
原创粉丝点击