Algorithm


D. Subway
time 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.

Input

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 to n 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.

Output

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.

Examples
input
Copy
4
1 3
4 3
4 2
1 2
output
Copy
0 0 0 0 
input
Copy
6
1 2
3 4
6 4
2 3
1 3
3 5
output
Copy
0 0 0 1 1 2 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#include <vector>

std::vector<std::vector<int> > edges;
std::vector<bool> visited;
std::vector<int> distance, parents;
int cycleStart(-1);
bool done(0);

void findCycle(int node, int caller){

    if(done){return;}
    //printf("Check for cycle, node %d called from %d\n", node, caller);
    if(parents[node] >= 0){
        //printf("***Cycle Detected starting at node %d\n", node);
        int currentParent = caller;
        cycleStart = caller;
        while(currentParent != node){
            //printf("Node %d is in the cycle\n", currentParent);
            distance[currentParent] = 0; 
            currentParent = parents[currentParent];
        }
        distance[currentParent] = 0;
        done = 1; return;
    }

    parents[node] = caller;
    for(int p = 0; p < edges[node].size(); p++){
        int nextNode = edges[node][p];
        if(nextNode == caller){continue;}
        findCycle(nextNode, node);
    }
}



void dfs(int node, int currentDist){
    if(visited[node]){return;}
    visited[node] = 1;
    for(int p = 0; p < edges[node].size(); p++){
        int nextNode = edges[node][p];
        if(visited[nextNode]){continue;}
        if(distance[nextNode] < 0){distance[nextNode] = 1 + currentDist;}
        //printf("Distance of node %d is %d\n", nextNode, distance[nextNode]);
        dfs(nextNode, distance[nextNode]);
    }
}

int main(){

    int n; scanf("%d\n", &n);
    edges.resize(n + 1); visited.resize(n + 1, 0), distance.resize(n + 1, -1); parents.resize(n + 1, -1);
    for(int p = 0; p < n; p++){int a, b; scanf("%d %d\n", &a, &b); edges[a].push_back(b); edges[b].push_back(a);}

    findCycle(1, 1);
    dfs(cycleStart, 0);
    for(int p = 1; p <= n; p++){printf("%d ", distance[p]);}; puts("");

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
1 3
4 3
4 2
1 2

Output

x
+
cmd
0 0 0 0
Advertisements

Demonstration


Codeforces Solution-D. Subway-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+