#1184: Connectivity two • Double connected components of the edgeTime limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe
In the basic network after the completion of the school to facilitate the management of all the servers need to be grouped, the network's teacher found little hi and small ho, I hope they help.
The teacher told Little Hi and Little ho: according to the current situation of the network, we want to group servers, for the same group of servers, should be satisfied: when any one of the groups in the disconnection, the group will not affect the connectivity of the server. Under these conditions, the more servers in each group, the better.
For example, there are 6 servers and 7 connections:
It contains 2 groups, {1,2,3},{4,5,6}, respectively. For {A-n-a}, when 1-2 is broken, there are still 1-3-2 can connect 1 and 2, and when 2-3 is broken, 2-1-3 can be connected 2 and 3, and 1-3-1-2 can be connected after 3 is disconnected. {4,5,6} This is the same group.
The teacher told the whole network about little Hi and Little ho, Little Hi and Little ho. To calculate the packet information for each server.
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: An integer that represents the number of server groups for the network.
Line 2nd: n integers, the number of the number of I is the first server to belong to the group, the numbering of the smallest server. For example, {1,2,3},{4,5,6}, output {1,1,1,4,4,4}, {1,2,2,1,1,2} if divided into {1,4,5},{2,3,6}
Sample input
6 71 21 32 33 44 54 65 6
Sample output
21 1 1 4 4 4
Analysis: The approach is actually similar to the strongly connected component of the graph. It is important to note that the graph does not need to determine whether the point is in the stack. Why is there no map without marking, at that time because, the edge is u->v, there is also a side from the edge v->u because you have been marked before, and traverse to the current node V is not through the edge of W (u,v), there must be another path to make you and V are interlinked, thereby u, V is a double-connected.
Strong connected components of graphs, cut points, bridges, blocks, reference: http://blog.csdn.net/shiqi_614/article/details/7833628
Title Link: http://hihocoder.com/problemset/problem/1184
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];stack<int>sta;bool instack[maxn];int belong[maxn];int MIN_NUM[MAXN ];int idx,sccno;void Init () {for (int i=0;i<maxn;i++) graph[i].clear (); while (!sta.empty ()) Sta.pop (); memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); Memset (Belong,0,sizeof (belong)); memset (min_num,0,sizeof (min_num)); memset (instack,false,sizeof (instack)); idx=0; sccno=0;} 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) {low[u]=dfn[u]=++idx;Instack[u]=true; Sta.push (U); BOOL Flag=true; for (int i=0;i<graph[u].size (); i++) {int v=graph[u][i]; if (v==father) continue;//if (!dfn[v]) {Tarjan (v,u); Low[u]=min (Low[u],low[v]); The Else//graph does not need a stack to mark Low[u]=min (Low[u],dfn[v]); } if (Low[u]==dfn[u]) {sccno++; Min_num[sccno]=u; while (!sta.empty ()) {int j=sta.top (); Sta.pop (); Instack[j]=false; Belong[j]=sccno; Min_num[sccno]=min (MIN_NUM[SCCNO],J); if (j==u) break; }}}void Solve () {//for (int i=1;i<=n;i++) {//if (!dfn[i]) Tarjan (i,i);//} Tarjan (n/a); printf ("%d\n", SCCNO); for (int i=1;i<=n;i++) {printf ("%d", Min_num[belong[i]]); } printf ("\ n");} int main () {init (); Input (); Solve (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hihocoder_#1184_ Connectivity Two • Double connected components of the edge