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 <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int MAX_N = 100;

int main() {
    vector<int> G[MAX_N];
    int N; cin >> N;
    for(int i = 0; i < N; i++) {
        int u, k; cin >> u >> k; u--;
        while(k--) {
            int v; cin >> v; v--;
            G[u].push_back(v);
        }
    }

    vector<int> dist(N, -1);
    queue<int> Q;
    Q.push(0);
    dist[0] = 0;

    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(int i = 0; i < (int)G[u].size(); i++) {
            int v = G[u][i];
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            Q.push(v);
        }
    }
    for(int i = 0; i < N; i++) cout << i + 1 << ' ' << dist[i] << endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
static const int MAX=101;

vector<int> Adjacency[MAX];
int dt[MAX];
bool Visited[MAX];

void bfs(int i)
{
    queue<int> Q;
    dt[i]=0;Q.push(i);
    Visited[i]=true;
    int d=0;
    while(!Q.empty())
    {
        int p=Q.front();Q.pop();d=dt[p]+1; //important
        for(int j=0;j<Adjacency[p].size();j++)
        {
            if(Visited[Adjacency[p][j]]==true) continue;
            //else
            Q.push(Adjacency[p][j]); Visited[Adjacency[p][j]]=true;
            dt[Adjacency[p][j]]=d;
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) dt[i]=-1;
    for(int i=1;i<=n;i++)
    {
        int j,num;
        cin>>j>>num;
        for(int k=0;k<num;k++)
        {
            int t;cin>>t;
            Adjacency[j].push_back(t);
        }
    }
    bfs(1);
    for(int i=1;i<=n;i++)
    {
        cout<<i<<' '<<dt[i]<<endl;
    }
}

C++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

#define MAX 100
//#define MAX_ 1000
struct Graph
{
	int id, k;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
//int cou = 0;
void Breadth_cou(Graph *g,int cou)
{
	for (auto i = g->node.begin(); i != g->node.end(); ++i) {
		if (G[*i].d == -1)
			G[*i].d = cou;	
	}
	g->k = 0;
}
void Breadth(Graph *g)
{
	if (g->k > 0) {
		Breadth_cou(g, g->d + 1);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			Breadth(&G[*i]);
		}
	}
}

int main(void)
{
	int n, u, xk, v;
	scanf_s("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf_s("%d %d", &u, &xk);
		G[u - 1].id = u - 1;
		G[u - 1].k = xk;
		for (int j = 0; j < xk; ++j) {
			scanf_s("%d", &v);
			G[u - 1].node.push_back(v - 1);
		}
	}
	G[0].d = 0;
	Breadth(&G[0]);
	for (int i = 0; i < n; ++i) {
		printf("%d %d\n", G[i].id + 1, G[i].d);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAXN = 100+7;
int m[MAXN][MAXN];
queue<int> q;
int d[MAXN];
bool v[MAXN];
int n;
void bfs(int start) {
    d[start] = 0;
    v[start] = true;
    q.push(start);
    int u = 1;
    while(!q.empty()) {
        int x = q.front();
        int i;
        for(i = 1; i <= n; i++) {
            if(m[x][i]==1 && v[i] == false) {
                v[i] = true;
                d[i] = d[x]+1;
                q.push(i);
            }
        }
        q.pop();
    }
}
int main() {
    scanf("%d", &n);
    memset(m, 0, sizeof(m));
    memset(d, -1, sizeof(d));
    memset(v, false, sizeof(v));

    int i;
    for(i = 0; i < n; i++) {
        int num;
        int k;
        scanf("%d", &num);
        scanf("%d", &k);
        int j;
        for(j = 0; j < k; j++) {
            int id;
            scanf("%d", &id);
            m[num][id] = 1;
        }
    }
    bfs(1);
    for(i = 1; i <= n; i++){
        printf("%d %d\n", i, d[i]);
    }
    return 0;
}

C++: #include<iostream>
#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] ={false};

 



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];
    }
  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<k[i]){


    }
    cout << endl;
  }
*/
  Queue que = Queue();

  que.push(1);

  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]);
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }


  }


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


  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
using namespace std;


vector<int> bfs(vector<vector<int> > &graph,int start){
	queue<int> todo;
	vector<int> dist(graph.size(),-1);
	dist[start] = 0;
	todo.push(start);
	while(!todo.empty()){
		int now = todo.front();
		todo.pop();
		for (int i=0;i<graph[now].size();i++){
			int nxt = graph[now][i];
			if (dist[nxt] != -1) continue;
			dist[nxt] = dist[now] + 1;
			todo.push(nxt);
		}
	}
	return dist;
}



int main(){
	int n; cin >> n;
	vector<vector<int> > graph(n+1);
	for (int i = 0; i < n; i++){
		int v, k; cin >> v >> k;
		for (int j = 0; j < k; j++) {
			int in; cin >> in;
			graph[v].push_back(in);
		}
	}
	vector<int> dist;
	dist = bfs(graph,1);


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

	return 0;
}

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

using namespace std;

long long inf=1000000007;

int main(){
	long long n;
	cin>>n;
	vector<vector<long long>> kankei(n+1);
	vector<long long> dis(n+1,-1);

	for(long long i=0;i<n;i++){
		long long u,k;
		cin>>u>>k;
		for(long long j=0;j<k;j++){
			long long hoge;
			cin>>hoge;
			kankei.at(u).push_back(hoge);
		}
	}

	queue<long long> que;
	que.push(1);
	dis.at(1)=0;

	while(!que.empty()){
		long long now=que.front();
		que.pop();

		for(auto a:kankei.at(now)){
			if(dis.at(a)!=-1) continue;
			que.push(a);
			dis.at(a)=dis.at(now)+1;
		}
	}

	for(long long i=1;i<=n;i++){
		cout<<i<<" "<<dis.at(i)<<endl;
	}

	return 0;
}