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)
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
void func(vector<vector<int> > &G, int *depth)
{
    queue<int> q;
    int p = 1;
    q.push(p);
    depth[p] = 0;
    while (q.empty() == false)
    {
        p = q.front();
        q.pop();
        rep(i, 0, G[p].size())
        {
            if (depth[G[p][i]] == -1)
            {
                q.push(G[p][i]);
                depth[G[p][i]] = depth[p] + 1;
            }
        }
    }
}
int main()
{
    int n;
    cin >> n;
    int ans[n + 1], u, k, v;
    vector<vector<int> > G(n + 1, vector<int>(0));
    rep(i, 0, n)
    {
        ans[i + 1] = -1;
        cin >> u >> k;
        rep(j, 0, k)
        {
            cin >> v;
            G[u].push_back(v);
        }
    }
    func(G, ans);
    rep(i, 1, n + 1)
    {
        printf("%d %d\n", i, ans[i]);
    }
}

C++: #include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main(){
  int n;cin>>n;
  vector<ll> g[n+1];
  rep(i, n){
    ll u,k;cin>>u>>k;
    rep(j, k){
      ll t;cin>>t;
      g[u].push_back(t);
    }
  }

  vector<ll>result(n+1);
  rep1(i, n)result[i]=-1;

  result[1]=0;

  queue<ll> q;
  q.push(1);
  while(!q.empty()){
    ll c = q.front();
    q.pop();
    for(auto to: g[c]){
      if(result[to]>=0)continue;

      result[to] = result[c]+1;
      q.push(to);
    }
  }

  rep1(i, n)cout<<i<<' '<<result[i]<<endl;

  return 0;
}

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

int n;
queue<int> q;
vector<int> depth;

void dfs(int start, vector<vector<int>> a) {
    depth[start] = 0;
    q.push(start);

    while (!q.empty()) {
        int current = q.front();
        q.pop();
        rep(adj, n) {
            if (a[current][adj] == 1 && depth[adj] == -1) {
                depth[adj] = depth[current] + 1;
                q.push(adj);
            }
        }
    }
}

int main() {
    scanf("%d", &n);
    depth.resize(n+1, -1);
    vector<vector<int>> a(n+1, vector<int>(n+1, 0));
    rep(i, n) {
        int u;
        scanf("%d", &u);
        int k;
        scanf("%d", &k);
        rep(j, k) {
            int adj;
            scanf("%d", &adj);
            a[u][adj] = 1;
        }
    }
    dfs(1, a);
    rep(i, n) {
        printf("%d %d\n", i, depth[i]);
    }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cstdio>
#include<queue>

using namespace std;

int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    fill(D, D + N, -1);
    D[src] = 0;
    while (! Q.empty()){
        int cur = Q.front();
        Q.pop();
        cerr << cur+1 << " " << D[cur] << endl;
        for (int k = 0; k < N; k++){
            if (G[cur][k] == 1 && D[k] < D[cur]){
                D[k] = D[cur]+1;
                Q.push(k);
            }
        }
    }
}

int main(){
	while (~scanf("%d", &N) && N){
		int id, c;
		for (int i = 0; i < N; i++){
			scanf("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
			scanf("%d", V + id);
			for (int j = 0; j < V[id]; j++){
				scanf("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
				G[id][c] = 1;
			}
		}//データの読み込み完了。
        bfs(0);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;


vector<int>v[105];
int t[105]={};
bool flag[105]={};
void BFS(int p)
{
	queue<int>Q;	
	Q.push(p);
	flag[p]=true;
	while(!Q.empty())
	{
		p=Q.front();
		Q.pop();
		for(int i=0;i<v[p].size();i++)
		{
			if(!flag[v[p][i]])
			{
				t[v[p][i]]=t[p]+1;
				Q.push(v[p][i]);
			}
			flag[v[p][i]]=true;
		}
	}
}
int main()
{	
	int n,a,b,k;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a>>k;
		for(int j=0;j<k;j++)
		{
			cin>>b;
			v[a].push_back(b);
		}
	}
	BFS(1);
	for(int i=1;i<=n;i++)
	{
		cout<<i<<" ";
		if(i!=1&&t[i]==0)cout<<-1<<endl;
		else cout<<t[i]<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=0; i<n; i++) d[i] = INFTY;
    d[s] = 0;
    int u;
    while(!q.empty()){
        u = q.front(); q.pop();
        for(int v=0; v<n; v++){
            if(M[u][v] == 0) continue;
            if(d[v] != INFTY) continue;
            //cout <<v<<" "<<d[v];
            d[v] = d[u] + 1;
            //cout <<"->"<< d[v] << endl;
            q.push(v);
        }
    }
    for(int i=0; i<n; i++){
        cout << i+1 << " " << ((d[i] == INFTY) ? (-1):d[i]) << endl;
    }
}

int main(){
    int u, k, v;
    
    cin >> n;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++) M[i][j] = 0;
    }
    
    for(int i=0; i<n; i++){
        cin >> u >> k;
        u--;
        for(int j=0; j<k; j++){
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }
    
    bfs(0);
    
    return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> dst(n + 1);
  for (int i = 1; i <= n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      dst[i].push_back(v);
    }
  }
  queue<int> Q;
  vector<int> ans(n + 1, -1);
  ans[1] = 0;
  Q.push(1);
  while (Q.size()) {
    int p = Q.front();
    for (int d : dst[p]) {
      if (ans[d] == -1) {
        ans[d] = ans[p] + 1;
        Q.push(d);
      }
    }
    Q.pop();
  }
  for (int i = 1; i <= n; i++) {
    cout << i << ' ' << ans[i] << endl;
  }
}