Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

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

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

using namespace std;
int graph[125][125];
int d[125];
void bfs(int s, int n){
	queue<int> Q;
	int color[125];	
	for(int i=1;i<=n;i++){
		color[i] =0;
		d[i] = 2e9;
	}
	color[s] = 1;
	d[s] = 0;
	Q.push(s);
	while(Q.size()!=0){
		int u = Q.front();
		Q.pop();
		for(int v=1;v<=n;v++){
			if(graph[u][v] == 0) continue;
			if(color[v] == 0){
				color[v] = 1;
				d[v] = d[u] +1;
				Q.push(v);
			}
		}
		color[u] = 2;
	}
}

int main(){
	int n;
	cin >> n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			graph[i][j] = 0;
		}
	}
	for(int i=1;i<=n;i++){
		int num;
		int k;
		int tmp;
		cin >> num >> k;
		if(k !=0){
			for(int j=0;j<k;j++){
				cin >> tmp;
				graph[num][tmp] = 1;
			}
		}
	}
	bfs(1, n);
	for(int i=1;i<=n;i++){
		cout << i << " "<<((d[i] == 2e9)? (-1) :d[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[*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 n;
int main(void)
{
	int u, xk, v;//, n;
	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 <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

#define NODE_NUM 100

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int id);
	int dist;
	int check[NODE_NUM];
};

Node::Node()
{
	next_num = 0;
	dist = -1;
	for (int i = 1; i <= NODE_NUM; i++) {
		check[i] = 0;
	}
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node Node_list[NODE_NUM + 1];

bool Node::Search(int id)
{
	for (int k = 0; k < next_num; k++) {
		if (Node_list[next_node_list[k]].dist == -1) {
			Node_list[next_node_list[k]].dist = dist + 1;
		}
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == id) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		if (Node_list[next_node_list[j]].check[id] == 0) {
			Node_list[next_node_list[j]].check[id] = 1;
			que.push(next_node_list[j]);
		}
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u;
		cin >> k;
		Node_list[u] = Node();
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			Node_list[u].AddNextNode(v);
		}
	}
	
	//dist
	Node_list[1].dist = 0;
	for (int i = 2; i <= n; i++) {
		bool ret = Node_list[1].Search(i);
		while (!ret && !que.empty()) {
			ret = Node_list[que.front()].Search(i);
			que.pop();
		};
		while (!que.empty()) {
			que.pop();
		}
	}

	//output
	for (int i = 1; i <= n; i++) {
		cout << i << " " << Node_list[i].dist << "\n";
	}
	
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <memory>
#include <string>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define MAX 105

struct Vtx{
    int id;
    int path;
    Vtx(){path=id=-1;}
    bool operator <(Vtx &b){
        return id<b.id;
    }
};

Vtx V[MAX];

set<int> adj[MAX];

int n,u,k,v;

void bfs(){
     queue<int> q;
     q.push(1);
     V[1].path=0;
     set<int> e;
     set<int>::iterator it;
     while(!q.empty()){
        e = adj[q.front()];
        it = e.begin();
        while(it!=e.end()){
            if(V[*it].path==-1){
                V[*it].path = V[q.front()].path+1;
                q.push(*it);
            }
            it++;
        }
        q.pop();
     }

}

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


void solve(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&u,&k);
        V[u].id=i;
        for(int j=0;j<k;j++){
            scanf("%d",&v);
            adj[u].insert(v);
        }
    }
    bfs();
    print();
}

int main(){

   // freopen("in","r",stdin);
    solve();
    return 0;
}

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  int m[n];
  rep(i,n){
    m[i]=10000;
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
        d[i][j]=10000;
      }
    }
  }
  m[0]=0;

  rep(i,n){
    int u,k;
    cin >> u >> k;
    if(k>0){
      rep(j,k){
        int v;
        cin >> v;
        if(d[v-1][u-1]!=1){
          d[u-1][v-1]=1;
        }
      }
    }
  }   
  q1.push(0);
  rep(i,n){
    int u = q1.front();
    rep(j,n){
      if(d[u][j]==1){
        q1.push(j);
        m[j] = min(m[j],m[u]+1);
      }
    }
    q1.pop();
  }

  rep(i,n){
    cout << i+1 << ' ';
    if(m[i]==10000){
      cout << -1 << endl;
    }else{
      cout << m[i] << endl;
    }
  }

  return 0;
}

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

struct Node {
	int ID;
	int d;
	bool visited;
	bool operator<(const Node& rhs) {return this->ID < rhs.ID;}
	bool operator>(const Node& rhs) {return this->ID > rhs.ID;}
	bool operator<=(const Node& rhs) {return this->ID <= rhs.ID;}
	bool operator>=(const Node& rhs) {return this->ID >= rhs.ID;}
	bool operator==(const Node& rhs) {return this->ID == rhs.ID;}
	bool operator!=(const Node& rhs) {return this->ID != rhs.ID;}
};

void BFS(std::vector<int>* adjlist, Node* nodes, std::queue<int> que)
{	
	nodes[que.front()].d = 0;
	
	while (!que.empty()) {
		int id = que.front(); que.pop();
		for (std::vector<int>::iterator it = adjlist[id].begin(); it != adjlist[id].end(); ++it) {
			if (!nodes[*it].visited) { 
				que.push(*it);
				nodes[*it].d = nodes[id].d + 1;
				nodes[*it].visited = true;
			}
		}
	}
}

int main()
{
	int n; std::cin >> n;
	
	std::vector<int>* adjlist = new std::vector<int>[n];
	Node* nodes = new Node[n];
	
	std::queue<int> que;
	
	for (int i=0; i<n; i++) {
		nodes[i].visited = false; nodes[i].d = -1;
	}
	
	for (int i=0; i<n; i++) {
		int x;
		int deg;
		std::cin >> x >> deg;
		nodes[i].ID = x-1;
		for (int j=0; j<deg; j++) {
			int y;
			std::cin >> y;
			adjlist[x-1].push_back(y-1);
		}
	}
	
	std::sort(nodes, nodes + n);
	

	que.push(0); nodes[0].visited = true;
	BFS(adjlist, nodes, que);
	
	
	
	for (int i=0; i<n; i++) {
		std::cout << nodes[i].ID + 1 << " " << nodes[i].d << std::endl;
	}
	
	delete[] adjlist;
	delete[] nodes;
	
	return 0;
}