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

C++: #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph=vector<vector<int>>;
typedef long long ll;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int main(){
    int n;cin>>n;
    Graph G(n+2);
    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[u].push_back(v);
        }
    }
    bool ans_check=false;
    vector<int> b(n+2,1000000);
    queue<int> q;
    b[1]=0;
    q.push(1);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int A=0;A<G[x].size();A++){
            int check_x=G[x][A];
            if(b[check_x]==1000000){
                q.push(check_x);
                b[check_x]=b[x]+1;
            }
        }
    }
    for(int i=1;i<n+1;i++){
        if(b[i]==1000000) b[i]=-1;
        cout<<i<<' '<<b[i]<<endl;
    }
}