http://hihocoder.com/problemset/problem/1190?sid=1051696
Just copy it.
Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description
When little Hi and Little Ho came back to school from John's house, the teacher of the network found the little Hi and Little ho.
The teacher told Little Hi and Little ho: There was a problem with the group before, and when the server (last time it was connected) went down, the servers in the same group might not be able to connect, so they wanted to group again. This time the teacher wants to group the connections , and the servers associated with all the connections within a group are also considered servers within the group (note that a server may belong to more than one group).
This time the condition is satisfied for the same group: when any one of the servers in the group goes down, the connectivity of the other servers in the group is not affected. Under these conditions, the number of edges in each group is as good as possible.
For example, there are 6 servers and 7 connections:
It contains 3 groups, {(), (2,3), (3,1)},{(4,5), (5,6), (4,6)},{(3,4)}. for {(x), (2,3), (3,1)}, there are {three-to-one} three servers associated with the group edge: When 1 is down, 2-3 can be connected 2 and 3; When 2 is down, 1-3 can be connected 1 and 3, and 3 can be connected 1-2 and 1 when 2 is down.
The teacher told the whole network of small hi and small ho, hope little Hi and small ho statistics a total of how many groups.
Tip: The double-connected component of a point
Input
Line 1th: 2 positive integers, n,m. Represents the number of points N, the number of edges M. 1≤n≤20,000, 1≤m≤100,000
2nd.. M+1 line: 2 positive integers, u,v. Line i+1 indicates that there is an edge (u,v), numbered I, connected to the U,v two servers. 1≤u<v≤n
Ensure that there is at least one connecting path between the inputs.
Output
Line 1th: An integer that represents the number of connection groups for the network.
Line 2nd: M integer, Number I indicates the number of the lowest number of connections within the group to which I am connected . For example, [1], (2,3) [3], (3,1) [2]},{(4,5) [5], (5,6) [7], (4,6) [6]},{(3,4) [4]}, in square brackets the number is output {1,1,1,4,5,5,5}.
-
Sample input
-
6 71 21 32 33 44 54 65 6
-
Sample output
-
31 1 1 4 5 5 5
Tip: The double-connected component of a point
Little ho: So what's the difference between this one and the previous one?
Little hi: In the last time, we solved the double connected component of the edge, and this time we called the point of the double connected component , which is defined as:
For a sub-graph of an undirected graph, when any one of these points is deleted, the connectivity of the points in the plot is not changed, so the sub-graph is called the double-connected sub-graph of the point. When the number of sides of a sub-graph reaches the maximum, the two connected components are called points.
Little ho: Is that different from the last time?
Little hi: The difference from the previous one is that the following situation may occur:
There are two groupings, respectively {(2,3), (3,1)},{(3,4), (4,5), (3,5)}. It cannot be grouped because when server 3rd goes down, the 4,5 is no longer connected.
Little ho: Feels like a lot harder?
Small hi: Actually also good, this time only need to make a little bit of improvement.
First we can guess from the above example that the bridge must be a double-connected component as a single point. In the area where the bridge is divided, there are two possible scenarios:
In the first case, each side of the bridge is a connected component, then the existence of the bridge is divided into 3 connected components, the bridge itself as a point of the two-connected components , and a, a two components can not be determined. In this figure, a, b two point is essentially a cut point.
In the second case, the bridge is connected and the other side is a separate point. The presence of the bridge divides the entire graph into 2 connected components, and the B-point part is not a group because there is no edge. In this figure, only the a point is the cut point.
Then we can first separate the entire graph according to the bridge.
A special case arises in the two-connected component components of a point, which is caused by the presence of a cut point in a double-connected component of an edge. Then we need to find the cut point in each of the connected components of the bridge.
Small ho: Simple every time there is a bridge to split the graph once, there is a cut point in each connected component to split the graph once.
Small hi: Yes, but in fact, it can be considered more closely, for the two cases of the bridge, it divides the number of regions just equal to +1 of the cut point, and the cut points within the connected components are also, each there is a cut point, the point of the two connected components to increase one.
Small ho: So, as long as the number of cut points, the point of the two-connected component is equal to the number of cut points plus 1?
Little hi: Yes, each one has a cut point, it divides a region into one, so the final result is to count the number of cut points. In the case of grouping, we still use the stack to help us record, the code is as follows:
void Dfs (int u) {//Record DFS traversal order static int counter = 0;//record node u subtree number int children = 0; Arcnode *p = graph[u].firstarc;visit[u] = 1;//initialize DFN with lowdfn[u] = Low[u] = ++counter;for (; p! = NULL; p = p->next) {int v = p->adjvex;if (Edge (u,v) is already marked) continue; Node v is not accessed, then (U,V) is the tree edge if (!visit[v]) {children++;p arent[v] = u;edgestack[top++] = Edge (U,V);//edge is placed on the stack Dfs (v); Low[u] = min (Low [u], low[v]);//case (1) if (parent[u] = = NIL && Children > 1) {printf ("Articulation point:%d\n", u);//Mark Edge The edges are stacked until the current side is out of the stack, and the edges are marked with the same group do {Nowedge = edgestack[top];top--;//tag nowedge}while (Nowedge! = Edge (U,v))}//case (2) if ( Parent[u] = NIL && low[v] >= Dfn[u]) {printf ("Articulation point:%d\n", u);//Mark edge//will edge out of the stack until the current side is out of the stack, these side labels Remember that the same group do {Nowedge = edgestack[top];top--;//tag nowedge}while (Nowedge! = Edge (u,v))}}//node v has been accessed, then (U,V) is the back edge else if (v! = Parent[u]) {edgestack[top++] = Edge (U,V); Low[u] = min (Low[u], dfn[v]);}}}
On the double-link component of points
I've been doing this for a long time. Now it turns out that the edges are actually chunked. The previous forward graph strong unicom component and the side double Unicom component, those are the point to divide the block.
Now it is necessary to block the edges in order to solve
The problem.
Vertex 3 belongs to two-point two-component. So I can't save a bit.
Then if the edge of the block, the judge to a point is a cut point, the current has the edge pop-up. Until the cur node is judged to be the edge of the cut point.
See the code for details, with the opposite and non-reversed edges. Note that an edge may be enumerated two times (without a tomoo edge two times), and then else if it can be judged if it is enumerated.
There is also a cut point that will be enumerated multiple times.
For example
There are three sides.
1 2
1 3
1 4
Then each side is a different component of the Unicom.
Point of the Unicom component equals cut point + 1? I don't feel it, but the example above is not.
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN =100000+ -;structEdge {intu, V, id; intTonext;} E[MAXN*2];intFIRST[MAXN], num;voidAddedge (intUintVintID) {++num; E[NUM].U= u, e[num].v = V, e[num].id =ID; E[num].tonext=First[u]; First[u]=num;}intDFN[MAXN], LOW[MAXN], when;intST[MAXN], top;intID[MAXN], toselid;intANS[MAXN];BOOLFLAG[MAXN];Const intRoot =1;voidTarjan (intCurintFA) {Dfn[cur]= Low[cur] = + +When ; for(inti = first[cur]; I i =E[i].tonext) { intv =e[i].v; if(v = = FA)Continue; if(!Dfn[v]) {st[++top] =e[i].id; Tarjan (v, cur); Low[cur]=min (low[cur], low[v]); if(Low[v] >= dfn[cur]) {//This is a cut point, the special case is 2.1 sides, the same cut point will be judged many times++Toselid; Do { intEID = st[top--]; Id[eid]= Toselid;//this side belongs to that block .Ans[toselid] =min (ans[toselid], EID); } while(St[top +1] !=e[i].id); } } Else if(Dfn[cur] > Dfn[v]) {//5-->4 the opposite side, but the 4 also enumerates the 4-->5 side, which is useless, and has been countedLow[cur] =min (low[cur], dfn[v]); st[++top] = e[i].id;//Reverse Edge } }}voidSolvetarjan (intN) {memset (DFN,0,sizeofDFN); memset (Low,0,sizeofLow ); when= top = Toselid =0; for(inti =1; I <= N; ++i) {if(!Dfn[i]) Tarjan (i, I); }}voidWork () {intN, M; CIN>> N >>m; for(inti =1; I <= m; ++i) {intu, v; CIN>> u >>v; Addedge (U, V, i); Addedge (V, U, i); } memset (ans,0x3f,sizeofans); Solvetarjan (n); cout<< Toselid <<Endl; for(inti =1; I <= m; ++i) {cout<< Ans[id[i]] <<" "; }}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endifWork (); return 0;}View Code
Hihocoder #1190: Connectivity • Four-point dual component