codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

131D.md (2656B)


      1 Source: https://codeforces.com/problemset/problem/131/D
      2 
      3 D. Subway
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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.
     22 
     23 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.
     24 
     25 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...).
     26 
     27 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.
     28 
     29 Input
     30 
     31 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 *x**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 are 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 be used to travel both ways. It is guaranteed that the given description represents a classic subway scheme.
     32 
     33 Output
     34 
     35 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.
     36 
     37 Examples
     38 
     39 Input
     40 
     41 ```
     42 4  
     43 1 3  
     44 4 3  
     45 4 2  
     46 1 2
     47 ```
     48 
     49 Output
     50 
     51 ```
     52 0 0 0 0
     53 ```
     54 
     55 Input
     56 
     57 ```
     58 6  
     59 1 2  
     60 3 4  
     61 6 4  
     62 2 3  
     63 1 3  
     64 3 5
     65 ```
     66 
     67 Output
     68 
     69 ```
     70 0 0 0 1 1 2
     71 ```