#1183: Connectivity One • Cutting edges and cutting pointsTime limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe
Remember the last time little Hi and Little Ho School was hacked, the attack finally caused the school network data loss. To avoid the recurrence of such a situation, the school decided to redesign the campus network.
The school now altogether possesses n servers (number 1. N) and M-bar connections, ensure that any two servers can be connected directly or indirectly through the data communication.
When a hacker attacks, the school will immediately cut off a connection in the network or immediately shut down a server, so that the entire network is isolated into two separate parts.
For example, for the following networks:
At least one path is connected between every two points, and when you cut off the edge (3,4), you can see that the entire network is quarantined as {1,2,3},{4,5,6} two parts:
If server 3 is shut down, the entire network is quarantined as {1,2},{4,5,6} two parts:
Little Hi and Little ho want to know what connections and which points are closed in the school's network, allowing the entire network to be quarantined into two parts.
In the example above, there is an edge (3,4) that satisfies the condition, point 3 and point 4.
Tip: Cutting edges & cutting points
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. Indicates that there is an edge (u,v) and that the U,v two servers are connected. 1≤u<v≤n
Ensure that there is at least one connecting path between the inputs.
Output
Line 1th: Several integers, separated by spaces, indicating the server number that satisfies the requirement. Arranged from small to large. If there are no points to satisfy the requirement, the line outputs null
2nd.. K-line: 2 integers per line, (U,V) represents the edge that satisfies the requirement, u<v. All edges are sorted according to the size of U, u small row in front, when u phase, v small row in front. If no edge is satisfied, no output is required
-
Sample input
6 71 21 32 33 44 54 65 6
Sample output
3 43 4
-
Analysis: Tarjan The cutting point and cutting edge (bridge) of the undirected connected graph.
-
lemma: for a Dfs tree that communicates undirected graph g={v,e},s={v,t} to G, the node U is the cut point of G when and only if one of the following conditions is satisfied:
1. U is the root of T and U has at least two sons
2. U is not the root of T and exists a son of U W, so that descendants from W or W have no edge to connect back to the ancestor of U (note, not back to u itself).
-
Similar to a cut point, we can define a bridge with undirected connectivity graphs: If you delete an edge E, the undirected graph G is no longer connected, and the bridge called E is G. The decision of the bridge is not difficult, only to be judged when the T-Edge (U,V) is found. If after v its descendants cannot connect to the ancestor of U or U, then remove (U, v) after you and V are not connected. That is: the T-edge (U, v) is found and recursively traverses v if DFN[U]<LOW[V], then (U, v) is the bridge. Similar to the cut-off, we call the graph without a bridge a side-connected graph. If an undirected graph is an edge-connected, it can be directed to a strongly connected graph.
For a diagram of the strong Unicom component, block, cut Point, Bridge, click here: http://blog.csdn.net/shiqi_614/article/details/7833628
void Tarjan (int u,int father) {//father is the parent node of U dfn[u]=low[u]=++idx; int children=0; Calculates the number of sons of U for (int i=0;i<graph[u].size (); i++) { int v=graph[u][i]; if (v==father) continue; if (!dfn[v]) { children++; Tarjan (v,u); Low[u]=min (Low[u],low[v]); To determine whether to cut points, points may enter multiple times, so use set to facilitate if ((father==u&&children>1) | | (Father!=u&&low[v]>=dfn[u])) { Vertex.insert (u); judge=true; } Determine whether to cut edge (bridge) if (Low[v]>dfn[u]) edge[ed++]=edge (min (u,v), Max (u,v)) ;} else//Do not need to determine whether in the stack, low[u]=min (Low[u],dfn[v]);} }
-
Title Link: http://hihocoder.com/problemset/problem/1183
Code Listing:
#include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> Using namespace Std;typedef long long ll;const int maxn = 20000 + 5;const int maxv = 100000 + 5;int n,m,a,b;vector<int& Gt;graph[maxn];int dfn[maxn];int low[maxn];int ve,ed,root,idx;bool judge;set<int>vertex;struct Edge{int u,v; Edge () {} edge (int u,int v) {this, u = u; This is V = V; }}edge[maxv];bool cmp (Edge A,edge B) {if (A.U==B.U) return a.v<b.v; return a.u<b.u;} void Init () {for (int i=0;i<maxn;i++) graph[i].clear (); Vertex.clear (); memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); ve=0; Ed=0; idx=0; Judge=false;} void input () {scanf ("%d%d", &n,&m); for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b); Graph[a].push_back (b); Graph[b].push_back (a); }}void Tarjan (int u,int father) {//father is the parent node of U dfn[u]=low[u]=++idx; int children=0; Calculates the number of sons of U for (int i=0;i<graph[u].size (); i++) {int v=graph[u][i]; if (v==father) continue; if (!dfn[v]) {children++; Tarjan (V,u); Low[u]=min (Low[u],low[v]); To determine whether to cut points, points may enter multiple times, so use set to facilitate if ((father==u&&children>1) | | (Father!=u&&low[v]>=dfn[u])) {Vertex.insert (U); Judge=true; }//Determine whether to cut edge (bridge) if (Low[v]>dfn[u]) Edge[ed++]=edge (min (u,v), Max (u,v)); } else//Do not need to determine whether in the stack, Low[u]=min (Low[u],dfn[v]); }}void solve () {Tarjan (); Sort (edge,edge+ed,cmp); if (!judge) printf ("null\n"); else{set<int>::iterator It=vertex.begin (); for (; It!=vertex.end (); it++) printf ("%d", *it); printf ("\ n"); } for (int i=0;i<ed;i++) {printf ("%d %d\n ", EDGE[I].U,EDGE[I].V); }}int Main () {init (); Input (); Solve (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hihocoder_#1183_ Connectivity One • Cutting edges and cutting points