Codeforces Beta Round #95 (Div. 2) D. Subway-side Double Unicom +SPFA

Source: Internet
Author: User

D. Subway

A subway scheme, classic for all Berland cities are represented by a set of n stations connected by n< /c5> passages, each of which connects exactly, stations and does no pass through any others. Besides, in the classic scheme one can get from any station to any other one along the passages. The passages can used to move in both directions. Between each pair of stations there are no more than one passage.

Berland mathematicians had recently proved a theorem that states that any classic scheme had a ringroad. There can is only one ringroad. In other words, in any classic scheme one can find the only scheme consisting of stations (where any of the neighbouring ones is linked by a passage) and this cycle doesn ' t contain no station more than once.

This invention had a powerful social impact as now the stations could being compared according to their distance from the Rin Groad. For example, a citizen could say ' I live in three passages from the Ringroad ' and another one could reply ' you loser, I Li ve in one passage from the Ringroad ". The Internet soon got filled with applications, promised to count the distance from the station to the ringroad (send A text message to a short number ...).

The Berland government decided to put a end to these disturbances and start to control the situation. You is requested to write a program which can determine the remoteness from the ringroad for each station by the city SUBW AY scheme.

Input

The first line contains an integerN(3≤N≤3000),NIs the number of stations (and trains at the same time) in the subway scheme. ThenNLines contain descriptions of the trains, one per line. Each line contains a pair of integersx i , y i   ( 1≤ x i , y i n ) and represents the presence of a passage from Station  x i  to station  y I . The stations is numbered from 1 to  n  in an arbitrary order. It is guaranteed That x i y i  and that no pair of stations contain more than one passage. The passages can used to travel both ways. It is guaranteed that the given description represents a classic subway scheme.

Output

Print n numbers. Separate the numbers by spaces, the i-th one should being equal to the distance of the I-th station From the ringroad. For the Ringroad stations print number 0.

Examples input
4
1 3
4 3
4 2
1 2
Output
Test Instructions: give you an n-point n-side graph with no direction, there must be a ring inside, so that you ask each point to the distance on this ringThe following:We run a Tarjan to find out what the ring is, and then re-map to find the shortest circuit or you can DFS to find the ring, and then DFS to seek distance or BFS distance
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>#include<queue>using namespacestd;Const intN = 1e5+Ten, M = 1e3+Ten, mod =1000000, INF = 1e9+ +; typedefLong Longll;intN,dfn[n],low[n],cnt,scc,iscut[n],d[n],q[n],top,belong[n],hav[n],inq[n];vector<int>G[n];vector<pair<int,int> >E[n];voidDfsintXintFA) {Dfn[x]= Low[x] = + +CNT; q[++top] =x; INQ[X]=1;  for(intI=0; I<g[x].size (); i++) {            intto =G[x][i]; if(fa==to)Continue; if(!Dfn[to])            {DFS (TO,X); LOW[X]=min (low[x],low[to]); }        Else if(Inq[to]) low[x] =min (low[x],dfn[to]); }    if(low[x]==Dfn[x]) {SCC++;  Do{Inq[q[top]]=0; HAV[SCC]++; Belong[q[top]]=SCC; } while(x!=q[top--]); }}voidTarjan () {DFS (1,-1);}intDist[n],vis[n];voidSPFA (intu) {queue<int>Q;    Q.push (U);  for(intI=0; i<=n;i++) {Dist[i]= INF, vis[i] =0; } dist[0] =0; vis[0] =1;  while(!Q.empty ()) {        intK =Q.front ();        Q.pop (); VIS[K]=0;  for(intj=0; J<e[k].size (); j + +) {            intto =E[k][j].first; intValue =E[k][j].second; if(dist[to]>dist[k]+value) {Dist[to]= dist[k]+value; if(!Vis[to]) {Vis[to]=1;                Q.push (to); }            }        }    }}voidsolve () { for(intI=1; i<=n;i++) {//cout<<belong[i]<<endl;         for(intj=0; J<g[i].size (); j + +) {            intA =i; intb =G[i][j]; if(hav[belong[a]]<=1&&hav[belong[b]]<=1) {E[a].push_back (Make_pair (b),1)); }            Else if(hav[belong[a]]<=1) {e[a].push_back (Make_pair (0,1)); }            Else if(hav[belong[b]]<=1) {e[0].push_back (Make_pair (b,1)); }}} SPFA (0);  for(intI=1; i<=n;i++) {        if(Dist[i]==inf) cout<<0<<" "; Elsecout<<dist[i]<<" "; }}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) {        intb; scanf ("%d%d",&a,&b);        G[a].push_back (b);        G[b].push_back (a); D[a]++; D[B]++;    } Tarjan ();    Solve (); return 0;}

Codeforces Beta Round #95 (Div. 2) D. Subway-side Double Unicom +SPFA

Related Article

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.