Codeforces Beta Round #95 (Div. 2) (點雙聯通)

來源:互聯網
上載者:User
D. Subwaytime limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A subway scheme, classic for all Berland cities is represented by a set of n stations connected by n passages,
each of which connects exactly two stations and does not 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 be used to move in both directions. Between each pair of stations
there is no more than one passage.

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

This invention had a powerful social impact as now the stations could be compared according to their distance from the ringroad. For example, a citizen could say "I live in three passages from the ringroad" and another one could reply "you loser, I live in
one passage from the ringroad". The Internet soon got filled with applications that 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 an end to these disturbances and start to control the situation. You are requested to write a program that can determine the remoteness from the ringroad for each station by the city subway scheme.

<p< p=""><p< p="">

Input

<p< p="">

The first line contains an integer n (3 ≤ n ≤ 3000), n is
the number of stations (and trains at the same time) in the subway scheme. Then n lines contain descriptions of the trains, one per line. Each line contains
a pair of integers xi, yi (1 ≤ xi, yi ≤ n)
and represents the presence of a passage from station xi to station yi.
The stations are numbered from 1 ton in an arbitrary order. It is guaranteed that xi ≠ yi and
that no pair of stations contain more than one passage. The passages can be used to travel both ways. It is guaranteed that the given description represents a classic subway scheme.

<p< p=""><p< p="">

Output

<p< p="">

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

<p< p=""><p< p="">

Sample test(s)

<p< p=""><p< p="">

input
41 34 34 21 2
output
0 0 0 0 
input
61 23 46 42 31 33 5
output
0 0 0 1 1 2 

#include<iostream>using namespace std;#define MAXN 10000#define INF 999999struct Edge { int v, next; };Edge edge[MAXN];int dfn[MAXN], low[MAXN];int stk[MAXN], top, id;int head[MAXN], E;int dis[MAXN], block[MAXN];void add_edge ( int u, int v ){    E++;    edge[E].v = v;    edge[E].next = head[u];    head[u] = E;}void Tarjan ( int u, int father ){    stk[++top] = u;    dfn[u] = low[u] = ++id;    for ( int i = head[u]; i != -1; i = edge[i].next )    {        if ( edge[i].v == father ) continue;        if ( dfn[edge[i].v] == 0 )        {            Tarjan ( edge[i].v, u );            low[u] = min ( low[u], low[edge[i].v] );            if ( low[edge[i].v] > dfn[u] )                top--;            else if ( low[edge[i].v] == dfn[u] )            {                int t, cnt = 0;                do                {                    t = stk[top--];                    block[++cnt] = t;                } while ( t != u );            }        }        else            low[u] = min ( low[u], dfn[edge[i].v] );    }}void BFS (){    int que[MAXN];    bool vis[MAXN] = { 0 };    int front = 0, rear = 0;    que[rear++] = block[1];    vis[block[1]] = true;    while ( front < rear )    {        int u = que[front++];        for ( int i = head[u]; i != -1; i = edge[i].next )        {            if ( ! vis[edge[i].v] )            {                if ( dis[edge[i].v] == INF )                    dis[edge[i].v] = dis[u] + 1;                que[rear++] = edge[i].v;                vis[edge[i].v] = true;            }        }    }}int main(){    int n, u, v;    cin >> n;    for ( int i = 0; i < n * 2 + 1; i++ )    {        head[i] = block[i] = -1;        dfn[i] = low[i] = 0;        dis[i] = INF;        E = top = id = 0;    }    for ( int i = 1; i <= n; i++ )    {        cin >> u >> v;        add_edge ( u, v );        add_edge ( v, u );    }    Tarjan ( 1, 0 );    for ( int i = 1; i <= n; i++ )        dis[block[i]] = 0;    BFS();    for ( int i = 1; i < n; i++ )        cout << dis[i] << ' ';    cout << dis[n] << endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.