uva 10972 - RevolC FaeLoN(边双连通分量)

来源:互联网 发布:知乎大神 编辑:程序博客网 时间:2024/05/19 06:50

Problem I

RevolC FaeLoN

Hopefully, you can remember Koorosh from one of the previous problems. Fortunately, Koorosh has solved Basm’s problem without your help! Now, he is a high-ranked advisor of Basm.

As usual, Basm has conquered a new territory, called RevolC FaeLoN. In order to make the people of RevolC FaeLoN satisfied, he wants to solve one of their basic problems. The RevolC FaeLoN has a high annual rate of camel accidents and Basm wants to make the roads of this territory unidirectional in order to reduce the accident rate.

He wants to do this task in such a way that each city has at least one path to every other city. Now, Basm has asked Koorosh to find the minimum number of unidirectional roads which need to be constructed (in addition to the task of making the existing roads unidirectional) in order for every city to be reachable from every other city. Note that the original graph representing the territory is simple but during the road construction process, extra roads can be constructed between two cities that have already been connected to each other by a road.

The Input

Input consists of several test-cases. Each test case begins with two numbers andm which are the number of cities and roads of RevolC FaeLoN respectively. The nextm lines, each contain two integers , indicating that there is a road betweenu and v. The input is terminated by end of file.

The Output

For each test-case, print the minimum number of roads that should be constructed to make Basm’s decision possible.

Sample Input

3 2

1 2

2 3

10 11

1 2

2 3

3 1

3 7

4 5

5 6

6 4

7 9

6 3

9 8

7 8

Sample Output

1

2


Amirkabir University of Technology - Local Contest - Round #2

本题要用到两个定理。

一个无向图G是边双连通的,当且仅当G中存在一条回路,它至少经过每个顶点一次。

可以给一个任意的边-双连通图的边定向,使它成为一个强连通图。

有了这两个定理后,我们只需求出边双连通分量,缩点后,计算度数为1的点的个数为a,度数为0的点的个数为b,则(a+2*b)/2的上取整就是答案。因为对无向无环图来说,要形成一个存在一条回路至少经过每个点一次的图,则每个点度数至少是2,并且是一定能通过合理的添边使新图符合要求的。(这里的证明不大好解释,但可以自己找一些图验证)。然后还要注意一个特殊情况,假如原图就是边双连通的,答案应该是0.

#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<algorithm>#include <stack>using namespace std;const int maxn = 1000 + 5;int pre[maxn],dfs_clock;vector<int> G[maxn];int isbridge[maxn][maxn];int dfs1(int u,int fa){    int lowu = pre[u] = ++dfs_clock;    for(int i = 0;i < G[u].size();i++){        int v = G[u][i];        if(!pre[v]){            int lowv = dfs1(v,u);            lowu = min(lowu,lowv);            if(lowv > pre[u]){                isbridge[u][v] = 1; isbridge[v][u] = 1;            }        }        else if(pre[v] < pre[u] && v != fa){            lowu = min(lowu,pre[v]);        }    }    return lowu;}int fa[maxn],vis[maxn];int find(int x){return x == fa[x]?x:fa[x] = find(fa[x]);}void dfs2(int u){    for(int i = 0;i < G[u].size();i++){        int v = G[u][i];        if(!vis[v] && !isbridge[u][v]){            int X = find(u); int Y = find(v);            if(X != Y) fa[X] = Y;            vis[v] = 1;            dfs2(v);        }    }}void find_bcc(int n){    memset(pre,0,sizeof(pre));    memset(isbridge,0,sizeof(isbridge));    dfs_clock = 0;    for(int i = 0;i < n;i++)        if(!pre[i]) dfs1(i,-1);    memset(vis,0,sizeof(vis));    for(int i = 0;i < n;i++){        if(!vis[i]) {            vis[i] = 1;            dfs2(i);        }    }}int degree[maxn];int used[maxn];int main(){    int n,m;    while(scanf("%d%d",&n,&m) != EOF){        for(int i = 0;i < n;i++) {G[i].clear(); fa[i] = i;}        while(m--){            int u,v;            scanf("%d%d",&u,&v);u--;v--;            G[u].push_back(v); G[v].push_back(u);        }        find_bcc(n);        memset(degree,0,sizeof(degree));        for(int i = 0;i < n;i++){            for(int j = 0;j < G[i].size();j++){                int v = G[i][j];                if(find(i) != find(v))  degree[find(v)]++;            }        }        int sum = 0;        memset(used,0,sizeof(used));        for(int i = 0;i < n;i++){            int x = find(i);            if(!used[x]){                used[x] = 1;                if(degree[x] == 1) sum++;                else if(degree[x] == 0) sum += 2;            }        }        int cnt = 0;        for(int i = 0;i < n;i++) if(used[i] == 1) cnt++;        if(cnt == 1) {printf("0\n"); continue;}        else printf("%d\n",(sum+1)/2);    }    return 0;}

原创粉丝点击