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;

int main(){

  int n;
  cin >> n;
  
  int data[n+1][n+1];
  
  for(int i = 0; i <= n; i++){
    for(int j = 0; j <= n; j++){
      data[i][j] = 0;
    }
  }
  
  int from, to, conesum;
  
  for(int i = 0; i < n; i++){
    cin >> from >> conesum;
    for(int j = 0; j < conesum; j++){
      cin >> to;
      data[from][to] = 1;
    }
  }
  //graph end  
  


  int sumpake;
  cin >> sumpake;
  
    queue<int> Q;

    int ttl, myposi, min, sumin, sumina, finish = 0;

    int come[n+1];

    for(int i = 0; i < sumpake; i++){
    for(int i = 0; i < n+1; i++) come[i] = 0;
      min = 0;
      cin >> from >> to >> ttl;
      myposi = from;
      come[myposi] = 1;
      for(int j = 0; j < ttl; j++){
	if(j != 0){
	  for(int l = 0; l < sumina; l++){
	    if(Q.front() == to){
	      min = j;
	      finish = 1;
	      break;
	    }
	    myposi = Q.front();
	    Q.pop();
	    if(come[myposi] == 0){
	      for(int k = 1; k <= n; k++){
		if(data[myposi][k] == 1) {
		  Q.push(k);
		  sumin++;
		}
	      }
	    }
	    come[myposi] = 1;
	  }
	  if(finish == 1) break;
	}else{
	  sumin = 0;
	  for(int k = 1; k <= n; k++){
	    if(data[myposi][k] == 1) {
	      Q.push(k);
	      sumin++;
	    }
	  }
	  
	}
	  sumina = sumin;
	  sumin = 0;
      }
      
      if(min == 0) cout << "NA";
      else cout << min+1;
      cout << endl;
      finish = 0;
      while(!Q.empty()) Q.pop();
    }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <climits>
#define REP(i,n) for(int i=0;i<n;++i)

//ダイグスとらで解くそうです

using namespace std;

typedef pair<int,int> DisNum;

vector<int> canto;

int Serch(vector<DisNum> canConnect[102],int from,int to,int hp)
{
	int distance[102] = {};//距離を保存
	REP(i,102) distance[i] = INT_MAX;
	priority_queue<DisNum,vector<DisNum>,greater<DisNum> > qu;//行き先のストックをキュー
	qu.push(DisNum(0,from));
	distance[from] = 0;
	while(!qu.empty())
	{
		DisNum nowfrom;
		nowfrom = qu.top();qu.pop();
		if(distance[nowfrom.second] != nowfrom.first) continue;
		if(nowfrom.second == to) return nowfrom.first;
		for(int i=0;i<canConnect[nowfrom.second].size();++i)
		{
			int v = canConnect[nowfrom.second][i].second;//今から調べる先の座標
			int n = nowfrom.second;
			if(distance[v] > distance[n] + canConnect[nowfrom.second][i].first)
			{
				distance[v] = distance[n] + canConnect[nowfrom.second][i].first;
				qu.push(DisNum(distance[v],v));
			}
		}
	}
}

int main(void)
{
	int maxn;
	canto.clear();
	vector<DisNum> canConnect[102];
	cin >> maxn;
	for(int i=0;i<maxn;++i)
	{
		int r,k;
		cin >> r >> k;
		for(int j=0;j<k;++j)
		{
			int t;
			cin >> t;
			canConnect[r].push_back(DisNum(1,t));
		}
	}
	int l;
	cin >> l;
	for(int j=0;j<l;++j)
	{
		int s,d,v;
		cin >> s >> d >> v;
		int ans = Serch(canConnect,s,d,v) + 1;
		if(ans <= v)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

const int MAX_N = 101;
const int INF = 1e+8;
typedef pair<int,int> P;

vector<int> G[MAX_N];

void init(){
	for(int i=0 ; i < MAX_N ; i++ ) G[i].clear();
}

void add_edge(int u, int v ){
	G[u].push_back( v );
}

int solve(int s, int g, int t){
	bool memo[MAX_N];
	for(int i=0 ; i < MAX_N ; i++ ) memo[i] = false;
	queue<P> q;
	q.push( P(0,s) );
	
	while( !q.empty() ){
		int cnt = q.front().first;
		int v = q.front().second;
		q.pop();
		memo[v] = true;
		
		if( v == g ){
			return cnt+1;
		}
		
		for(int i=0 ; i < G[v].size() ; i++ ){
			int to = G[v][i];
			if( cnt+1 < t && !memo[to] ){
				q.push( P(cnt+1,to) );
			}
		}
	}
	return INF;
}

int main(){
	int n;
	while( cin >> n ){
		init();
		
		for(int i=0 ; i < n ; i++ ){
			int u, k;
			cin >> u >> k;
			for(int j=0 ; j < k ; j++ ){
				int v;
				cin >> v;
				add_edge( u , v );
			}
		}
		int p;
		cin >> p;
		for(int j=0 ; j < p ; j++ ){
			int s, g, t;
			cin >> s >> g >> t;
			int ans = solve( s , g , t );
			if( ans == INF )
				cout << "NA" << endl;
			else
				cout << ans << endl;
		}
	}
}

C++: #include<cstdio>
#include<cstring>
#include<utility>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int>P;
vector<int>R[105];

int f(int s,int t,int v)
{
	int flg[105][105];
	memset(flg,0x7f,sizeof(flg));
	priority_queue<P>Q;
	Q.push(P(1,s));
	flg[s][s]=0;
	for(;!Q.empty();)
	{
		P now=Q.top();Q.pop();
		if(now.second==t)return now.first;
		now.first++;
		if(now.first>v)continue;
		int n=now.second;
		for(int i=0;i<R[n].size();i++)
		{
			if(flg[s][R[n][i]]>now.first)
			{
				flg[s][R[n][i]]=now.first;
				Q.push(P(now.first,R[n][i]));
			}
		}
	}
	return -1;
}

int main()
{
	int N,M,s,t,v,r;
	scanf("%d",&N);
	for(;N--;)
	{
		scanf("%d%d",&s,&t);
		for(;t--;)
		{
			scanf("%d",&v);
			R[s].push_back(v);
		}
	}
	scanf("%d",&M);
	for(;M--;)
	{
		scanf("%d%d%d",&s,&t,&v);
		r=f(s,t,v);
		if(r!=-1)printf("%d\n",r);
		else puts("NA");
	}
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<utility>
using namespace std;

constexpr int INF = (1 << 30) - 1;

int main(){
    int n;
    while(cin >> n){
        vector< vector<int> > g(n);
        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;
                g[r].push_back(t - 1);
            }
        }

        vector< vector<int> > cost(n, vector<int>(n, INF));
        for(int i = 0 ; i < n ; ++i){
            cost[i][i] = 1;
            queue< pair<int, int> > que;
            que.push({1, i});
            while(!que.empty()){
                pair<int, int> now = que.front();
                que.pop();
                if(cost[i][now.second] < now.first){
                    continue;
                }
                for(auto to : g[now.second]){
                    if(cost[i][to] > now.first + 1){
                        cost[i][to] = now.first + 1;
                        que.push({now.first + 1, to});
                    }
                }
            }
        }

        int p;
        cin >> p;

        while(p--){
            int s, d, v;
            cin >> s >> d >> v;
            --s;
            --d;

            if(cost[s][d] <= v){
                cout << cost[s][d] << endl;
            }else{
                cout << "NA" << endl;
            }
        }
    }

    return 0;
}