Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

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

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
	int degree,number,num,count;
	cin>>degree;
	int arr[100][100];
	for(int i=0;i<degree;i++)
	{
		for(int j=0;j<degree;j++)
		{
			arr[i][j]=0;
		}
	}
	for(int i=0;i<degree;i++)
	{
		cin>>count;
		cin>>number;
		for(int j=0;j<number;j++)
		{
			cin>>num;
			arr[i][num-1]=1;
		}
	}
	int queue[100];
	int L=0,R=0;
	int ans[degree];
	bool flag=false;
	for(int i=0;i<degree;i++)
	{
		ans[i]=0;
	}
	R+=1;
	while(L!=R)
	{
		for(int i=0;i<degree;i++)
		{
			if(arr[queue[L]][i]!=0 && ans[i]==0)
			{
				queue[R++]=i;
				ans[i]=ans[queue[L]]+1;
			}
		}
		L++;
	}
	for(int i=0;i<degree;i++)
	{
		cout<<i+1<<" ";
		if(ans[i]==0 && i!=0)
		{
			cout<<"-1"<<endl;
		}
		else if(i!=0)
		{
			cout<<ans[i]<<endl;
		}
		if(i==0)
			cout<<0<<endl;
	}
}

C++: #include<iostream>
#include<queue>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
using namespace std;
int G[100][100];
queue<int> q;

class ANS
{
public:
	int id, d;
	bool done;
};

void bfs(ANS a[])
{
	q.push(a[0].id);
	a[q.front() - 1].done = true;
	a[q.front() - 1].d++;
	while (q.empty() != true)
	{
		int Q = q.front();
		FOR(i, 1, 100)
		{
			if (G[Q-1][i-1] == 1 && a[i-1].done == false)
			{
				a[i-1].done = true;
				a[i-1].d = a[Q-1].d + 1;
				q.push(i);
			}
		}
		q.pop();
	}
}

int main()
{
	ANS ans[100];
	int n;
	cin >> n;
	FOR(i, 0, n)
	{
		ans[i] = { i + 1,-1,false };
		int u, m;
		cin >> u >> m;
		FOR(j, 0, m)
		{
			int l;
			cin >> l;
			G[u - 1][l - 1] = 1;
		}
	}
	bfs(ans);
	FOR(i, 0, n)
	{
		cout << ans[i].id << " " << ans[i].d << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;

void bfs(int start, int n, vector<vector<int>> &a) {
	int INF = 10000000;
	vector<int> d(n, INF);
	queue<int> q;
	d[start] = 0;
	q.push(start);
	while (q.size() != 0) {
		int u = q.front();
		q.pop();
		for (int i = 0; i < n; i++) {
			if (a[u][i] == 0)
				continue;
			if (d[i] != INF)
				continue;
			d[i] = d[u] + 1;
			q.push(i);
		}
	}
	for (int i = 0; i < n; i++) {
		if (d[i] == INF) {
			cout << i + 1 << " -1" << endl;
		}
		else {
			cout << i + 1 << " " << d[i] << endl;
		}
	}
}
int main()
{
	int n;
	vector<vector<int>> a;
	cin >> n;
	a.resize(n);
	for (int i = 0; i < n; i++) {
		a[i].resize(n);
	}
	for (int i = 0; i < n; i++) {
		int temp, k;
		cin >> temp >> k;
		for (int j = 0; j < k; j++) {
			int temp2;
			cin >> temp2;
			a[temp - 1][temp2 - 1] = 1;
		}
	}

	bfs(0, n, a);

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <cstring>

#define LP(i,n) for(int i=0;i<n;i++)
#define Mset(x,v) memset(x,v,sizeof(x))

using namespace std;
using LL=long long;
const int INF=100000000;

int A[110][110],d[100];

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

C++: #include<iostream>
#include<queue>
#include<algorithm>
#define INF 999999999
using namespace std;

int res[101];
int G[101][101] = {0};

int main() {
    int n, u, v, k;
    std::queue<int> Q;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> u >> k;
        for (int j = 0; j < k; j++) {
            std::cin >> v;
            G[u - 1][v - 1] = 1;
        }
    }

    for (int i = 0; i < n; i++) res[i] = INF;

    Q.push(0);
    res[0] = 0;

    while (! Q.empty()) {
        u = Q.front();
        for (int i = 0; i < n; i++) {
            if (G[u][i]) {
                Q.push(i);
                res[i] = std::min(res[i], res[u] + 1);
            }
        }
        Q.pop();
    }

    for (int i = 0; i < n; i++) {
        if (res[i] != INF) std::cout << i + 1 << " " << res[i] << "\n";
        else std::cout << i + 1 << " -1\n";
    }

    return 0;
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define INF 1000000

int main()
{
  int n;cin>>n;
  int i,j;
  vector<vector<int>> g(n);
  rep(i,n)
  {
    int u,k;cin>>u>>k;
    u--;
    rep(j,k)
    {
      int vj;cin>>vj;
      vj--;
      g[u].emplace_back(vj);
    }
  }
  queue<int> q;
  queue<int> q2;
  q.push(0);
  vector<int> ans(n,INF);
  ans[0]=0;
  while(!q.empty())
  {
    while(!q.empty())
    {
      int now=q.front();
      q.pop();
      rep(i,g[now].size())
      {
        if(ans[g[now][i]]==INF)
        {
          q2.push(g[now][i]);
          ans[g[now][i]]=ans[now]+1;
        }
      }
    }
    while(!q2.empty())
    {
      q.push(q2.front());
      q2.pop();
    }
  }
  rep(i,n)
  {
    cout<<i+1<<' ';
    if(ans[i]==INF)cout<<-1<<endl;
    else cout<<ans[i]<<endl;
  }
}

C++: //単一始点最短経路
#include<iostream>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

void dijkstra(){
    int minv;
    int d[MAX],color[MAX];

    for(int i=0; i<n; i++){
        d[i]=INFTY;
        color[i]=WHITE;
    }

    d[0]=0;
    color[0]=GRAY;

    while(1){
        minv=INFTY;
        int u=-1;
        for(int i=0; i<n; i++){
            if(minv>d[i] && color[i]!=BLACK){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1){ break; }
        color[u]=BLACK;
        for(int v=0; v<n; v++){
            if(color[v]!=BLACK &&  M[u][v]!=INFTY){
                if(d[v]>d[u]+M[u][v]){
                    d[v]=d[u]+M[u][v];
                    color[v]=GRAY;
                }
            }
        }
    }
    for(int i=0; i<n; i++){
        cout<<i<<" "<<(d[i]==INFTY ? -1 : d[i])<<endl;
    }
}

int main(){
    cin>>n;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            M[i][j]=INFTY;
        }         
    }

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

    dijkstra();
    return 0;
}