POJ3352-Road construction (edge connected component)

Source: Internet
Author: User

Question Link


Question: How many edges need to be added to change an undirected graph to an edges-connected graph.

Idea: how to change a bridge connected graph by adding edges to an edge connected graph? The method is to first find all bridges and then delete these bridges. Each connected block is a double connected subgraph. Every connected subgraph is reduced to a vertex, and the bridge edge is added back. The final graph is a tree with the edge connectivity of 1.

Count the number of nodes with a moderate value of 1 in the tree, that is, the number of leaf nodes, which is recorded as leaf. Then, at least two (leaf + 1)/two sides are added to the tree to enable the tree to be connected by two sides. Therefore, the minimum number of edge added is (leaf + 1)/2. The specific method is to first connect an edge between the two most recent common ancestor's two leaf nodes, so that the two nodes can contract all the points on the path of the ancestor, because the formation of a ring must be dual-connected. Then we can find two leaf nodes with the farthest recent common ancestor. This one is exactly the same as (leaf + 1)/two times, and all the points are reduced together.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int MAXN = 5005;vector<int> g[MAXN];int pre[MAXN], low[MAXN], dg[MAXN], dfs_clock;bool vis[MAXN], map[MAXN][MAXN];int n, r;void tarjan(int u, int fa) {    pre[u] = low[u] = ++dfs_clock;    vis[u] = 1;    for (int i = 0; i < g[u].size(); i++) {        int v = g[u][i];         if (v == fa) continue;        if (!pre[v]) {            tarjan(v, u);             low[u] = min(low[u], low[v]);        }        else if (vis[v]) {            low[u] = min(low[u], pre[v]);         }     }}void find_ebc() {    memset(pre, 0, sizeof(pre));     memset(low, 0, sizeof(low));     memset(vis, 0, sizeof(vis));     dfs_clock = 0;    for (int i = 1; i <= n; i++)        if (!pre[i])            tarjan(i, -1);}int main() {    while (scanf("%d%d", &n, &r) != EOF) {        for (int i = 1; i <= n; i++)             g[i].clear();        memset(map, 0, sizeof(map));        int u, v;        for (int i = 0; i < r; i++) {            scanf("%d%d", &u, &v);             if (!map[u][v]) {                g[u].push_back(v);                g[v].push_back(u);                map[u][v] = map[v][u] = 1;            }        }        find_ebc();         memset(dg, 0, sizeof(dg));        for (int u = 1; u <= n; u++) {            for (int i = 0; i < g[u].size(); i++) {                int v = g[u][i];                 if (low[u] != low[v])                    dg[low[u]]++;              }          }        int cnt = 0;        for (int i = 1; i <= n; i++)            if (dg[i] == 1)                cnt++;        printf("%d\n", (cnt + 1) / 2);    }    return 0;}


POJ3352-Road construction (edge connected component)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.