import sys
from collections import deque
n,m = map(int , sys.stdin.readline().split(" "))
c = [[] for _ in range(n+1)]
for _ in range(m):
a,b = map(int , sys.stdin.readline().split(" "))
c[a].append(b); c[b].append(a)
def bfs():
global n; global c
visited = [-1 for _ in range(n+1)]
root = deque([[1, 0]])
while root:
pops = root.popleft()
if visited[pops[0]] == -1:
visited[pops[0]] = pops[1]
for i in c[pops[0]]:
if visited[i] == -1:
root.append([i, pops[1]+1])
return visited
k = int(sys.stdin.readline())
for _ in range(k):
a, ct1, ct2 = map(int, sys.stdin.readline().split(" "))
if a == 1:
c[ct1].append(ct2); c[ct2].append(ct1)
else:
c[ct1].remove(ct2); c[ct2].remove(ct1)
ans = bfs()
print(0, end=" ")
for i in range(2, n):
print(ans[i], end=" ")
print(ans[n])
https://www.acmicpc.net/problem/14217
그래프탐색 문제를 통하여 코딩테스트 문제연습을 하였습니다.