Poj3352Road Construction edge dual connection + pseudo contraction point, construction

Source: Internet
Author: User

Poj3352Road Construction edge dual connection + pseudo contraction point, construction

/* The method for edge dual-connected branches is simpler. After finding all the bridges, you only need to delete the edges and convert the \ source image into multiple connected blocks. Each connected block is an edge dual-connected branch. A bridge does not belong to any edge dual-connected branch. The other edges and each vertex belong to and only belongs to one edge dual-connected branch. How can a connected graph with a bridge be converted into an edge-connected graph by adding edges? 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. * // * (1) n indicates the number of vertices. The number of labels starts from 1. (2) c Indicates the adjacent table of the source image. g indicates the adjacent table of the E_BCC graph. (3) num [u] indicates that the vertex u in the source image belongs to the num [u] E_BCC (4) edge [] in the new image, storing all bridges (5) note that the pool [M] should be large enough to accommodate all the edges in the new and old charts (6). The self-ring is removed from the E_BCC graph (obviously there are no multiple edges) */# include <iostream> # include <cstdio> # include <cstring> using namespace std; const int N = 11115; const int M = 2000005; struct List {int v, id; List * next;} pool [M], * c [N], * g [N], * pp; // c is the adjacent table of the source image, g is the adjacent table of the E_BCC graph. // note that pool [M] Open enough to accommodate all the inline void add_edge (int u, int v, int id, List * c []) {pp-> v = v; pp-> id = id; pp-> next = c [u]; c [u] = pp ++;} struct Edge {int u, v ;} edge [M]; // edge [] stores all bridges. u and v are the two vertices of the bridge, int n, m, label, tot, top; int low [N], dfn [N], num [N], stack [N]; bool eflag [M]; // label timestamp, number of tot connection blocks // dfn is used to save the timestamp (Order) no, the earliest sequence number of the subtree for low to store vertex I or I // num [u] indicates that the vertex u in the source image belongs to the num [u] E_BCCvoid E_BCC_VISIT (int u) in the new graph) {low [u] = Dfn [u] = label ++; stack [++ top] = u; for (List * p = c [u]; p = p-> next) {int v = p-> v; if (eflag [p-> id]) continue; eflag [p-> id] = true; // if (dfn [v]) {low [u] <? = Dfn [v]; continue;} if (dfn [v]) {if (low [u]> dfn [v]) low [u] = dfn [v]; continue;} E_BCC_VISIT (v); // low [u] <? = Low [v]; if (low [u]> low [v]) low [u] = low [v]; if (low [v]> dfn [u]) {edge [m]. u = u; // two vertices of the m-th bridge u, v edge [m ++]. v = v; ++ tot; do {num [stack [top] = tot;} while (stack [top --]! = V) ;}} void E_BCC () {int I; tot = 0; m = 0; // for (I = 1; I <= n; ++ I) dfn [I] = 0, num [I] =-1; for (I = 0; I <m; ++ I) eflag [I] = false; for (I = 1; I <= n; ++ I) if (dfn [I] = 0) {label = 1; top =-1; E_BCC_VISIT (I ); ++ tot; while (top> = 0) {num [stack [top] = tot; -- top ;}} for (I = 1; I <= tot; ++ I) g [I] = NULL; // for (I = 1; I <= n; ++ I) {// shrink point, this question does not need // int u = num [I]; // u is a dual-Connected Component // fo R (List * p = c [I]; p = p-> next) {// int v = num [p-> v]; // v is another dual-Connected Component // if (u! = V) add_edge (u, v, 0, g); // create an edge between two components. //} int main () {int I, j, k; while (scanf ("% d", & n, & m) = 2) {for (I = 1; I <= n; ++ I) c [I] = NULL; pp = pool; for (k = 0; k <m; ++ k) {scanf ("% d", & I, & j); add_edge (I, j, k, c); add_edge (j, I, k, c) ;}e_bcc (); if (m = 0) {cout <0 <endl; continue;} int du [N] = {0}; for (int I = 0; I <m; I ++) {// The Bridge is the edge between the connected blocks. Here, the pseudo-point reduction is processed. // cout <num [edge [I]. u] <<'' <num [edge [I]. v] <endl; du [num [edge [I]. u] ++; // map the data to the connected block number using num [] to calculate the degree of the connected block. du [num [edge [I]. v] ++;} int leaf = 0; // leaf // cout <tot <endl <m <endl; for (int I = 1; I <= tot; I ++) if (du [I] = 1) leaf ++; cout <(leaf + 1)/2 <endl; // for (int I = 0; I <= m; I ++) printf ("num [% d]: % d \ n", I, num [I]); // printf ("tot: % d m: % d \ n", tot, m);} return 0 ;}




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.