Topic Links:
http://poj.org/problem?id=3352
Main topic:
There are n tourist attractions on a tropical paradise island, and there are paths between any of the 2 tourist attractions (not necessarily directly connected to each other). In order to make tourists
Travel is more convenient, and the tour company is asking for additional roads. At the time of construction, only one road construction can be selected at a time, after construction
In addition to the road accident, other roads are still accessible. Because the construction road is forbidden, this leads to the construction period
Visitors may not be able to reach some of the classics.
The company has decided to build temporary bridges to ensure that all tourist attractions are open to visitors during the construction period, making
No matter where the road works, visitors can reach all the tourist attractions. So here's the question: give you n attractions and M-Two sides,
Q: At least a few temporary bridges will make it possible for any road to be constructed without affecting visitors to all tourist attractions.
Ideas:
The topic can be changed to: give you an undirected connected graph, ask at least a few sides to add to become a double connected graph. Let's consider this.
Problem. When there is a cut edge (bridge) in the diagram, delete it, it is obvious that the structure is not a double connected graph. Both ends of the cutting edge belong to two edge connected points
Amount Deleting the original is divided into two side-connected components. It is necessary to add an additional edge between the two side-connected components (that is, the topic
said in the pro when the bridge, but not just the cutting edge), the two sides of the connecting component is also a double-connected. So, if the original has more than one
Edge connected components, how to add temporary edge, in order to make any of the two edge connectivity between the components become a double connectivity.
(1) Find out all the side-connected components of the original image.
(2) When the original image is shrunk, the point of the same side connected component is regarded as a point.
(3) After the contraction point, add a few bidirectional edges at least, so that all points can be directly reached.
For the third step, refer to the workaround:
The original G is indented as shown in the image on the right. If all the edge connected components become double connected. You have to make A, B, C, D in the right image.
able to connect with each other. It can be clearly seen that the right image and add 2 additional edges to meet the requirements. Add (A, b) so that A, B, d are doubly connected to each other
The . Add (b, c) or (A, c) to make the whole part A, B, C and D interconnected.
Then the third step is to find out the number of points with all degrees 1 after the indent point. Result ans = (number of points after the indent is 1 + 1)/2.
Reference post: http://blog.csdn.net/lyy289065406/article/details/6762370
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 1100;const int MAXM = 5500;struct edgenode{int to; int next;} Edges[maxm];int head[maxn];int dfn[maxn],low[maxn],stack[maxn],degree[maxn];int m,id,scc,lay,M,N;void AddEdges (int U,int v) {edges[id].to = v; Edges[id].next = Head[u]; Head[u] = id++;} void Tarjanbfs (int pos,int from) {stack[m++] = pos; Dfn[pos] = Low[pos] = ++lay; for (int i = Head[pos]; I! =-1; i = edges[i].next) {if (edges[i].to = = from) continue; if (!dfn[edges[i].to]) {TARJANBFS (edges[i].to,pos); Low[pos] = min (low[pos],low[edges[i].to])//if (Dfn[pos] < low[edges[i].to])//{// while (stack[m--]! = edges[i].to);/}} else Low[pos] = min (low[pos],low[edges[i].t O]); }}int Main () {int u,v; while (~SCANF ("%d%d", &n,&m)) {memset (head,-1,sizeof (Head)); memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); memset (degree,0,sizeof (degree)); m = Lay = SCC = id = 0; for (int i = 0; i < M; ++i) {scanf ("%d%d", &u,&v); Addedges (U,V); Addedges (V,u); } for (int i = 1; I <= N; ++i) if (!dfn[i]) Tarjanbfs (1,-1); for (int i = 1, i <= N; ++i) {for (int j = head[i]; J! =-1; j = edges[j].next) { if (low[i]! = low[edges[j].to]) degree[low[i]]++; }} int ans = 0; for (int i = 1; I <= N; ++i) if (degree[i] = = 1) ans++; printf ("%d\n", (ans+1)/2); } return 0;}
POJ3352 Road Construction "side Double Unicom Component" "Tarjan"