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++: //59
#include<iostream>
#include<vector>
#include<queue>
#include<utility>

using namespace std;

int main(){
  int n;
  cin>>n;
  vector<int> e[101];
  for(int i=0;i<n;i++){
    int r,k;
    cin>>r>>k;
    while(k--){
      int t;
      cin>>t;
      e[r].push_back(t);
    }
  }
  int p;
  cin>>p;
  while(p--){
    int s,d,v;
    cin>>s>>d>>v;
    queue<pair<int,int> > que;
    que.push(make_pair(1,s));
    while(!que.empty()){
      int c=que.front().second;
      if(c==d||que.front().first>v)break;
      for(int i=0;i<e[c].size();i++){
	que.push(make_pair(que.front().first+1,e[c][i]));
      }
      que.pop();
    }
    if(que.empty()||que.front().first>v){
      cout<<"NA"<<endl;
    }else{
      cout<<que.front().first<<endl;
    }
  }
  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;



int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit,memo[101][101],P[101],D[101],num,min,min_index,current;

	for(int i=1; i < 101; i++){
		for(int k=1; k < 101; k++){
			table[i][k] = 0;
			memo[i][k] = 0;
		}
	}

	scanf("%d",&n);

	for(int i = 0; i < n; i++){
		scanf("%d %d",&r,&k);
		for(int p=0;p < k; p++){
			scanf("%d",&to);
			table[r][to] = 1;
		}
	}

	scanf("%d",&num_of_packet);


	for(int i=0; i < num_of_packet; i++){
		scanf("%d %d %d",&start,&goal,&t_limit);

		if(t_limit <= 1){
			printf("NA\n");
			continue;
		}

		if(memo[start][goal] != 0){
			if(memo[start][goal] <=  t_limit){
				printf("%d\n",memo[start][goal]);
			}else{
				printf("NA\n");
			}
			continue;
		}

		for(int index=1; index <= n; index++){
			P[index] = 0; //???????????????????????¨???????????????
			D[index] = 1000000000; //start??°?????????????????¢?????¨???????????????
		}

		P[start] = 1; //??????????????°????????????????????¨??????
		num = 1;

		for(int index = 1; index <= n; index++){
			if(index != start && table[start][index] == 1){
				D[index] = 1;  //start??°???????????????????????????start??°??????????????¢1??¨??????
			}
		}

		while(num < n){
			min = 1000000000;
			for(int index=1;index <= n; index++){  //??????????????°?????????????????????????????¢???
				if(P[index] == 0 && D[index] < min){ //?????????????????§?????????????????????????????????
					min = D[index];
					min_index =index;
				}
			}

			if(min == 1000000000)break; //?????????????????????????????????????????°break

			P[min_index] = 1; //min_index?????????????????¨??????
			current = min_index;
			num++; //???????????????????????°???1?¢???????

			for(int i=1; i <= n; i++){
				if(table[current][i] == 1){	//start???????????????????????¨???????????£????????????
					D[i] = (D[i] <= (D[current]+1))?D[i]:(D[current]+1); //?????????????????´??\????????¢??¨???current????????±???????????¢???????????????D??¨??????
				}
			}

		}

		if(D[goal] >= t_limit){
			printf("NA\n");
		}else{
			memo[start][goal] = D[goal]+1;
			printf("%d\n",D[goal]+1);
		}

	}

    return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
typedef pair<int,int> P;
const int INF =10000000;
int n;
int d[101][101];
int main()
{
  int r,k;
  while(cin>>n){
    for(int i = 0;i < 101;i++){
      for(int j = 0;j <101;j++){
	d[i][j]= INF;
	if(i==j)
	  d[i][j] = 0;
      }
    }
    for(int i = 1;i <=n;i++){
      cin>>r;
      cin>>k;
      for(int j = 0;j < k;j++){
	int x;
	cin>>x;
	d[r][x] = 1;
      }
    }
    for(int i = 1;i <=n;i++){
      for(int j = 1;j <=n;j++){
	for(int k = 1;k <=n;k++){
	  d[i][j] =min(d[i][j],d[i][k]+d[k][j]);
	}
      }
    }
    int p;
    cin>>p;
    int s1,d1,v1;
    for(int i = 0;i <p;i++){
      cin>>s1>>d1>>v1;
      if(d[s1][d1]+1<=v1){
	cout <<d[s1][d1]+1<<endl;
      }else{
	cout <<"NA"<<endl;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
const int INF = 10000000;
int wf[101][101];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			wf[i][j] = i == j ? 0 : INF;
		}
	}
	for(int i = 0; i < n; i++) {
		int a, t;
		cin >> a >> t;
		while(t--) {
			int b;
			cin >> b;
			wf[a][b] = 1;
		}
	}

	for(int k = 1; k <= n; k++)
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);

	int q;
	cin >> q;
	while(q--) {
		int s, d, v;
		cin >> s >> d >> v;
		if(wf[s][d] >= v) {
			cout << "NA" << endl;
		}
		else {
			cout << wf[s][d] + 1 << endl;
		}
	}
}

C++: #include<iostream>
#include<stack>
#include<vector>
#include <map>
#include <queue>
#include<string.h>
using namespace std;
typedef pair<int,int> P;
vector<int> node[200];

int que(int s,int d,int v)
{
  queue<P> qu;
  int c[200];
  memset(c,-1,sizeof(c));
  for(int i=0;i<node[s].size();i++)
    {
      if(v-1>0)
	qu.push(P(node[s][i],v-1));      
    }
  while(!qu.empty())
    {
      P a = qu.front();
      qu.pop();
      //      cout<<a.first<<" "<<a.second<<endl;
      if(a.first==d)
	return v-a.second+1;
      for(int i=0;i<node[a.first].size();i++)
	{
	  if(a.second-1>0)
	    {
	      if(c[node[a.first][i]]==-1)
		{
		  c[node[a.first][i]]=a.second-1;
		  qu.push(P(node[a.first][i],a.second-1));	    
		}
	    }
	} 
    }
      return -1;
}
int main()
{
  int n;
  cin>>n;
  for(;n--;)
    {
      int k,t;
      cin>>k>>t;
      for(int i=0;i<t;i++)
	{
	  int tmp;
	  cin>>tmp;
	  node[k].push_back(tmp);
	}
    }
  int p;
  cin>>p;
  for(;p--;)
    {
      int s,d,v;
      cin>>s>>d>>v;
      int ans=que(s,d,v);
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
      
    }
  
}