[USACO06JAN] redundant paths Redundant Paths

Synchronization: https://buringstraw.win/index.php/archives/46/

topic

To go to another from F (1≤F≤5000) a pasture in a Bessie and her companions sometimes have to pass some of their nasty horrible tree. Cows are tired of forced to go to a road, so they want to build a new road, so that each has at least two mutually separated paths are between pastures, so they have more choices.

It has at least one path between every pair of pastures. A description is given of all R (F-1≤R≤10000) bidirectional paths, each path connecting two different pasture, calculate the minimum number of new roads, the path from end to end connected by a plurality of roads. Two paths separated from each other, refers to two paths do not overlap one way. However, there may be some of the same pastures on two separate paths. For the same between pastures, you may have two different roads, you can also build a road between them, as another different path.

Input format :
Line. 1: Space-Separated Two integers: and R & lt F.

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

输出格式
Line 1: A single integer that is the number of new paths that must be built.

Sample Input Output
Input Sample # 1:
. 7. 7
1 2
2. 3
. 3. 4
2. 5
. 4. 5
. 5. 6
. 5. 7

Output Sample # 1:
2

Thinking

This is the template title on a one-pass. Seeking a connected graph edge becomes the bridge by adding a double edge connected graph, we need to add the number of sides,
first remove all the bridges, and the rest are some of the side double Unicom components. Each side to the two-component Unicom shrunk to a vertex, added back to the bridge, what is left will be a tree.
Suppose the number of leaf nodes is tree leaf, it is necessary to increase number of edges leaf == 1 ? 0 : (leaf + 1) / 2. Here except number is divisible.
This is tested! !
UTOOLS1563883675341.png

UTOOLS1563883793008.png

Code

#include <cstdio>
#include <iostream>
#include <stack>
#include <cmath>
using std::stack;
using std::min;

const int MAXN = 40000 + 5;

namespace m1 {
    struct ed {
        int to, nex, frm;
    } e[MAXN];
    int head[MAXN];
    int newp = 1;
    int low[MAXN], dfn[MAXN], out[MAXN];
    int tim;
    int dcnt;//双联通分量编号
    int dcolor[MAXN];//双联通分量染色
    bool bridge[MAXN];
    void insert (int p1, int p2);
    void tarjan (int p, int ed);
    void dfs (int p);
}

int n, m;

int main (void) {
    {
        using namespace m1;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; ++i) {
            int x, y;
            scanf("%d%d", &x, &y);
            insert(x, y);
            insert(y, x);
        }
        tarjan(1, 0);
        for (int i = 1; i <= n; ++i) {
            if (!dcolor[i]) {
                ++dcnt;
                dfs(i);
            }
        }
        for (int i = 1; i <= m; ++i) {
            if (dcolor[e[i * 2].frm] != dcolor[e[i * 2].to]) {
                ++out[dcolor[e[i * 2].frm]];
                ++out[dcolor[e[i * 2].to]];
            }
        }
        int leaf = 0;
        for (int i = 1; i <= dcnt; ++i) {
            if (out[i] == 1) {
                ++leaf;
            }
        }
        printf("%d\n", leaf ==  1 ? 0 : (leaf + 1) / 2);
    }
    return 0;
}

void m1::insert (int p1, int p2) {
    ++newp;
    e[newp].frm = p1;
    e[newp].to = p2;
    e[newp].nex = head[p1];
    head[p1] = newp;
}

void m1::tarjan (int p, int ed) {
    dfn[p] = low[p] = ++tim;
    for (int i = head[p]; i; i = e[i].nex) {
        int y = e[i].to;
        if (!dfn[y]) {
            tarjan(y, i);
            low[p] = min(low[p], low[y]);
            if (dfn[p] < low[y]) {
                bridge[i] = bridge[i ^ 1] = 1;
            }
        }
        else if (i != (ed ^ 1)) {
            low[p] = min(low[p], dfn[y]);
        }
    }
}

void m1::dfs (int p) {
    using namespace m1;
    dcolor[p] = dcnt;
    for (int i = head[p]; i; i = e[i].nex) {
        int y = e[i].to;
        if (dcolor[y] != 0 || bridge[i] == 1) {
            continue;
        }
        dfs(y);
    }
}

Guess you like

Origin www.cnblogs.com/buringstraw/p/11239206.html