Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

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

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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

const int INF = 1001001001;
int n;
int v[100][100];
int d[100]; //?§??????????????????¢

void bfs(int s) //s????§????
{
  queue<int> q;
  q.push(s);
  for (int i = 0; i < n; i++) d[i] = INF;

  d[s] = 0;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();

    for (int i = 0; i < n; i++) {
      if (v[u][i] == 0) continue;
      if (d[i] != INF) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }

  for (int i = 0; i < n; i++) {
      cout << i + 1 << " " << ((d[i] == INF) ? -1 : d[i]) << endl;
  }
}

signed main()
{
  int u, k;

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

  bfs(0);

  return (0);
}

C++: #include <bits/stdc++.h>
using namespace std;
void main_sub(void);
int main(int argc, char* argv[]) {
    main_sub();
    return 0;
}
// int を int64_t にする
// g++ に -DUSE_STRICT_INT をつけるか、
// 下のコメントアウトを外すと無効化
// #define USE_STRICT_INT
using i64 = int64_t;
#ifndef USE_STRICT_INT
#define int i64
#endif
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = (1 << 30);
constexpr i64 INFL = (1LL << 62);
constexpr i64 MOD = 1000000007;

void main_sub(void) {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i, n) {
        int u, k;
        cin >> u;
        u--;
        cin >> k;
        rep(j, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    queue<pair<int, int>> q;
    vector<int> dist(n, INF);
    q.push({0, 0});
    while (!q.empty()) {
        int from, d;
        tie(from, d) = q.front();
        q.pop();
        if (dist[from] > d) {
            dist[from] = d;
        }
        for (auto& to : g[from]) {
            if (dist[to] > d + 1) {
                dist[to] = d + 1;
                q.push({to, d + 1});
            }
        }
    }
    rep(i, n) {
        cout << i + 1 << " " << (dist[i] == INF ? -1 : dist[i]) << endl;
    }

    return;
}

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

//そのまま再帰にかけて実行しては間に合わない。先に各部分木の根に対して、子に分配する数値を加算しておき、
//根である1から子に対して、根の数値を加算していく作業を一回だけ行えば条件は満たされる

void DFS(const Graph &G,int v,vector<bool> &seen,vector<int> &counter){//深さ優先探索。
//引数は順にグラフ、始点(注目点)、探索済みかどうか、各点の持つ値を記録するカウンタ
  
  seen[v]=true;

  for(auto next_v : G[v]){
    if(seen[next_v]) continue;
    counter[next_v]+=counter[v];
    DFS(G,next_v,seen,counter);
  }

  return ;

}

 
int main() {

  int N ;  cin >> N ;

  Graph G(N+1);//頂点はn個。今回は数値にそのまま対応させるために+1した

  rep(i,N){
    int u,k;
    cin >> u >> k;//頂点番号、頂点の数
    rep(j,k){
      int x; cin >> x;
      G[u].push_back(x);
    }
  }//入力終了

  vector<int> dist(N+1,-1);//全地点を未訪問で初期化
  
  dist[1]=0;//頂点1（スタート）の距離を0にする

  queue<int> que;//BFS用キュー

  que.push(1);

  while(!que.empty()){
    int v = que.front();
    que.pop();          //先頭頂点を取り出す
    
    for(int nv : G[v]){
      if(dist[nv]!=-1) continue;//探索済みならスルー

      //初めて候補に挙がった点は距離を入力し、キューに入れる
      dist[nv]= dist[v]+1;
      que.push(nv);
    }

  }

  for(int i=1;i<=N;i++){
    cout << i << ' ' << dist[i] <<endl ;
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<cstdio>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;
int n ;
vector<int> G[MAX];
int color [MAX];
void dfs (int r,int c)
{
  stack <int> S;
  S.push(r);
  color[r] = c;
  while(!S.empty())
  {
    int u = S.top();
    S.pop();
    for(int i = 0;i<G[u].size();i++)
        {
          int v = G[u][i];
          if(color[v] == NIL)
          {
            color[v] = c;
            S.push(v);
          }
        }
  }
}
void ass()
{
  int id = 1;
  for(int i = 0;i<n;i++)
    color[i] = NIL;
  for(int u = 0;u<n;u++)
  {
    if(color[u] == NIL)
    dfs(u,id++);
  }
}
int main ()
{
  int s,t,m,q;
  cin>>n>>m;
  for(int i = 0;i<m;i++)
    {
      cin>>s>>t;
      G[s].push_back(t);
      G[t].push_back(s);
    }
  ass();
  cin>>q;
  for(int i = 0;i<q;i++)
  {
    cin>>s>>t;
    if(color[s] == color[t])
    {
      cout<<"yes"<<endl;
    }
    else cout<<"no"<<endl;
  }
  return 0 ;
}

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

typedef struct{
  int A[1000];
  int ans;
  bool c;
}node;
node T[1000];
int Q[100000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    for(int i=2;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	}
	Q[tail++]=i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    num=Q[head++];
    //  cout<<head<<" "<<tail<<endl;
  }

}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }
  Q[0]=1;
  function(1);
  T[1].ans=0;
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

  for(int i=1;i<=n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }


  return 0;
}