Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)
const INF = 1<<31

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	arr := make([][]int, n)
	for i := range arr {
		arr[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			arr[i][j] = INF
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		x, k := input[0]-1, input[1]
		for j := 0; j < k; j++ {
			y := input[j+2]-1
			arr[x][y] = 1
		}
	}
	for k := 0; k < n; k++ {
		arr[k][k] = 0
		for i := 0; i < n; i++ {
			if arr[i][k] >= INF {
				continue
			}
			for j := 0; j < n; j++ {
				if arr[k][j] >= INF {
					continue
				}
				arr[i][j] = min(arr[i][j], arr[i][k]+arr[k][j])
			}
		}
	}
	scanner.Scan()
	p, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < p; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		s, d, v := input[0]-1, input[1]-1, input[2]
		if arr[s][d] < v {
			fmt.Println(arr[s][d]+1)
		} else {
			fmt.Println("NA")
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <iostream>
#include <set>
#include <string.h>
#include <vector>
using namespace std;

const int SIZE = 101;

int solve( int table[ SIZE ][ SIZE ], int s, int e, int initTtl, int numRooter )
{
	if( initTtl < 2 ){
		return 0;
	}
	bool bPast[ SIZE ];
	memset( bPast, 0, sizeof( bPast ) );
	bPast[ s ] = true;
	vector< int > pList;
	for( int i = 1; i <= numRooter; ++i ){
		if( table[ s ][ i ] ){
			if( i == e ){
				return 2;
			}
			bPast[ i ] = true;
			pList.push_back( i );
		}
	}

	int ttl = initTtl;
	while( pList.size() > 0 ){
		--ttl;
		if( ttl < 1 ){
			return 0;
		}
		vector< int > pList2;
		for( int i = 0; i < pList.size(); ++i ){
			int p = pList[ i ];
			if( p == e ){
				return initTtl - ttl + 1;
			}
			for( int j = 1; j <= numRooter; ++j ){
				if( table[ p ][ j ] && !bPast[ j ] ){
					bPast[ j ] = true;
					pList2.push_back( j );
				}
			}
		}
		pList = pList2;
	}

	return 0;
}

int main()
{
	int table[ SIZE ][ SIZE ];

	int n;
	while( cin >> n ){
		memset( table, 0, sizeof(table) );
		for( int i = 0; i < n; ++i ){
			int id, numRooter;
			cin >> id >> numRooter;
			for( int j = 0; j < numRooter; ++j ){
				int to;
				cin >> to;
				table[ id ][ to ] = true;
			}
		}
		int p;
		cin >> p;
		for( int i = 0; i < p; ++i ){
			int from, to, ttl;
			cin >> from >> to >> ttl;
			int result = solve( table, from, to, ttl, n );
			if( result > 0 ){
				cout << result << endl;
			}else{
				cout << "NA" << endl;
			}
		}
	}

	return 0;
}

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

using namespace std;

int solve( list<int> *router, int from, int to, int ttl ){
	set<int> history;
	queue<int> q;
	int dist[101];

	for( int i = 0;i < 101;i++ ) dist[i] = 9999999;

	history.insert( from );
	q.push( from );
	dist[from] = 1;

	while( !q.empty() ){
		int u = q.front();

		if( dist[u] > ttl ){
			q.pop();
			continue;
		}

		if( u == to ){
			return dist[u];
		}

		for( list<int>::iterator it = router[u].begin(); it != router[u].end(); it++ ){
			int v = *it;
			if( history.count( v ) ) continue;
			history.insert( v );
			dist[v] = dist[u] + 1;
			q.push( v );
		}
		q.pop();
	}
	return -1;
}



int main(void)
{
	int n;
	list<int> router[101];

	cin >> n;

	for( int i = 0;i < n;i++ ){
		int hoge;
		int number;
		int piyo;

		cin >> number >> hoge;

		for( int j = 0;j < hoge;j++ ){
			cin >> piyo;
			router[number].push_back( piyo );
		}
	}

	cin >> n;
	int from,to,ttl;
	for( int i = 0;i < n;i++ ){
		cin >> from >> to >> ttl;
		int answer = solve( router, from, to, ttl );
		if( answer == -1 ) cout << "NA" << endl;
		else cout << answer << endl;
	}
	return 0;
}

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

using namespace std;

const int INF = 100000000;

int main()
{
    int n, p;
    cin >> n;
    int w[n+1][n+1];
    for(int i = 1; i <= n; i++) 
        for(int j = 1; j <= n; j++) 
            w[i][j] = (i==j) ? 0 : INF;

    int r,k,t;
    for(int i = 1; i <= n; i++) {
        cin >> r >> k;
        for(int j = 0; j < k; j++) {
            cin >> t;
            w[r][t] = 1;
        }
    }

    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
            }
        }
    }
    
    cin >> p;
    int s,d,v;
    for(int i = 0; i < p; i++) {
        cin >> s >> d >> v;
        if(w[s][d] + 1 <= v) {
            cout << w[s][d] + 1 << endl;
        } else {
            cout << "NA" << endl;
        }
    }
}

C++: #include <iostream>

#define INF 1e+9
#define MAX_N 101

using namespace std;
int main(void){
	int n;cin>>n;
	int g[MAX_N][MAX_N];
	for(int i=0;i<MAX_N;++i)for(int j=0;j<MAX_N;++j) g[i][j]=INF;
	for(int i=0,r,k;i<n;++i){
		cin>>r>>k;
		for(int j=0,t;j<k;++j){
			cin>>t;
			g[r][t]=1;
		}
	}
	int d[MAX_N][MAX_N];
	for(int i=0;i<MAX_N;++i)for(int j=0;j<MAX_N;++j)d[i][j]=(i==j)?1:INF;
	for(int i=1;i<=n;++i){
		bool f=false;
		for(;;){
			for(int j=1;j<=n;++j){//from
				for(int k=1;k<=n;++k){//to
					if(d[i][j]!=INF && d[i][j] + g[j][k] < d[i][k]){
						d[i][k] = d[i][j] + g[j][k];
						f=true;
					}
				}
			}
			if(f==false) break;
			f=false;
		}
	}
	int p;cin>>p;
	for(int i=0,from,to,v;i<p;++i){
		cin>>from>>to>>v;
		if(d[from][to]>v) cout<<"NA"<<endl;
		else cout<<d[from][to]<<endl;
	}
	return 0;
}

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

int router[200][200];
int b[200][200];

int main(void){
  int N;
  cin >> N;
  for(int n=0; n < N; n++){
    int r, k;
    cin >> r >> k;
    for(int i=0; i < k; i++){
      int tmp; cin >> tmp;
      b[r][tmp] = 1;
    }
  }


  int pac_n;
  cin >> pac_n;
  for(int p=0; p < pac_n; p++){
    int start, des, ttl; cin >> start >> des >> ttl;
    
    queue<pair<int, int> > que;   //<id, now_ttl>
    que.push(make_pair(start, ttl));
    bool flg = false;
    for(int x = 0; x < N+1; x++){
      for(int y = 0; y < N+1; y++){
        router[x][y] = b[x][y];
      }
    }
    while(!que.empty()){
      pair<int, int> q = que.front(); que.pop();
      if(q.first == des){
        cout << ttl - q.second + 1<< endl;
        flg = true;
        break;
      }
      if(q.second != 1){
        for(int i=0; i <= N; i++){
          if(router[q.first][i] == 1){
            router[q.first][i] = 0;
            que.push(make_pair(i, q.second - 1));
          }
        }
      }
    }
    if(!flg)
      cout << "NA" << endl;
  }
  return 0;
}