Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e2 + 10;

int n;
vector<int> g[MAXN];
int vis[MAXN];
int dep[MAXN];

void BFS(){
    int d=0;
    queue<int> q;
    for(int i=1;i<=n;i++) dep[i]=-1;
    dep[1]=0;
    q.push(1);
    vis[1]=1;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i];
            if(vis[v]) continue;
            dep[v]=dep[u]+1;
            vis[v]=1;
            q.push(v);
        }
    }
}

void work(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int u,k; scanf("%d%d",&u,&k);
        while(k--){
            int v; scanf("%d",&v);
            g[u].push_back(v);
        }
        sort(g[u].begin(),g[u].end());
    }
    BFS();
    for(int i=1;i<=n;i++) printf("%d %d\n",i,dep[i]);
}

int main(){
    work();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(void)
{
     //??£??\??????????????£??\???????????????
    int n, k;

    cin >> n;
    vector<vector<bool> > A(n, vector<bool>(n, false));
    for(int i = 0; i < n; i++){
        cin >> k >> k;
        for(int j = 0; j < k; j++){
            int input;
            cin >> input;
            A[i][input-1] = true;
        }
    }

    vector<int> u(n, 1000000);
    u[0] = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(u[j] == i){
                for(int k = 0; k < n; k++){
                    if(A[j][k] == 1) u[k] = min(i+1, u[k]);
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout << i << " " << u[i] << endl;
    }
    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
int a[101][101],num[101],dis[101];
bool vis[101];
int n,u,k,x,t=1;
queue<int> q;
int main(){
	memset(a,0,sizeof(a));
	memset(vis,false,sizeof(vis));
	memset(dis,0x1f1f1f1f,sizeof(dis));
	cin>>n;
	for (int i=0; i<n; i++){
		cin>>u>>k;
		num[u]=k;
		int j=0;
		while (k--){
			  cin>>x;
			  a[u][++j]=x;
		}
	}
	q.push(1);
	dis[1]=0;
	while (!q.empty()){
		  x=q.front(); q.pop(); vis[x]=true;
		  for (int i=1; i<=num[x]; i++){
		  	  if (!vis[a[x][i]]){
		  	     dis[a[x][i]]=min(dis[a[x][i]],dis[x]+1);
		  	     q.push(a[x][i]);
              }
		  }
	}
    for (int i=1; i<=n; i++){
    	cout<<i<<' ';
    	if (dis[i]==0x1f1f1f1f) dis[i]=-1;
		cout<<dis[i]<<endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

bool visited[111];
int d[111];
vector<vector<int> > g(111);
typedef pair<int, int> P;

int main(){
  int n; cin >> n;
  FOR(i,0,n){
    int u, k; cin >> u >> k;
    FOR(j,0,k){
      int v;
      cin >> v;
      g[u].push_back(v);
    }
  }
  FOR(i,0,n+1) visited[i] = false;
  FOR(i,0,n+1) d[i] = -1;
  queue<P> que;
  que.push(P(1,0));
  visited[1] = true;
  while(!que.empty()){
    P p = que.front();que.pop();
    int u = p.first;
    d[u] = p.second;
    for(auto &v : g[u]){
      if(!visited[v]){
        que.push(P(v, p.second + 1));
        visited[v] = true;
      }
    }
  }
  FOR(i,1,n+1){
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

C++: #if 0
	??????????????????????????????
#endif
#include <iostream>
#include <queue>

using namespace std;

int main(){
	int n, u, k, v;
	int D[100];
	bool adj[100][100] = {};
	int pushed[100] = {};
	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> u >> k;
		for (int j = 1; j <= k; j++){
			cin >> v;
			adj[u][v] = true;
		}
	}
	for (int i = 1; i <= n; i++){
		D[i] = -1;
	}
	queue<int> Q;
	Q.push(1);
	D[1] = 0;
	pushed[1] = 1;
	while(!Q.empty()){
		int m = Q.front();
		Q.pop();
		for (int i = 1; i <= n; i++){
			if (adj[m][i] == true && pushed[i] == 0){
				D[i] = D[m]+1;
				Q.push(i);
				pushed[i] = 1;
			}
		}
	}
	
	for (int i = 1; i < n+1; i++){
		cout << i << ' ' << D[i] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
const int INF = 2e9;
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

int main() {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i, 0, n) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, 0, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }

    int dist[n];
    rep(i, 0, n) dist[i] = -1;
    dist[0] = 0;
    queue<int> q;
    q.push(0);

    while (!q.empty()) {
        int x = q.front();
        q.pop();
        rep(i, 0, (int)g[x].size()) {
            int nx = g[x][i];
            if (dist[nx] != -1) continue;
            dist[nx] = dist[x] + 1;
            q.push(nx);
        }
    }

    rep(i, 0, n) {
        cout << i + 1 << " " << dist[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
	int n, u, k, v;
	cin >> n;
	vector<vector<int>> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> u >> k;
		for (int j = 0; j < k; ++j) {
			cin >> v;
			a[u - 1].push_back(v - 1);
		}
	}
	queue<int> b;
	b.push(0);
	vector<int> ans(n, -1);
	ans[0] = 0;
	while (!b.empty()) {
		for (int& i : a[b.front()]) {
			if (-1 == ans[i]) {
				ans[i] = 1 + ans[b.front()];
				b.push(i);
			}
		}
		b.pop();
	}
	for (int i = 0; i < n; ++i) cout << 1 + i << ' ' << ans[i] << endl;
}