Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, &g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

func (q *Queue) isEmpty() bool {
	return q.head == nil
}

func fillDistancesByBreadthFirstSearch(g *DirectedGraph, startNode *DirectedGraphNode) {
	startNode.visited = true
	startNode.d = 0

	queue := Queue{}
	queue.enqueue(startNode)
	for !queue.isEmpty() {
		u := queue.dequeue().node
		for _, adjacent := range u.adjacents {
			if !adjacent.visited {
				adjacent.visited = true
				adjacent.d = u.d + 1
				adjacent.parent = u
				queue.enqueue(adjacent)
			}
		}
		u.visited = true
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	// fmt.Println(g)
	fillDistancesByBreadthFirstSearch(g, &g.nodes[1])
	// fmt.Println(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		distance := node.d
		if distance == infty {
			distance = -1
		}
		fmt.Println(node.key, distance)
	}
}

C++: //?????????P282-286 ????????§????????????

#include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int UNREACHABLE = -1;

int n, M[N][N];	//M[N][N]?????£??\??????
int d[N];	//???i????????¢???d[i]???????´???????

void bfs(int);

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;
		for(int j=0; j<k; j++){
			cin >> v;
			M[u-1][v-1] = 1;
		}
	}

	//?§???????0??¨?????????????????¢?´¢???????§?
	bfs(0);

	return 0;
}

void bfs(int s){
	queue<int> q;	//?¬?????¨???????????????????????¨??????????????????????????????\???
	q.push(s);

	for(int i=0; i<n; i++){		//???????????????????????¢?´¢??¶??????UNREACHABLE??????????????????
		d[i] = UNREACHABLE;
	}
	
	d[s] = 0;	//?§????????????¢???0???????????????

	int u;	//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] != UNREACHABLE) continue;	//??¢?´¢???????????????????????????
			d[v] = d[u] + 1;
			q.push(v);	//v????????¢?´¢?????£????????§???????????¢?´¢???????????¨??????v????????\??????????????????
		}
	}
	//???????????¨?§??????????????????¢?????¨???
	for(int i=0; i<n; i++){
		cout << i+1 << " " << d[i] << endl;
	}
}

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

int main(void) {
	int n, u, k, i, j, x, t, v;
	bool path[110][110];
	int d[110];
	memset(path, 0, sizeof(path));
	memset(d, -1, sizeof(d));
	cin >> n;
	for (i = 0;i<n;i++) {
		cin >> u >> k;
		u--;
		for (j = 0;j<k;j++) {
			cin >> x;
			x--;
			path[u][x] = true;
		}
	}

	queue<pair<int, int> > q;
	q.push({ 0,0 });
	while (!q.empty()) {
		v = q.front().first;
		t = q.front().second;
		q.pop();
		if (d[v] == -1)d[v] = t;
		else continue;

		t++;
		for (i = 0;i<n;i++) {
			if (path[v][i] && d[i] == -1) {
				q.push({ i,t });
			}
		}
	}

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

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int matrix[110][110];
int dist[110];
int n;
queue<int> Q;

int main() {
  REP(x, 110) {
    REP(y, 110) {
      matrix[x][y] = 0;
    }
    dist[x] = (1 << 15);
  }

  // int n;
  cin >> n;

  REP(_, n) {
    int v, num;
    cin >> v >> num;

    REP(i, num) {
      int cv;
      cin >> cv;
      matrix[v][cv] = 1;
    }
  }

  dist[1] = 0;
  Q.push(1);

  while (!Q.empty()) {
    int f = Q.front(); Q.pop();

    int updated = 0;
    FOR(v, 1, n + 1) {
      if (matrix[f][v]) {
        Q.push(v);
        if (dist[v] > dist[f] + 1) {
          dist[v] = dist[f] + 1;
          updated = 1;
        }
      }
    }
    if (!updated) break;
  }


  FOR(v, 1, n + 1) {
    if (dist[v] == (1 << 15)) {
      cout << v << " -1" << endl;
    } else {
      cout << v << " " << dist[v] << endl;
    }
  }


  return 0;
}

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

using namespace std;

int n,u,k,v,x;
int a[101][101]={};
int d[101]={0};
queue<int> q;

int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> u >> k;
        u-=1;
        for(int j=0;j<k;j++)
        {
            cin >> v;
            v-=1;
            a[u][v]=1;
        }
    }
    q.push(0);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        for(int i=0;i<n;i++)
        {
            if(a[x][i]==0) continue;
            if(d[i]!=0) continue;
            d[i]=d[x]+1;
            q.push(i);
        }
    }
    cout << 1 << " " << 0 << endl;
    for(int i=1;i<n;i++)
    {
        cout << i+1 << " ";
        if(d[i]==0) cout << -1;
        else cout << d[i];
        cout << endl;
    }
    return 0;
}

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



int main(){
  const int MX=1e9+7;
  int a[1000][1000];
  fill(&a[0][0],&a[999][999],MX);
  for(int i=0;i<1000;i++){
    a[i][i]=0;
  }
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    int w;
    cin>>w;
    int m;
    cin>>m;
    for(int t=0;t<m;t++){
      int e;
      cin>>e;
      a[w][e]=1;
    }
  }
  for(int i=1;i<=n;i++){
    for(int t=1;t<=n;t++){
      for(int k=1;k<=n;k++){
	a[t][k]=min(a[t][k],a[t][i]+a[i][k]);
      }
    }
  }
  for(int i=1;i<=n;i++){
    cout<<i<<" "<<(a[1][i]==MX? -1:a[1][i])<<endl;
  }


  
  return 0;
}