Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

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

func (q *Queue) pop() Pair {
	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([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	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 <stdio.h>
#include <queue>
using namespace std;
struct bfs{
    int n;
    int l;
};
int main(){
    int n,a,m,temp,ans[101];
    bool graph[101][101];
    bfs t,b;
    queue<bfs> q;
    scanf("%d",&n);
    for(int i=0;i<101;i++){
        for(int j=0;j<101;j++){
            graph[i][j]=false;
        }
        ans[i]=-1;
    }
    for(int i=0;i<n;i++){
        scanf("%d%d",&a,&m);
        for(int j=0;j<m;j++){
            scanf("%d",&temp);
            graph[a][temp]=true;
        }
    }
    t.n=1;
    t.l=0;
    q.push(t);
    while(!q.empty()){
        t=q.front();
        q.pop();
        if(ans[t.n]==-1)ans[t.n]=t.l;
        else continue;
        t.l++;
        b=t;
        for(int i=1;i<=n;i++){
            if(graph[t.n][i]){
                b.n=i;
                q.push(b);
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d %d\n",i,ans[i]);
    }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <string.h>
#include <limits.h>
using namespace std;
vector<int> v[101];
int d[101];
queue<int> q;
bool flag[101];
int main(){
	int n, u, k; scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		scanf("%d %d", &u, &k);
		for (int j = 0; j < k; ++j){
			int tmp; scanf("%d", &tmp);
			v[u].push_back(tmp);
		}
	}
	memset(d, -1, sizeof(d));
	d[1] = 0;
	q.push(1); flag[1] = true;
	while (!q.empty()){
		int m = q.front(); q.pop();
		for (int i = 0; i < v[m].size(); ++i){
			int tmp = v[m][i];
			if (!flag[tmp]){
				flag[tmp] = true;
				d[tmp]=d[m]+1;
				q.push(tmp);
			}
		}
	}
	for (int i = 1; i <= n; ++i){
		cout << i << ' ' << d[i] << '\n';
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const int MAX = 105;

vector<int> to[MAX];
bool seen[MAX];
int d[MAX];

void bfs(int u) {
  queue<int> q;
  seen[u] = true;
  d[u] = 0;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int v: to[u]) {
      if (seen[v]) continue;
      d[v] = d[u] + 1;
      seen[v] = true;
      q.push(v);
    }
  }
}

int main() {
  int n, u, k, v;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      to[u-1].push_back(v-1);
    }
  }

  rep(i, n) {
    seen[i] = false;
    d[i] = INF;
  }

  bfs(0);

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



  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 100;
struct Graph{
    struct Edge{
        int to, w;
        Edge(int v, int _w):to(v), w(_w){}
    };
    vector<Edge> edge;
    vector<vector<int> > vertex;
    int V, E;
    Graph(int _V):V(_V),E(0){
        edge.clear(); vertex.clear();
        vertex.resize(V);
    }
    void addEdge(int u, int v, int w){
        vertex[u].push_back(E++);
        edge.push_back(Edge(v, w));
    }
};
void SPFA(Graph& G, LL* d, int s){
    bool inq[MAXN]; int cnt[MAXN];
    memset(cnt, 0, sizeof(int)*G.V);
    memset(d, INF, sizeof(LL)*G.V);
    memset(inq, false, sizeof(bool)*G.V);
    d[s] = 0; inq[s] = true;
    queue<int> q; q.push(s);
    while (q.size()){
        int p = q.front(); q.pop(); inq[p] = false;
        cnt[p]++; if (cnt[p] > G.V){ d[s] = -1; break;}
        for (auto index : G.vertex[p]){
            auto &e = G.edge[index];
            if (d[p] + e.w < d[e.to]){
                d[e.to] = d[p] + e.w;
                if (!inq[e.to]) inq[e.to] = true, q.push(e.to);
            }
        }
    }
}
int main(){
    int n;
    cin >> n;
    Graph G(n);
    LL d[MAXN];
    for (int i = 0 ; i < n ; i++){
        int m;
        cin >> m >> m;
        while (m--){
            int v;
            cin >> v;
            G.addEdge(i, v-1, 1);
        }
    }
    SPFA(G, d, 0);
    for (int i = 0 ; i < n ; i++)
        cout << i+1 << ' ' << d[i] << '\n';
}

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

using namespace std;
int V;
vector<int> G[102];
bool vis[102];
int d[102];
const int inf=0x3f3f3f;

void bfs(int s=1){
    d[s]=0;
    queue<int> que;
    que.push(s);
    while(!que.empty()){
        int t=que.front();
        que.pop();
        vis[t]=true;
        for(int i=0;i<G[t].size();i++){
            if(!vis[G[t][i]]){
                que.push(G[t][i]);
                d[G[t][i]]=min(d[G[t][i]],d[t]+1);
            }
        }
    }
}


int main(){
    cin>>V;
	int i;
	for(int i=1;i<=V;i++){
        d[i]=inf;
        vis[i]=false;
	}
    for(i=1;i<=V;i++){
        int id,k;
        cin>>id>>k;
        while(k--){
            int out;
            cin>>out;
            G[id].push_back(out);
        }
        sort(G[id].begin(),G[id].end());
    }
    bfs();
    for(int i=1;i<=V;i++){
        cout<<i<<" "<<(d[i]==inf?-1:d[i])<<endl;
    }
    return 0;
}

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

int main() {
  ll n;
  cin >> n;
  vector<set<ll>> v(n);
  for(ll i=0;i<n;i++) {
    ll f,k;
    cin >> f >> k;
    for(ll j=0;j<k;j++) {
      ll temp;
      cin >> temp;
      temp--;
      v[i].insert(temp);
    }
  }
  vector<ll> ans(n,-1);
  ans[0]=0;
  queue<ll> q;
  q.push(0);
  while(q.size()) {
    ll p=q.front();
    q.pop();
    for(ll t:v[p]) {
      if(ans[t]<0) {
        ans[t]=ans[p]+1;
        q.push(t);
      }
    }
  }
  for(ll i=0;i<n;i++) {
    cout << i+1 << " " << ans[i] << endl;
  }
}

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

using namespace std;

int main(){
    int n;
    cin >> n;

    vector<vector<int>> graph(n);

    for(int i = 0; i < n; i++ ){
        int v, k;
        cin >> v >> k;
        for(int j = 0; j < k; j++){
            int v2;
            cin >> v2;
            graph[v-1].push_back(v2-1);
        }
    }

    vector<int> d(n, -1);
    queue<int> q;
    q.push(0);
    d[0] = 0;

    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int i = 0; i < graph[cur].size(); i++ ){
            int next = graph[cur][i];
            if(d[next] == -1){
                d[next] = d[cur]+1;
                q.push(next);
            }
        }
    }

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

    return 0;
}