POJ 3177Redundant Paths (Heavy-edge labeling method with double-connected branches with heavy edges)

Source: Internet
Author: User

Approximate test instructions:

In order to protect the grazing environment and avoid excessive biting of the turf in the same place, ranchers decided to use the continuous migration of livestock to feed the way to protect the pasture. However, livestock will also gnaw on the forage on the road during the migration, so if the same road is used for each migration, the road will be damaged by biting too much.

The ranchers now have an F farm, which is known to have at least one path connected (not necessarily directly connected), but from some farms there is at least one way to go. In order to protect the pasture on the road, the farmer hopes to build several more roads, so that at least 2 migration routes will be made each time the livestock is migrated, avoiding the repeated steps to the migration path. Given the current R Road, ask the farmer to build at least a few new roads to meet the requirements.

  Wrong idea: to find out the number of bridges N, also should consider the situation of the heavy edge .  The result should be (n + 1)/2; But it's wrong to think of it as a simple question. For example, one of the simplest figures    1-> 2, 3 4   According to our above results we get 3 bridges, that is, we need to add two edges on the line, in fact, we just have to connect the 1-4 on the line. So the conclusion we have above is wrong. (Thinking about the problem to be careful to be comprehensive)   correct thinking:   to first the graph of the strong components of the contraction point, in the undirected graph we call the side of the two connected components. All sides of the two connected components to be reduced to a point, the formation of a tree, we just have to determine the number of tree leaf nodes on the line.                     Assume that the number of leaf nodes is n   Then there is (n+1)/2 edges to turn this graph into a double connected graph without a bridge                     to determine whether a point is a leaf node as long as the point is determined by the degree of the line, the degree of 1 of the point is the leaf node.    Knowledge Summary: Seek bridge: On the basis of cutting point, if a side does not have heavy edge (heavy edge 1-2, 1->2 has two times, then 1->2 is two side, then 1-&GT;2 is not a bridge). When and only if (U,V) is a parent-child edge, and satisfies dfn[u] < Low[v] We ask for a side double connected branch, on the Internet some people say after the Tarjan   after the   the value of the direct division of the edge of the two branches, in fact, this is not feasible, the following is a counter example &nbs p; Data  4.4  According to what we can see, they belong to a side of the two connected branches, but the low value is not the same.   Here we seek the edge of the two-connected branch is the same as the strong Unicom branch, but there is a little difference, in fact, we do not need to mark whether this point is in the stack. Because our side is bidirectional, one-way cannot be determined u->v   V must be able to the U?   Price heavy side: Here used a flag variable K,   if this point to Father's road there are two, then I can not go first, but the second one has to go, because it is a heavy edge, so that my son node can also update the low.  
#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<vector>#include<queue>#include<cmath>#include<stack>#include<cstring>UsingNamespace std;#defineINF 0XFFFFFFF#defineMAXN 10025#defineMin (A, b) (A&LT;B?A:B)intm, N, Time, CNT, top;intDFN[MAXN], BLOCK[MAXN], LOW[MAXN], FATHER[MAXN], Stack[maxn];vector<int>G[MAXN];voidinit () {memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (block,0,sizeof(block)); memset (Father,0,sizeof(Father)); Top= time = CNT =0;  for(intI=0; i<=n; i++) G[i].clear ();}voidTarjan (intUintFA) {Dfn[u]= Low[u] = + +Time ; Father[u]=FA; Stack[top++] =u; intLen = G[u].size (), V, k =0;  for(intI=0; i<len; i++) {v=G[u][i]; if(v = = FA &&!)k) {k++; Continue; }        if(!Low[v])            {Tarjan (V, u); Low[u]=min (Low[u], low[v]); }        ElseLow[u]=min (Low[u], dfn[v]); }    if(Dfn[u] = =Low[u]) {         Do{v= stack[--top]; BLOCK[V]=CNT; } while(U! =v); CNT++; }}voidsolve () {intI, degree[maxn] = {0}, ans =0;  for(i=1; i<=n; i++)    {        if( !Low[i])    Tarjan (i, I); }     for(i=1; i<=n; i++)    {        intv =Father[i]; if(Block[i]! =Block[v]) {Degree[block[i]]++; DEGREE[BLOCK[V]]++; }    }     for(i=0; i<cnt; i++)    {        if(Degree[i] = =1) ans++; } printf ("%d\n", (ans+1)/2 );}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF)        {init ();  while(M--)        {            intA, B; scanf ("%d%d", &a, &b);            G[a].push_back (b);        G[b].push_back (a);    } solve (); } Return0;}

POJ 3177Redundant Paths (Heavy-edge labeling method with double-connected branches with heavy edges)

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.