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<iostream>
#include<queue>

using namespeace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

coid dfs(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 =.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++: #include <iostream>
#include <vector>
#include <queue>

int main()
{
	int n;
	std::cin >> n;
	std::vector<std::vector<int> > E(n); 
	for (int i = 0; i < n; i++)
	{
		int u, k;
		std::vector<int> G;
		std::cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++)
		{
			int g;
			std::cin >> g;
			g--;
			G.push_back(g);
		}
		E.at(u) = G;
	}
	std::vector<int> Dist(n, -1);
	Dist.at(0) = 0;
	std::queue<int> Que;
	Que.push(0);
	while (!(Que.empty()))
	{
		int s = Que.front();
		Que.pop();
		for (int i = 0; i < E.at(s).size(); i++)
		{
			int next = E.at(s).at(i);
			if (Dist.at(next) == -1)
			{
				Dist.at(next) = Dist.at(s) + 1;
				Que.push(next);
			}
		}
	}
	for (int i = 0; i < n; i++)
		std::cout << i + 1 << ' ' << Dist.at(i) << '\n';
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdlib>
int INF = 1e9;
int main(){
    long long n;
    std::cin >> n;
    std::vector<int> G[n];
    std::vector<int> used(n, 0);
    for(int i = 0; i < n; i++){
        int a, k;
        std::cin >> a >> k;
        a--;
        for(int j = 0; j < k; j++){
            int b;
            std::cin >> b;
            b--;
            G[a].push_back(b);
        }
    }
    std::vector<int> d(n, INF);
    std::queue<int> q;
    q.push(0);
    d[0] =0;
    while(!q.empty()){
        int t = q.front();
        q.pop();
        used[t] = 1;
        for(int i: G[t]){
            if(used[i] == 0){
                q.push(i);
                d[i] = std::min(d[i], d[t] + 1);
            }
        }
    }
    for(int i = 0; i < n; i++) if(d[i] == INF) d[i] = -1;
    for(int i = 0; i < n; i++) std::cout << i + 1 << ' ' << d[i] <<  std::endl;
}

C++: #include<iostream>
using namespace std;
int n, u,k[101], v[101][101], out[101];

void hitung(int a, int b){
	int i;
	out[a]=b;
	for(i=1; i<=k[a]; i++){
		if(out[v[a][i]]==-1 || out[v[a][i]]>b+1){
			hitung(v[a][i],	b+1);
		}
	}
}

int main(){
	int i,j;
    cin>>n;
    for(i=1; i<=n; i++){
        cin>>u>>k[i]; //getchar();
		out[i]=-1;
        for(j=1; j<=k[i]; j++){
            cin>>v[i][j];// getchar();
        }
    }
    
    hitung(1,0);
	for(i=1;i<=n;i++){
		cout<<i<<" "<<out[i]<<endl;
	}


    //getchar();
    return 0;
}

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

const int maxn=(1<<21);
int G[110][110];///构造图
int d[110];///该顶点到顶点1的距离
bool vis[110];
int n;

void BFS(){
    queue<int> q;
    q.push(1);
    d[1]=0;
    vis[1]=true;
    while(!q.empty()){
        int top=q.front();
        q.pop();
        //vis[top]=true;
        for(int i=1;i<=n;i++){
            if(G[top][i]&&vis[i]==false){
       //     if(G[top][i]&&d[i]==maxn){
                d[i]=d[top]+1;
                vis[i]=true;
                q.push(i);
            }
        }
    }
}


int main(){
    scanf("%d",&n);
    memset(G,0,sizeof(G));
    memset(vis,0,sizeof(vis));
    fill(d,d+110,maxn);

  //  memset(d,maxn,sizeof(d));
    for(int i=0;i<n;i++){
        int u,k;
        scanf("%d%d",&u,&k);
        for(int j=0;j<k;j++){
            int v;
            scanf("%d",&v);
            G[u][v]=1;
        }
    }

    BFS();
    for(int i=1;i<=n;i++){
        printf("%d %d\n",i,(d[i]==maxn)?(-1):d[i]);
    }
    return 0;
}

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(){
	scanf("%d", &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 <cmath>
#include <queue>
#include <iostream>
using namespace std;

int G[110][110] = {0};
int n;
int D[110] = {0};
void bfs(int src){
 queue<int> Q;
 Q.push(src);
 D[src] = 0;
 while(! Q.empty()){
  int cur = Q.front();
  Q.pop();
  for(int i=1; i<=n; i++){
   if(G[cur][i]==1 && D[i]==0){
    D[i] = D[cur]+1;
    Q.push(i);
   }
  }
 }

 for(int i=1; i<=n; i++){
  if(i!=1 && D[i]==0){
   D[i]=-1;
  }
  cout << i << " " << D[i] << endl;
 }
}

int main(){
 cin >> n;
 int u, k;
 int v[100000];
 for(int i=0; i<n; i++){
  cin >> u >> k;
  for(int j=0; j<k; j++){
   cin >> v[j];
   G[i+1][v[j]] = 1;
  }
 }
 bfs(1);
}

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

static const int N = 100;
static const int no = -1;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {{}}, d[N] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for(int i = 1; i <= n; i++) d[i] = no;
  d[v] = 0;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for(int i = 1; i <= n; i++) {
      if((graph[u][i] == 0) || (d[i] != no)) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
	ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

	return 0;
}