Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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;
            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<stdio.h>
 
 
int G[101][101], q1[101], q2[101], n;
 
void f(int a, int b){
  int i;
  q1[a] = b;
  for(i = 1; i <= B[a]; i++){
    if(b + 1 < q1[G[a][i]]) f(G[a][i], b + 1);
  }
}
 
int main(){
  int i, j, m;
 
  scanf("%d", &n);
 
  for(i = 1; i <= n; i++){
    q1[i] = 100000000;
    scanf("%d", &m);
    scanf("%d", &q2[m]);
 
    for(j = 1; j <= q2[m]; j++) scanf("%d", &G[m][j]);
  }
 
  f(1, 0);
 
  for(i = 1; i <= n; i++){
    if(q1[i] == 100000000) q1[i] = -1;
  }
 
  for(i = 1; i <= n; i++) printf("%d %d\n", i, q1[i]);
 
  return 0;
}

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

int G[110][110];
int N,num,lines,tar;
int D[1000];
int ret[1000];

void bfs(int src);

int main() {
	for (int i=0;i<110;i++) {
		for(int j=0;j<110;j++) {
			G[i][j] = 0;
		}
	}
	for(int i=0;i<1000;i++) {
		D[i] = 100;
	}
	cin >> N;
	for(int i=0;i<N;i++) {
		cin >> num >> lines;
		for(int j=0;j<lines;j++) {
			cin >> tar;
			G[num][tar] = 1;
		}
	}
	/*
	for(int i=0;i<num+1;i++) {
		for(int j=0;j<num+1;j++) {
			cout << G[i][j] << " ";
		}
		cout << endl;
	}
	*/
	bfs(1);
	for(int i=1;i<num+1;i++) {
		cout << i << " " << ret[i] << endl;
	}

}
void bfs(int src) {
	int cur,dst;
	//cerr << "bfs root = " << src << endl;
	queue<int> Q; // ??´??°????????????????????\????????????
	Q.push(src);
	D[src] = 0; // ?????????
	while (! Q.empty()) {
		int cur = Q.front(); // ??????????´????????????????
		Q.pop();
// ???????¢??????¨??¨???
		ret[cur] = D[cur];
		//cerr << cur << " " << D[cur] << endl;
		for (int dst=1;dst<N+1;dst++) { // ???????????? dst ????????????
			if ((G[cur][dst] == 1) && (D[dst] == 100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
				D[dst] = D[cur]+1; //
				Q.push(dst); // dst ????¨???????????????????
			}
		}
	}
}

C++: #include <iostream>
using namespace std;
#define N 100
#define WHITE 0
#define BLACK 1

int M[N+1][N+1];
int n, dist;
int DIST[N+1];
int color[N+1];


//キューを使うプログラムを考える
void bfs(int r) {
    int u;
    int next[N+1], next_num = 0;
    bool added = false;
    //現時点での距離を入れておく
    for(int i=1; i<=n; i++) {
        if(M[r][i]==1 && color[i]==WHITE) {
            if(!added) {
                dist++;
                added = true;
            }
            color[i] = BLACK;
            DIST[i] = dist;
            next[next_num] = i;
            next_num++;
        }
    }
    
    for(int i=0; i<next_num; i++) {
        bfs(next[i]);
    }
}   

int main() {
    int u, k, v;

    cin >>n;
    for(int i=0; i<=n; i++) {
        DIST[i] = -1;
        color[i] = WHITE;
        for(int j=0; j<=n; j++) {
            M[i][j] = 0;
        }
    }

    for(int i=0; i<n; i++) {
        cin >> u >> k;
        for(int j=0; j<k; j++) {
            cin >> v;
            M[u][v] = 1;
        }
    }

    dist = 0;
    DIST[1] = 0;
    bfs(1);

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

    return 0;
}

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

struct node {
	std::vector<int> child;
	int distance;
};

int main() {
	int node_num;

	std::cin >> node_num;

	std::vector<node> n(node_num);

	for (int i = 0; i < node_num; i++) {
		int num, to, node_val;
		std::cin >> node_val >> num;
		for (int j = 0; j < num; j++) {
			std::cin >> to;
			n[node_val - 1].child.push_back(to);
		}
		n[i].distance = -1;
	}

	//???????????????????????¢0
	n[0].distance = 0;
	std::queue<int> q;
	int dist = 1;
	q.push(1);
	while(q.size() != 0){
		int num = q.front();
		q.pop();
		for (std::vector<int>::iterator it = n[num - 1].child.begin(); it != n[num - 1].child.end(); it++) {
			if (n[*it - 1].distance > n[num - 1].distance || n[*it - 1].distance == -1) {
				n[*it - 1].distance = n[num - 1].distance;
				q.push(*it);
			}
		}
	}

	for (int i = 0; i < node_num; i++) {
		std::cout << i + 1 << " " << n[i].distance << std::endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>

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



using namespace std;




int main() {
	
	int n;
	cin>>n;
	vector<vector<int>>t(n,vector<int>(0));
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		for(int j=0;j<b;j++){
			int x;
			cin>>x;x--;t[i].push_back(x);
		}
	}
	queue<int>r;
	r.push(0);
	int g[105]={1000000};
	for(int i=0;i<n;i++){
		g[i]=10000000;
	}
	g[0]=0;
	while(r.size()){
		int v=r.front();
		for(int i=0;i<t[v].size();i++){
			r.push(t[v][i]);
			g[t[v][i]]=min(g[t[v][i]],g[v]+1);
		}
		r.pop();
		

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

	
	return 0;
}

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

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

int M[N][N] = {0}, d[N], n;

void bfs() {
  int u, v;
  queue<int> q;
  for (u = 0; u < n; u++) {
    d[u] = INFTY;
  }
  d[0] = 0;
  q.push(0);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (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 (u = 0; u < n; u++) {
    cout << u + 1 << ' ' << ((d[u] == INFTY) ? (-1) : d[u]) << endl;
  }
}

int main() {
  int u, v, k, i, j;

  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs();

  return 0;
}

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

typedef struct{
  int A[1000];
  int ans;
}node;
node T[1000];
int Q[1000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    for(int i=1;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	}
	Q[tail++]=i;
      }
    }
    num=Q[head++];
  }
  
}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }

  function(1);
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

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


  return 0;
}