import sys
input = sys.stdin.readline
from collections import deque
n,m,r = map(int, input().split())
graph = [ [] for _ in range(n+1)]
visited = [0] * (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 lst in graph:
lst.sort(reverse=True)
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(val)