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<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int G[101][101];
int d[101];

void explore(int id, int n,int cnt)
{
	if (cnt == n)
	{
		return;
	}
	if (d[id] == -1)
	{
		d[id] = cnt;
	}
	else
	{
		d[id] = min(d[id], cnt);
	}

	for (int i = 1; i <= n; i++)
	{
		if (G[id][i] == 1)
		{
			explore(i, n, cnt + 1);
		}
	}
}

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

	for (int i = 0; i < n; i++)
	{
		int id, k;
		cin >> id >> k;

		for (int i = 0; i < k; i++)
		{
			int v;
			cin >> v;
			G[id][v] = 1;
		}
	}

	for (int i = 1; i <= 100; i++)
	{
		d[i] = -1;
	}

	explore(1, n, 0);

	for (int i = 1; i <= n; i++)
	{
		printf("%d %d\n", i, d[i]);
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define All(v) (v).begin(),(v).end()
int dy[8] = {-1,0,1,0,-1,1,1,-1};
int dx[8] = {0,1,0,-1,1,1,-1,-1};
const int mod = 1000000007;
const int inf = mod*mod;
const int d5 = 100100;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    vector<int> edge[110];
    for(int i=0;i<n;i++){
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++){
            int x;
            cin>>x;
            edge[u].push_back(x);
        }
    }
    int d[110];
    fill(d,d+110,-1);
    d[1]=0;
    queue<int> Q;
    Q.push(1);
    while(!Q.empty()){
        int now=Q.front();Q.pop();
        for(auto it:edge[now]){
            if(d[it]!=-1)continue;
            d[it]=d[now]+1;
            Q.push(it);
        }
    }
    for(int i=1;i<=n;i++){
        cout<<i<<' '<<d[i]<<endl;
    }
}

C++: /**
 * Bredth first search.
 * graphs could be circular.
 * there could be a better answer later.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N] = {0};
int s[N] = {0};

void bfs(int n, int dd) {
	int k = adj[n][0];
	int kk = 0;
	for (int j=1;j<=k;j++) {
		int nn = adj[n][j];
		if (d[nn] != -1 && d[nn] <= dd) continue; // this condition is important.
		d[nn]=dd;
		s[kk++]=nn;
	}

	for (int j=0;j<kk;j++)
	{
		bfs(s[j], dd+1);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		d[j]=-1;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
	}

	d[1]=0;

	bfs(1, 1);

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

	return 0;
}

C++: 
#include<iostream>
#include<queue>
using namespace std;
#define NIL -999
bool mark[105][105];
int n;

int d[105] = { 0 };

inline int next(int i) {
	for (int tmp = 1; tmp <= n; ++tmp) {
		if (mark[i][tmp] && d[tmp]==-1) {
			//cout << endl << i << "的下一个节点是：" << tmp << endl;
			return tmp;
		}
	}
	//cout << endl << i << "的下一个节点是：" << NIL << endl;
	return NIL;
}
void bfs_IN(int i) {
	int tm = 0;
	queue<int> q;
	int now, nt;
	int status = 1;;
	q.push(i);
	d[i] = tm;
	//cout << 3 << endl;
	++tm;
	while (!q.empty()) {
		now = q.front();
		nt = next(now);
		while (nt != NIL) {
			//status = true;
			q.push(nt);
			if (-1==d[nt])d[nt] = tm;
			nt = next(now);
		//	cout << 4 << endl;
		}
		q.pop();
		--status;
		if (!status){
			status = q.size();
			++tm;
		}
		//status = false;
	}
}
void bfs() {
	int k, Node, tmp;

	for (int i = 1; i <= n; ++i)
		for (int j = 0; j <= n; ++j)
			mark[i][j] = false;

	cin >> n;
	for (int i = 0; i <= n; ++i)
		d[i] = -1;
	for (int i = 1; i <= n; ++i) {
		cin >> Node >> k;
		for (int j = 1; j <= k; ++j) {
			cin >> tmp;
			mark[Node][tmp] = true;
		}
	}

	bfs_IN(1);

	//cout << 1 << endl;
	for (int p = 1; p <= n; ++p) {
		cout << p << ' ' << d[p] << endl;
	}

}



int main() {

	bfs();
	return 0;
}

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

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

int n,M[N][N];
int d[N];
void bfs(int s){
  int i,u,v;
  queue<int> q;
  q.push(s);
  for(i=0;i<n;i++)d[i]=INF;
  d[s]=0;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(v=0;v<n;v++){
      if(d[v]!=INF)continue;
      if(d[v]!=INF)continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(i=0;i<n;i++){
    cout << i+1 << " " << ((d[i]==INF) ? (-1) : d[i])<<endl;
  }
}
int main(){
  int u,k,v;
  int i,j;
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++)M[i][j]=0;
  }
  for(i=0;i<n;i++){
    cin >> u >> k;
    u--;
    for(j=0;j<k;j++){
      cin >> v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);

  return 0;
}

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

#define VISITED 1

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> D(n, 0);
  vector<vector<int> > graph(n);
  vector<int> dist(n, -1);
  queue<int> q;
  for(int i = 0; i < n; i++) {
    int u, k, v_i;
    cin >> u >> k;
    for(int j = 0; j < k; j++) {
      cin >> v_i;
      graph[u-1].push_back(v_i-1);
    }
  }
  q.push(0);
  D[0] = VISITED;
  dist[0] = 0;
  while(!q.empty()) {
    int id = q.front();
    q.pop();
    for(int i = 0; i < graph[id].size(); i++) {
      if(D[graph[id][i]] != VISITED) {
	dist[graph[id][i]] = dist[id] + 1;
	q.push(graph[id][i]);
	D[graph[id][i]] = VISITED;
      }
    }
  }

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

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;

class Queue{
private:
  int inf[MAX];
  int head,tail;
 
public:
 
  Queue(){
    syokika();
  }
 
  void syokika(){
    head = 0;
    tail = 0;
  }
 
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
 
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
 
  int front(){
    return inf[head];
  }
 
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
 
  int size(){
    return tail - head;
  }
 
};

int imap[200][200];
int k[200] = {0}; 
int ans[200] = {0};
bool used[200];

int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }

   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j; 
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }

  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<k[u[i]];j++){
      cout << imap[u[i]][j] << " ";
    }
    cout << endl;
  }
  */

  Queue que = Queue();

  que.push(u[0]);

  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
	que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;

 
  

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

  return 0;
}