Shortest path - MarisaOJ: Marisa Online Judge

Shortest path

Time limit: 1000 ms
Memory limit: 256 MB
Given a undirected graph of $n$ vertices and $m$ edges. Find the shortest distance from $1$ to each vertex from $2$ to $n$. ### Input - The first line contains 2 integers $n, m$. - The next $m$ lines, each line contains 2 integers $u, v$, there is an edge between those vertices. ### Output - Print $n - 1$ numbers, $i^{th}$ number is the distance from $1$ to $i + 1$. If there is no way to reach $i + 1$ from $1$, print `-1`. ### Constraints - $1 \le n, m \le 10^5$. - $1 \le u, v \le n$. ### Example Input: ``` 5 4 1 2 1 3 2 3 4 5 ``` Output: ``` 1 1 -1 -1 ```