POJ 3177 redundant Paths (bridge, side two connected components, with heavy edges)

Source: Internet
Author: User

Test instructions: To an undirected graph, ask how many edges need to be added to make the entire graph into a "side double connected graph", that is, any two pairs of points between the total collapse of a path, these two points can still be connected through other paths.

Idea: POJ 3352 upgrade version, I heard that this map will give the heavy edge. First, look at the 3352 http://www.cnblogs.com/xcw0754/p/4619594.html.

In fact, with 3352 different is the appearance of the heavy side how to do? What if the heavy edges appear to be just the bridge?

The first thing to know,"cut point" can be two "points of two connected components" separated, because only a "point of two connected components," there is no cut point, then every two pairs of points are in a number of simple rings at the same time when a point is removed can still be interoperable.

the "bridge" can be two "side of the two connected components", because only a "side of the two connected components," there is certainly no bridge, then there must be 2 or more paths between each two points can be reached (can pass the same 1 points), when any 1 edges removed after each two point pair still can reach.

  The above two points are very important, according to the 2nd, we know that if in a "side of the two connected components" any of the two edges connected to add any edge is not affected, any two points still have 2 different "edge" path can be reached. But what if the bridge appears? Then the two points on the bridge will belong to a "side double connected component" at the same time, the two "side of the two connected components" merge into one.

How to judge whether the bridge has a heavy edge? We generally use T!=far to determine whether to encounter a side to the father, but now to change, we have to determine whether to encounter this side several times, if many times, to update the low value of this node x, should be used Low[x]=min (Low[x],dfn[far]) to update, After the update, their low value will naturally be under the same leadership, at this time, in accordance with the 3352-like and check-set method can be completely grouped into the same "side of the two connected components."

  

  

1#include <iostream>2#include <cmath>3#include <cstdio>4#include <vector>5#include <cstring>6#include <Set>7 //#include <bits/stdc++.h>8 using namespacestd;9 Const intn= the+5;Tenvector<int>Vect[n]; One intLow[n], dfn[n], pre[n], cnter; A intDu[n]; -vector<pair<int,int> >Cutt; - intFindintX//look for the low value of x the { -     if(Low[x]==dfn[x])returnLow[x]; -     returnlow[x]=find (Pre[low[x]); - } +  - voidDFS (intXintFar ) + { Alow[x]= dfn[x]= + +Cnter; atPre[cnter]=x;//the first cnter visited is x -     inttimes=0;//record whether the bridge has two, if any, to update the low value -      for(intI=0; I<vect[x].size (); i++) -     { -         intt=Vect[x][i]; -         if(!Dfn[t]) in         { - DFS (t,x); tolow[x]=min (low[x],low[t]); +             if(low[t]>dfn[x]) cutt.push_back (Make_pair (x,t)); -         } the         Else if(T!=far) low[x]=min (low[x],dfn[t]); *         Else $         {Panax Notoginseng             if(Times) Low[x]=min (Low[x],dfn[far]);//There are more than 1 sides of the far-x, and low should be updated to far.  -times++; the         } +     } A } the  +  - intCAL_BCC (intF//finding cut points and double connected components $ { $ cutt.clear (); -Memset (Du,0,sizeof(du)); -memset (Low,0,sizeof(Low)); thememset (DFN,0,sizeof(DFN)); -memset (PRE,0,sizeof(pre));WuyiCnter=0; theDFS (1,0); -  Wu      for(intI=0; I<cutt.size (); i++) -     { About         intA=Cutt[i].first; $         intb=Cutt[i].second; -Du[find (a)]++; -Du[find (b)]++; -     } A  +     intans=0; the      for(intI=1; i<=f; i++)if(du[i]==1) ans++; -  $     return((ans+1)/2);//if the number of leaves is even, can be intertwined, leaf order 1234 can be 1-3,2-4. The odd number will be deducted 1, then intertwined, plus 1.  the } the  the intMain () the { -Freopen ("Input.txt","R", stdin); in     intF, R, A, B, j=0; the     CharS[n]; the      while(cin>>f>>R) About     { the  the          for(intI=1; i<=f; i++) vect[i].clear (); the          while(r--) +         { -scanf"%d%d", &a, &b); the Vect[a].push_back (b);Bayi Vect[b].push_back (a); the         } theprintf"%d\n", CAL_BCC (f)); -     } -     return 0; the}
AC Code

POJ 3177 redundant Paths (bridge, side two connected components, 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.