Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <iostream>
#include <queue> 
using namespace std;
int V[100][100];
int d[100];
int t[100];
int n;
queue<int> qu;
void bfs()
{
	t[0] = 1;
	d[0] = 0;
	qu.push(0);
	while (!qu.empty())
	{
		int u = qu.front(); qu.pop();
		for (int i = 0; i < n; i++)
		{
			if (V[u][i] == 1 && t[i] == 0)
			{
				t[i] = 1;
				d[i] = d[u] + 1;
				qu.push(i);
			}
		}
		t[u] = 2;
	}
	return;
}

int main()
{
	int k, u, v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			V[i][j] = 0;
		}
	}

	for (int i = 0; i < n; i++)
	{
		cin >> u >> k;
		for (int j = 0; j < k; j++)
		{
			cin >> v;
			V[u - 1][v - 1] = 1;
		}
	}

	for (int i = 0; i < n; i++)
	{
		d[i] = -1;
		t[i] = 0;
	}
	bfs();
	for (int i = 0; i < n; i++)
	{
		cout << i + 1 << " " << d[i] << endl;
	}
	return 0;
}

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

class Node{//普通の木用のデータ
//ノードはポインタでもっておこう
public:
  ///ノードの番号
  int num;
  //そのノードの親
  //Node* p;(親の情報が必要ない)
  //そのノードの子(ここ、vectorで管理してる場合はintの方が扱いやすい)
  vector<int> v;
  //探索済みかどうか
  bool visit=false;
};

void breadth_search(queue<int> vs,vector<Node>& vn,vector<int>& vd){
  int d=0;//vsにすでに格納しているものは距離が決定している前提
  int l=vs.size();
  while(l!=0){
    d++;
    l=vs.size();
    for(int i=0;i<l;i++){
      int x=vs.front();
      int l_sub=vn[x].v.size();
      for(int j=0;j<l_sub;j++){
        if(vn[vn[x].v[j]].visit==false){
          vs.push(vn[x].v[j]);
          vn[vn[x].v[j]].visit=true;
          vd[vn[x].v[j]]=d;
        }
      }
      vs.pop();
    }
  }
}


int main(){
  int n;cin >> n;
  vector<Node> vn(n);for(int i=0;i<n;i++){vn[i].num=i;}
  vector<int> vd(n,-1);//深さを保存(辿れないと-1)
  queue<int> vs;//今見ているNodeのnumを入れておく

  for(int i=0;i<n;i++){
    int k;cin >> k >> k;
    for(int j=0;j<k;j++){
      int l;cin >>l;l-=1;
      //vn[l].p=&vn[i];
      vn[i].v.push_back(l);
    }
  }

  vn[0].visit=true;
  vd[0]=0;
  vs.push(0);
  breadth_search(vs,vn,vd);
  for(int i=0;i<n;i++){
    cout << i+1 << " " << vd[i] << endl;
  }



}

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

struct Node{
	int s;
	queue<int> Q;
};

int main(){
	int n;
	cin >> n;
	Node node[n + 1];
	for(int i = 1; i <= n; i++){
		int u, k, v;
		cin >> u >> k;
		node[u].s = -1;
		while(k--){
			cin >> v;
			node[u].Q.push(v);
		}
	}

	int p = 1, next, m;
	queue<int> move;
	node[p].s = 0;
	move.push(p);

	while(!move.empty()){
		p = move.front();
		//cout << "Pivot is " << p << endl;
		if(node[p].Q.empty()){
			//cout << "Nowhere to go from " << p << endl;
			move.pop();
		} else {
			m = node[p].Q.size();
			while(m--){
				next = node[p].Q.front();
				node[p].Q.pop();
				//cout << "Checking " << next << endl;
				if(node[next].s < 0){
					//cout << "Can go to " << next << endl;
					node[next].s = node[p].s + 1;
					move.push(next);
				}
			}
			move.pop();
		}
	}

	for(int i = 1; i <= n; i++)
		cout << i << " " << node[i].s << endl;
}

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

using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)

int M[100][100];
int d[100];
int n;
static const int INFTY = (1 << 21);

void bfs(int x) {
    queue<int> q;
    q.push(x);
    REP (i, n) d[i] = INFTY;

    d[x] = 0;
    while (!q.empty()) {
        int u;
        u = q.front();
        q.pop();
        REP(i, n) {
            if (M[u][i] == 0) continue;
            if (d[i] != INFTY) continue;
            d[i] = d[u] + 1;
            q.push(i);
        }
    }

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

int main() {
    cin >> n;
    int v, m, k;
    REP(i, n) {
        cin >> v >> m;
        v--;
        REP(j, m) {
            cin >> k;
            k--;
            M[v][k] = 1;
        }
    }

    bfs(0);
    return 0;
}

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

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

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

coid dfs(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 =.front(); q.pop();
     for(int v = 0; v < n; v++)
      {
        if(M[u][v] == 0)continue;
        if(d[v] != INFTY)continue;
        d[v] = d[u] + 1;
        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 <iostream>
#include <vector>
#include <queue>

int main()
{
	int n;
	std::cin >> n;
	std::vector<std::vector<int> > E(n); 
	for (int i = 0; i < n; i++)
	{
		int u, k;
		std::vector<int> G;
		std::cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++)
		{
			int g;
			std::cin >> g;
			g--;
			G.push_back(g);
		}
		E.at(u) = G;
	}
	std::vector<int> Dist(n, -1);
	Dist.at(0) = 0;
	std::queue<int> Que;
	Que.push(0);
	while (!(Que.empty()))
	{
		int s = Que.front();
		Que.pop();
		for (int i = 0; i < E.at(s).size(); i++)
		{
			int next = E.at(s).at(i);
			if (Dist.at(next) == -1)
			{
				Dist.at(next) = Dist.at(s) + 1;
				Que.push(next);
			}
		}
	}
	for (int i = 0; i < n; i++)
		std::cout << i + 1 << ' ' << Dist.at(i) << '\n';
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdlib>
int INF = 1e9;
int main(){
    long long n;
    std::cin >> n;
    std::vector<int> G[n];
    std::vector<int> used(n, 0);
    for(int i = 0; i < n; i++){
        int a, k;
        std::cin >> a >> k;
        a--;
        for(int j = 0; j < k; j++){
            int b;
            std::cin >> b;
            b--;
            G[a].push_back(b);
        }
    }
    std::vector<int> d(n, INF);
    std::queue<int> q;
    q.push(0);
    d[0] =0;
    while(!q.empty()){
        int t = q.front();
        q.pop();
        used[t] = 1;
        for(int i: G[t]){
            if(used[i] == 0){
                q.push(i);
                d[i] = std::min(d[i], d[t] + 1);
            }
        }
    }
    for(int i = 0; i < n; i++) if(d[i] == INF) d[i] = -1;
    for(int i = 0; i < n; i++) std::cout << i + 1 << ' ' << d[i] <<  std::endl;
}

C++: #include<iostream>
using namespace std;
int n, u,k[101], v[101][101], out[101];

void hitung(int a, int b){
	int i;
	out[a]=b;
	for(i=1; i<=k[a]; i++){
		if(out[v[a][i]]==-1 || out[v[a][i]]>b+1){
			hitung(v[a][i],	b+1);
		}
	}
}

int main(){
	int i,j;
    cin>>n;
    for(i=1; i<=n; i++){
        cin>>u>>k[i]; //getchar();
		out[i]=-1;
        for(j=1; j<=k[i]; j++){
            cin>>v[i][j];// getchar();
        }
    }
    
    hitung(1,0);
	for(i=1;i<=n;i++){
		cout<<i<<" "<<out[i]<<endl;
	}


    //getchar();
    return 0;
}