Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)

	paths[1] = 1
	d := 2
	for len(queue) > 0 {
		u := queue.pop()

		pushed := false
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
				pushed = true
			}
		}
		if pushed {
			d += 1
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

struct edge {int to, cost;};
//=============dijkstra============================
vector<long long> dijkstra(int s, int V, vector<vector<edge>> G)
{
  vector<long long> d(V, INF);
  typedef pair<long long, int> P;// first は最短距離　second は頂点の番号
  priority_queue<P, vector<P>, greater<P>> que;
  d.at(s) = 0;
  que.push(P(0, s));
  while (!que.empty())
  {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d.at(v) < p.first) continue;
    for (int i = 0; i < G.at(v).size(); ++i)
    {
      edge e = G.at(v).at(i);
      if (d.at(e.to) > d.at(v) + e.cost)
      {
        d.at(e.to) = d.at(v) + e.cost;
        que.push(P(d.at(e.to), e.to));
      }
    }
  }
  return d;
}
//=================================================


int main()
{
  int n;
  cin >> n;
  vector<vector<edge>> G(n, vector<edge>(0));
  for (int i = 0; i < n; ++i)
  {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; ++j)
    {
      int v;
      cin >> v;
      G.at(u - 1).push_back({v - 1, 1});
    }
  }
  vector<long long> ans = dijkstra(0, n, G);
  for (int i = 0; i < n; ++i)
  {
    if (ans.at(i) == INF)
    {
      cout << i + 1 << ' ' << -1 << endl;
    }
    else
    {
      cout << i + 1 << ' ' << ans.at(i) << endl;
    }
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>

using namespace std;

const int MAX_N = 101;
const int DUMMY = 10000;

int N, u, k, v = 0;

vector<int>V [MAX_N];
int d [MAX_N];
bool visited [MAX_N];

bool bfs(int u, int depth)
{
    //if (V[u].size() == 0) return false;
    
    // for (int i = 0; i < V[u].size(); ++i)
    //     cout << V[u][i] << ' ';
    // cout << endl;
    
    for (int i = 0; i < V[u].size(); ++i)
        bfs(V[u][i] - 1, depth + 1);
    
    d[u] = min(d[u], depth);
    return true;
}

int main()
{
    //initialize
    for (int i = 0; i < MAX_N; ++i) {
        d[i]   = DUMMY;
        visited[i] = false;
    }
    
    //input
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      scanf("%d %d", &u, &k);
      for (int j = 0; j < k; ++j) {
          scanf("%d", &v);
          V[i].push_back(v);
      }
      sort(V[i].begin(), V[i].end());
    }

    //serach
    bfs(1-1, 0);
    
    //output
    for (int i = 0; i < N; ++i) {
      if(d[i] == DUMMY) cout << i+1 << ' ' << -1 << endl;
      else cout << i+1 << ' ' << d[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
  const char* dlm = "";
  for(const auto& x:v) cout << exchange(dlm, "\t") << x;
  return os;
}
struct Graph{
  // size_t time;
  vector<vector<size_t>> v;
  // vector<size_t> d,f;
  vector<int32_t> d;
  // Graph(size_t n): time(1), v(n), d(n,0), f(n) {}
  Graph(size_t n): v(n), d(n,-1) {}
  // void dfs(size_t i){
  //   d[i]=time++;
  //   for(const auto& x:v[i]) if(d[x-1]==0) dfs(x-1);
  //   f[i]=time++;
  // }
  void bfs(size_t n){
    d[n]=0;
    deque<size_t> que(1,n);
    while(!que.empty()){
      size_t x=que.front(); que.pop_front();
      for(const auto& y:v[x]){
        if(d[y-1]==-1){
          d[y-1]=d[x]+1;
          que.push_back(y-1);
        }
      }
    }
  }
};
ostream& operator<<(ostream& os, const Graph& x){
  for(size_t i=0; i<x.v.size(); ++i)
    // cout<<(i+1)<<' '<<x.d[i]<<' '<<x.f[i]<<'\n';
    cout<<(i+1)<<' '<<x.d[i]<<'\n';
  return os;
}
int32_t main(){
  size_t n; cin>>n;
  Graph graph(n);
  for(size_t i=0; i<n; ++i){
    size_t id,k; cin>>id>>k;
    graph.v[id-1].resize(k); for(auto&& x:graph.v[id-1]) cin>>x;
  }
  graph.bfs(0);
  cout<<graph;
}

C++: #include <iostream>
#include <cstdlib>
#include <string>
#include <typeinfo>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INFTY = (1<<21);
int n,M[N][N], 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;
            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++: //
//
//  algoC.cpp - created by us162022
//
#include <iostream>
#include <queue>
using namespace std;
struct Node{
  int u,k,v[99];
  int distance;
};

int main(int argc, char *argv[])
{
  int n;
  Node node[100];
  queue<Node> que;

  cin >> n;

  for(int i=0; i<n; i++){
    cin >> node[i].u >> node[i].k;

    for(int j=0; j<node[i].k; j++)
      cin >> node[i].v[j];

    node[i].distance=-1;
  }

  node[0].distance = 0;
  que.push(node[0]);

  while(!que.empty()){
    Node fro = que.front();

    for(int i=0; i<fro.k; i++){
      int x = fro.v[i]-1;
  
      if(node[x].distance==-1)
	    node[x].distance = fro.distance+1;

      que.push(node[x]);
    }

    que.pop();
  }

  for(int i=0; i<n; i++)
    cout << node[i].u << " " << node[i].distance << endl;

}

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

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int adj[105][105] = {0};
int res[105] = {0};
int n;

int main() {
    cin >> n;
    REP(i, n){
        int u, k;
        cin >> u >> k;
        REP(j, k) {
            int tmp;
            cin >> tmp;
            adj[u-1][tmp-1] = 1;
        }
        res[i] = INF;
    }
    res[0] = 0;
    REP(i, n) REP(j, n)
        if(adj[i][j] != 0)
            res[j] = min(res[j], res[i]+1);

    REP(i, n){
        if(res[i] != INF)
            cout << i+1 << " " << res[i] << endl;
        else
            cout << i+1 << " -1" << endl;
    }
    return 0;
}