using
System;
using
System.Collections.Generic;
class
MainClass {
public
static
void
MinDisIncludingNode(
int
n,
int
m,
int
[][] edges) {
List<
int
>[] g =
new
List<
int
>[10005];
for
(
int
i = 0; i < 10005; i++) {
g[i] =
new
List<
int
>();
}
for
(
int
i = 0; i < m; i++) {
int
a = edges[i][0] - 1;
int
b = edges[i][1] - 1;
g[a].Add(b);
g[b].Add(a);
}
Queue<
int
> q =
new
Queue<
int
>();
q.Enqueue(0);
int
[] dist =
new
int
[n];
for
(
int
i = 0; i < n; i++) {
dist[i] =
int
.MaxValue;
}
dist[0] = 0;
while
(q.Count != 0) {
int
x = q.Dequeue();
if
(x == n - 1)
continue
;
foreach
(
int
y
in
g[x]) {
if
(dist[y] > dist[x] + 1) {
dist[y] = dist[x] + 1;
q.Enqueue(y);
}
}
}
Queue<
int
> q1 =
new
Queue<
int
>();
q1.Enqueue(n - 1);
int
[] dist1 =
new
int
[n];
for
(
int
i = 0; i < n; i++) {
dist1[i] =
int
.MaxValue;
}
dist1[n - 1] = 0;
while
(q1.Count != 0) {
int
x = q1.Dequeue();
if
(x == 0)
continue
;
foreach
(
int
y
in
g[x]) {
if
(dist1[y] > dist1[x] + 1) {
dist1[y] = dist1[x] + 1;
q1.Enqueue(y);
}
}
}
for
(
int
i = 0; i < n; i++) {
if
(dist[i] + dist1[i] >
int
.MaxValue)
Console.Write(-1 +
" "
);
else
Console.Write(dist[i] + dist1[i] +
" "
);
}
}
public
static
void
Main(
string
[] args) {
int
n = 5;
int
m = 7;
int
[][] edges = {
new
int
[] { 1, 2 },
new
int
[] { 1, 4 },
new
int
[] { 2, 3 },
new
int
[] { 2, 5 },
new
int
[] { 4, 3 },
new
int
[] { 4, 5 },
new
int
[] { 1, 5 } };
MinDisIncludingNode(n, m, edges);
}
}