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 <queue>

using namespace std;


static const int NO = -2;
static const int N = 100;


int graph[N + 1][N + 1] = {{}};
int dis[N + 1] = {};
int n;

void bfs(int v){
  queue<int> Q;

  Q.push(v);

  for(int i = 1; i <= N; i++){
    dis[i] = NO;
  }

  dis[v] = 1;

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

    for(int i = 1;i <= n; i++){
      if(graph[u][i]==0) continue;
      if(dis[i]!=NO)continue;
      dis[i] = dis[u] + 1;
      Q.push(i);

    }
  }
}

int main(){

  int r[N] = {};
  int d[N] = {};
  int k[N] = {};
  int f[N] = {};
  int p,s[N] = {};
  int v[N] = {};


  //in
  cin >> n;

  for(int i = 1;i <= n; i++){

    cin >> r[i] >> k[i];

    for(int j = 1;j <= k[i];j++){

      cin >> f[j];
      graph[i][f[j]] = 1;
    }
  }

  cin >> p;
  for(int i = 1;i <= p; i++){
    cin >> s[i] >> d[i] >> v[i];
    bfs(s[i]);

    if(v[i] >= dis[d[i]]) cout << dis[d[i]] << endl;
    else cout << "NA" << endl;
  }
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define SUB 103
int net[SUB][SUB],n,maxx=0;
typedef pair<int,int> BVB;
int ans(int,int,int);
int main(){
  int i,j,p,num,n1,th;
  cin >> n;
  for(i=0;i<n;i++){
    cin >> num >> n1;
    if(num >= maxx)maxx = num;
    for(j=0;j<n1;j++){
      cin >> th;
      net[num][th]=1;
    }
  }
  for(int m=0;m<maxx;m++){
    for(int n=0;n<maxx;n++){
      if(net[m][n]!=1)net[m][n]=0;
    }
  }
  int pkn;
  cin >> pkn;
  int st,ed,c,answer;
  for(i=0;i<pkn;i++){
    cin >> st >> ed >> c;
    answer = ans(st,ed,c);
    if(answer != -2)cout << answer << endl;
    else cout << "NA" << endl;
  }
}
int ans(int st,int ed,int c){
  queue<BVB> Nari;
  int i,j,k,l,cal=0,mem=0;
  BVB next,fr;
  fr.first=st;
  fr.second=c;
  Nari.push(fr);
  if(st == ed && c != 0)return 1;
  while(1){
    next=Nari.front();
    Nari.pop();
    next.second=next.second-1;
    if(fr.second == (next.second+1) && next.second == 0 && next.first != ed)return -2;
    if(next.second < 0)break; 
    for(i=1;i<=maxx;i++){
      if(net[next.first][i] == 1)mem++;  
      if(net[next.first][i] == 1 && i == ed ){
	return (fr.second - next.second)+1;
      }
      if(net[next.first][i] == 1)Nari.push(BVB(i,next.second));
      if(mem == n)break;
    }
  }
  return -2;  
}

C++: #include <bits/stdc++.h>
using namespace std;
constexpr int IINF = INT_MAX;

int main() {
    int n, d[105][105];
    while(cin >> n){
        for(int i=0;i<n;i++) fill(d[i], d[i]+n, IINF/3);
        for(int i=0;i<n;i++){
            int r, k;
            cin >> r >> k;
            r--;
            for(int j=0;j<k;j++){
                int t;
                cin >> t;
                t--;
                d[r][t] = 1;
            }
        }
        for(int k=0;k<n;k++){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }
        int p;
        cin >> p;
        for(int i=0;i<p;i++){
            int s, g, v;
            cin >> s >> g >> v;
            s--; g--;
            if(d[s][g] < v){
                cout << d[s][g]+1 << endl;
            }
            else{
                cout << "NA" << endl;
            }
        }
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_NODE = 110;

vector<int> makeInput(string input){
    vector<int> ret;
    stringstream ss(input);

    while(ss >>input) ret.push_back(atoi(input.c_str()));
    return ret;
}

int main(){
    int n;
    while(cin >>n && n){
        int d[MAX_NODE][MAX_NODE];
        REP(i, MAX_NODE) REP(j, MAX_NODE) d[i][j] = INF;
        REP(i, MAX_NODE) d[i][i] = 0;
        vector< vector<int> > graph(MAX_NODE, vector<int>(MAX_NODE));
        string dust_str;
        getline(cin, dust_str);
        //make graph
        REP(i, n){
            string input;
            getline(cin, input);
            vector<int> sum_list = makeInput(input);

            //make
            REP(j, sum_list[1]){
                graph[sum_list[0]].push_back(sum_list[j + 2]);
                d[sum_list[0]][sum_list[j + 2]] = 1;
            }
        }

        //ワーシャルフロイド
        int v = n + 1;
        REP(k, v){
            REP(i, v){
                REP(j, v){
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }

        //cal ans
        int m;
        cin >>m;
        getline(cin, dust_str);
        REP(i, m){
            string input;
            getline(cin, input);
            vector<int> sum_list = makeInput(input);
            int from = sum_list[0], to = sum_list[1], ttl = sum_list[2];
            if(ttl < (d[from][to] + 1)) cout <<"NA" <<endl;
            else cout <<d[from][to] + 1 <<endl;
        }
    }
    return 0;
}

C++: #include <iostream>
const int INF=500;
using namespace std;
int main(){
  int d[100][100],n,p;
  cin>>n;
  for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]=INF;
  for(int i=0,r,k,t;i<n&&cin>>r>>k;i++) for(int j=0;j<k&&cin>>t;j++) d[r-1][t-1]=1;
  for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
  for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]++;
  cin>>p;
  for(int i=0,s,t,v;i<p;i++){
    cin>>s>>t>>v;
    if(d[s-1][t-1]>v) cout<<"NA"<<endl;
    else cout<<d[s-1][t-1]<<endl;
  }
  return 0;
}