hihocoder_#1190_ Connectivity • Four • Double connected component (block) of Point

Source: Internet
Author: User

#1190: Connectivity • FourTime limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Describe

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.

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

Analysis:

The two-connected component definition of a point: for a sub-graph of an undirected graph, when any of these points are deleted, the connectivity of the points in the plot is not changed, and such a sub-graph is called a double-connected sub-graph of points. When the number of sides of a sub-graph reaches the maximum, the two connected components are called points.

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;

In the second case, the bridge is connected and the other side is a separate point. The existence of a bridge divides the entire graph into 2 connected components.

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.

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 cut point +1, and the cut points within the connected component are also, each there is a cut point, the two connected components of a pointJust add one.

Conclusion: The double connected component of points = number of cut points + 1.


Title Link: http://hihocoder.com/problemset/problem/1190

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 = 200000 + 5;struct node{int V,id,next ; }graph[maxv];int n,m,a,b;int dfn[maxn];int low[maxn];int belong[maxv];int number[maxv];int head[maxn];int num,idx,    Sccno,top;int sta[maxv];void init () {memset (dfn,0,sizeof (DFN));    memset (low,0,sizeof (Low));    memset (head,-1,sizeof (head));    Memset (Belong,0,sizeof (belong)); idx=0; sccno=0; num=0; Top=0;}    void Add (int u,int v,int ID) {graph[num].v=v;    Graph[num].next=head[u];    Graph[num].id=id; head[u]=num++;}    void input () {scanf ("%d%d", &n,&m);        for (int i=1;i<=m;i++) {scanf ("%d%d", &a,&b);        Add (a,b,i);    Add (b,a,i);    }}void Tarjan (int u,int father) {low[u]=dfn[u]=++idx; for (int i=head[u];I!=-1;i=graph[i].next) {int v=graph[i].v;        int id=graph[i].id;        if (v==father) continue;         if (Belong[id]) continue; Node v is not accessed, then (U,V) is a tree edge sta[top++]=id;            Edge-in-stack if (!dfn[v]) {Tarjan (v,u);            Low[u]=min (Low[u],low[v]);                if (Low[v]>=dfn[u]) {//child nodes cannot reach nodes earlier than u, then u is cut point sccno++;                int NO=MAXV;                int id=-1;                    while (id!=id&&top>=0) {id=sta[--top];                    Belong[id]=sccno;                if (no>id) No=id;            } Number[sccno]=no;        }} else{Low[u]=min (Low[u],dfn[v]);    }}}void Solve () {Tarjan ();    printf ("%d\n", SCCNO);        for (int i=1;i<=m;i++) {int be=belong[i];    printf ("%d", Number[be]); } 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_#1190_ Connectivity • Four • Double connected component (block) of Point

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.