from collections import deque
import sys
input = sys.stdin.readline
n, m, r = map(int, input().split())
visited = [0] * (n+1)
graph = [ [] for _ in range(n+1)]
ans = [0] * (n+1)
ans_idx = 1
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for list in graph:
list.sort()
def bfs(graph, r, visited):
queue = deque()
queue.append(r)
visited[r] = True
while queue:
global ans_idx
v = queue.popleft()
ans[v] = ans_idx
ans_idx += 1
for i in graph[v]:
if visited[i] == 0:
queue.append(i)
visited[i] = True
bfs(graph, r, visited)
for val in ans[1:]:
print(str(val))