Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var distance []int

type queue struct {
	q    []int
	head int
	tail int
}

func (q *queue) push(n int) {
	q.q[q.tail] = n
	q.tail++
}

func (q *queue) pop() int {
	q.head++
	return q.q[q.head-1]
}

func (q *queue) isEmpty() bool {
	return q.head == q.tail
}

func makeAdjList() [][]int {
	var n int
	fmt.Scanf("%d", &n)

	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, n)
	}

	sc := bufio.NewScanner(os.Stdin)
	for i := 0; i < n; i++ {
		sc.Scan()
		nodeInfo := strings.Split(sc.Text(), " ")
		outDegree, _ := strconv.Atoi(nodeInfo[1])

		if outDegree > 0 {
			for k := 0; k < outDegree; k++ {
				j, _ := strconv.Atoi(nodeInfo[k+2])
				j-- // convert 1 based to 0 based
				adj[i][j] = 1
			}
		}
	}

	return adj
}

func bfs(graph [][]int, s int) {
	distance = make([]int, len(graph))

	infty := -1
	for i := 0; i < len(distance); i++ {
		distance[i] = infty
	}
	distance[s] = 0

	q := &queue{make([]int, 1000), 0, 0}
	q.push(s)

	for !q.isEmpty() {
		u := q.pop()
		for i := 0; i < len(graph); i++ {
			if graph[u][i] == 0 {
				continue // u, i間に辺がない場合
			}
			if distance[i] != infty {
				continue // 訪問済みの場合
			}
			// 訪問するnodeの始点からの距離を設定し、訪問キューに入れる
			distance[i] = distance[u] + 1
			q.push(i)
		}
	}

	// print distance
	for i, d := range distance {
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	adj := makeAdjList()
	bfs(adj, 0)
}

Python: from collections import deque

N = int(input())

nodes = [tuple(map(int, input().split(' '))) for _ in range(N)]

distances = [-1] * (N + 1)

queue = deque()
queue.append((1, 0))

while len(queue) > 0:
    node, distance = queue.popleft()
    if distances[node] != -1:
        continue
    distances[node] = distance

    neighbors = nodes[node - 1][2:]
    for neighbor in neighbors:
        queue.append((neighbor, distance + 1))

for node in range(1, N + 1):
    print(node, distances[node])

Python: #coding: utf-8
inf = 100000000
n = int(input())

List = [0] + [list(map(int,input().split())) for i in range(n)]
color = [0] + ["white" for  i in range(n)]
d = [inf for i in range(n+1)]

s = 1
Q = [s]
d[s] = 0
while Q != []:
    q = Q.pop()
    if color[q] == "white":
        k = List[q][1]
        if k != 0:
            Q += List[q][2:2+k]
            for i in List[q][2:2+k]:
                d[i] = min(d[q] + 1, d[i])
    
    color[q] = "black"


for i, dis in enumerate(d[1:], 1):
    if dis == inf:
        dis = -1
    print(i, dis)

Python: # coding: utf-8
import queue

q = queue.Queue()
A = [[]*101 for _ in range(101)]
d = [-1]*101
visited = [False]*101

n = int(input().rstrip())
for i in range(n):
    line = [int(j) for j in input().rstrip().split()]
    A[i+1].extend(line[2:])

d[1] = 0

q.put(1)
while not q.empty():
    v = q.get()
    if visited[v] == True:
        continue
    visited[v] = True
    for i in A[v]:
        if d[i] == -1:
            d[i] = d[v] + 1
        else:
            d[i] = min(d[i],d[v] + 1)
        q.put(i)
        
for i in range(n):
    print(str(i+1)+" "+str(d[i+1]))

Python: import queue

n = int(input())

Graph = [[x-1 for x in list(map(int, input().split()))[2:]] for i in range(n)]


# for i in range(n):
#     li = list(map(int, input().split()))
#     a = li[0]
#     a-=1
#     b = li[2:]
#     Graph[a].append(b)


dist = [-1]*n
q = queue.Queue()

dist[0] = 0
q.put(0)

while not q.empty():
    v = q.get()

    for nv in Graph[v]:
        if dist[nv] != -1: continue

        dist[nv] = dist[v] + 1
        q.put(nv)


for v in range(n):
    print(v+1, dist[v])

Python: INF = 10**18 + 7


def main():
    from collections import deque
    N = int(input())
    G = [[] for _ in range(N)]
    for _ in range(N):
        u, k, *A = (int(i)-1 for i in input().split())
        G[u] = A

    def bfs(s=0):
        que = deque([])
        dist = [INF]*N
        dist[s] = 0
        que.append(s)
        while que:
            pos = que.popleft()
            for v in G[pos]:
                if dist[v] != INF:
                    continue
                dist[v] = dist[pos] + 1
                que.append(v)
        return dist

    dist = bfs()
    for i, d in enumerate(dist, start=1):
        print(i, d if d != INF else -1)


if __name__ == '__main__':
    main()

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using Graph = vector<vector<int>>;

int main(){
    int n; cin>>n;
    Graph G(n);
    rep(i,n){
        int u,k; cin>>u>>k;
        rep(j,k){
            int v; cin>>v;
            G[u-1].push_back(v-1);
        }
    }

    vector<int> dist(n,-1);
    queue<int> que;

    dist[0]=0;
    que.push(0);

    while(!que.empty()){
        int v = que.front();
        que.pop();

        for(auto nv : G[v]){
            if(dist[nv]!=-1)continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i,n){
        cout<<i+1<<" "<<dist[i]<<endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <queue>
using namespace std;
struct bfs{
    int n;
    int l;
};
int main(){
    int n,a,m,temp,ans[101];
    bool graph[101][101];
    bfs t,b;
    queue<bfs> q;
    scanf("%d",&n);
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++){
            graph[i][j]=false;
        }
        ans[i]=-1;
    }
    for(int i=0;i<n;i++){
        scanf("%d%d",&a,&m);
        for(int j=0;j<m;j++){
            scanf("%d",&temp);
            graph[a][temp]=true;
        }
    }
    t.n=1;
    t.l=0;
    q.push(t);
    while(!q.empty()){
        t=q.front();
        q.pop();
        if(ans[t.n]==-1)ans[t.n]=t.l;
        else continue;
        t.l++;
        b=t;
        for(int i=1;i<=n;i++){
            if(graph[t.n][i]){
                b.n=i;
                q.push(b);
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d %d\n",i,ans[i]);
    }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <string.h>
#include <limits.h>
using namespace std;
vector<int> v[101];
int d[101];
queue<int> q;
bool flag[101];
int main(){
	int n, u, k; scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		scanf("%d %d", &u, &k);
		for (int j = 0; j < k; ++j){
			int tmp; scanf("%d", &tmp);
			v[u].push_back(tmp);
		}
	}
	memset(d, -1, sizeof(d));
	d[1] = 0;
	q.push(1); flag[1] = true;
	while (!q.empty()){
		int m = q.front(); q.pop();
		for (int i = 0; i < v[m].size(); ++i){
			int tmp = v[m][i];
			if (!flag[tmp]){
				flag[tmp] = true;
				d[tmp]=d[m]+1;
				q.push(tmp);
			}
		}
	}
	for (int i = 1; i <= n; ++i){
		cout << i << ' ' << d[i] << '\n';
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const int MAX = 105;

vector<int> to[MAX];
bool seen[MAX];
int d[MAX];

void bfs(int u) {
  queue<int> q;
  seen[u] = true;
  d[u] = 0;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v: to[u]) {
      if (seen[v]) continue;
      d[v] = d[u] + 1;
      seen[v] = true;
      q.push(v);
    }
  }
}

int main() {
  int n, u, k, v;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      to[u-1].push_back(v-1);
    }
  }

  rep(i, n) {
    seen[i] = false;
    d[i] = INF;
  }

  bfs(0);

  rep(i, n) {
    cout << i+1 << " ";
    if (seen[i]) cout << d[i] << endl;
    else cout << -1 << endl;
  }



  
  return 0;
}