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

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

	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;

int main(void)
{
     //??£??\??????????????£??\???????????????
    int n, k;

    cin >> n;
    vector<vector<bool> > A(n, vector<bool>(n, false));
    for(int i = 0; i < n; i++){
        cin >> k >> k;
        for(int j = 0; j < k; j++){
            int input;
            cin >> input;
            A[i][input-1] = true;
        }
    }

    vector<int> u(n, 1000000);
    u[0] = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(u[j] == i){
                for(int k = 0; k < n; k++){
                    if(A[j][k] == 1) u[k] = min(i+1, u[k]);
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout << i << " " << u[i] << endl;
    }
    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
int a[101][101],num[101],dis[101];
bool vis[101];
int n,u,k,x,t=1;
queue<int> q;
int main(){
	memset(a,0,sizeof(a));
	memset(vis,false,sizeof(vis));
	memset(dis,0x1f1f1f1f,sizeof(dis));
	cin>>n;
	for (int i=0; i<n; i++){
		cin>>u>>k;
		num[u]=k;
		int j=0;
		while (k--){
			  cin>>x;
			  a[u][++j]=x;
		}
	}
	q.push(1);
	dis[1]=0;
	while (!q.empty()){
		  x=q.front(); q.pop(); vis[x]=true;
		  for (int i=1; i<=num[x]; i++){
		  	  if (!vis[a[x][i]]){
		  	     dis[a[x][i]]=min(dis[a[x][i]],dis[x]+1);
		  	     q.push(a[x][i]);
              }
		  }
	}
    for (int i=1; i<=n; i++){
    	cout<<i<<' ';
    	if (dis[i]==0x1f1f1f1f) dis[i]=-1;
		cout<<dis[i]<<endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

bool visited[111];
int d[111];
vector<vector<int> > g(111);
typedef pair<int, int> P;

int main(){
  int n; cin >> n;
  FOR(i,0,n){
    int u, k; cin >> u >> k;
    FOR(j,0,k){
      int v;
      cin >> v;
      g[u].push_back(v);
    }
  }
  FOR(i,0,n+1) visited[i] = false;
  FOR(i,0,n+1) d[i] = -1;
  queue<P> que;
  que.push(P(1,0));
  visited[1] = true;
  while(!que.empty()){
    P p = que.front();que.pop();
    int u = p.first;
    d[u] = p.second;
    for(auto &v : g[u]){
      if(!visited[v]){
        que.push(P(v, p.second + 1));
        visited[v] = true;
      }
    }
  }
  FOR(i,1,n+1){
    cout << i << " " << d[i] << endl;
  }
  return 0;
}

C++: #if 0
	??????????????????????????????
#endif
#include <iostream>
#include <queue>

using namespace std;

int main(){
	int n, u, k, v;
	int D[100];
	bool adj[100][100] = {};
	int pushed[100] = {};
	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> u >> k;
		for (int j = 1; j <= k; j++){
			cin >> v;
			adj[u][v] = true;
		}
	}
	for (int i = 1; i <= n; i++){
		D[i] = -1;
	}
	queue<int> Q;
	Q.push(1);
	D[1] = 0;
	pushed[1] = 1;
	while(!Q.empty()){
		int m = Q.front();
		Q.pop();
		for (int i = 1; i <= n; i++){
			if (adj[m][i] == true && pushed[i] == 0){
				D[i] = D[m]+1;
				Q.push(i);
				pushed[i] = 1;
			}
		}
	}
	
	for (int i = 1; i < n+1; i++){
		cout << i << ' ' << D[i] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
const int INF = 2e9;
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

int main() {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i, 0, n) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, 0, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }

    int dist[n];
    rep(i, 0, n) dist[i] = -1;
    dist[0] = 0;
    queue<int> q;
    q.push(0);

    while (!q.empty()) {
        int x = q.front();
        q.pop();
        rep(i, 0, (int)g[x].size()) {
            int nx = g[x][i];
            if (dist[nx] != -1) continue;
            dist[nx] = dist[x] + 1;
            q.push(nx);
        }
    }

    rep(i, 0, n) {
        cout << i + 1 << " " << dist[i] << endl;
    }

    return 0;
}

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

int main() {
	int n, u, k, v;
	cin >> n;
	vector<vector<int>> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> u >> k;
		for (int j = 0; j < k; ++j) {
			cin >> v;
			a[u - 1].push_back(v - 1);
		}
	}
	queue<int> b;
	b.push(0);
	vector<int> ans(n, -1);
	ans[0] = 0;
	while (!b.empty()) {
		for (int& i : a[b.front()]) {
			if (-1 == ans[i]) {
				ans[i] = 1 + ans[b.front()];
				b.push(i);
			}
		}
		b.pop();
	}
	for (int i = 0; i < n; ++i) cout << 1 + i << ' ' << ans[i] << endl;
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
const ll INF = 1LL<<60;
const int INT_INF = 1e9;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;

enum State {
    unseen, // 未訪問
    seen,   // 発見済み
    end     // 探索終了
};

vector<vector<int>> adj; // 隣接行列
int t = 0;  // 大域時刻
int d[110]; // 各頂点の頂点1からの最短距離
State s[110];


int main() {
    int n; cin >> n;
    adj.resize(n);
    rep(i, n) {
        int u, k; cin >> u >> k;
        --u;
        rep(j, k) {
            int v; cin >> v;
            --v;
            adj[i].emplace_back(v);
        }
    }

    // 初期化
    rep(i, n) {
        d[i] = INT_INF;
        s[i] = State::unseen;
    }
    d[0] = 0;
    queue<int> q;
    q.push(0);

    // BFS
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto &v: adj[u]) {
            if (d[v] == INT_INF) {
                q.push(v);
                d[v] = d[u] + 1;
            }
        }
    }

    // 出力
    rep(i, n) {
        int dist = (d[i] != INT_INF) ? d[i] : -1;
        cout << i+1 << ' ' << dist << endl;
    }
}