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++: // 2011/10/02 Tazoe

#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;

	int G[100][100];
	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
			if(i==j)
				G[i][j] = 0;
			else
				G[i][j] = -1;

	for(int i=0; i<n; i++){
		int r;
		cin >> r;
		int k;
		cin >> k;
		for(int j=0; j<k; j++){
			int t;
			cin >> t;
			G[r-1][t-1] = 1;
		}
	}
/*
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++)
			cout << G[i][j] << ' ';
		cout << endl;
	}
*/

	for(int k=0; k<n; k++)
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++)
				if(G[i][k]==-1||G[k][j]==-1)
					continue;
				else if(G[i][j]==-1||(G[i][j]>G[i][k]+G[k][j]))
					G[i][j] = G[i][k]+G[k][j];

	int p;
	cin >> p;

	for(int i=0; i<p; i++){
		int s, d, v;
		cin >> s >> d >> v;

		if(v>=G[s-1][d-1]+1)
			cout << G[s-1][d-1]+1 << endl;
		else
			cout << "NA" << 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);

	bool FLG;

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

		FLG = false;

		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;
		}

		P[start] = 1;
		num = 1;

		for(int index = 1; index <= n; index++){
			if(index != start && table[start][index] == 1){
				if(memo[index][goal] > 0 && memo[index][goal] + 1 <= t_limit){
					printf("%d\n",memo[index][goal]+1);
					FLG = true;
					break;
				}
				D[index] = 1;
			}
		}

		if(FLG)continue;

		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?¢???????

			/*if(memo[current][goal] > 0 && D[current]+memo[current][goal] <= t_limit){
				D[goal] = D[current]+memo[current][goal]-1;
				break;
			}*/

			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<stdio.h>
#include<limits.h>
#include<queue>
#include<utility>
#include<algorithm>
using namespace std;

const int N = 108;
bool isConnect[N][N];

int MinRoot(int from,int to,int rootNum)
{
	int distance[N],i;
	fill(distance,distance+N,INT_MAX);
	queue<pair<int,int> > q;
	pair<int,int> p;
	q.push(make_pair(from,1));
	while(!q.empty())
	{
		p=q.front(); q.pop();
		if(distance[p.first] <= p.second) continue;

		distance[p.first] = p.second;

		for(i=1; i<=rootNum; ++i)
		{
			if(isConnect[p.first][i])
			{
				q.push(make_pair(i,p.second+1));
			}
		}
	}
	return distance[to];
}
int main()
{
	int n,connectCount,fromRooterNum,toRooterNum,i,j,p,ttl;

	for(i=0;i<N;++i) for(j=0;j<N;++j) isConnect[i][j] = false;

	scanf("%d",&n);
	for(i=0; i<n; ++i)
	{
		scanf("%d%d",&fromRooterNum,&connectCount);
		for(j=0; j<connectCount; ++j)
			scanf("%d",&toRooterNum),isConnect[fromRooterNum][toRooterNum]=true;
	}
	scanf("%d",&p);
	for(i=0;i<p;++i)
	{
		scanf("%d%d%d",&fromRooterNum,&toRooterNum,&ttl);
		int minRooter = MinRoot(fromRooterNum,toRooterNum,n);
		if(minRooter>ttl) puts("NA");
		else printf("%d\n",minRooter);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    int n,p;
    int map[101][101];
    memset(map,10000,sizeof(map));
    cin >> n;
    REP(i,n){
	int r,k,t;
	cin >> r >> k;
	REP(j,k){
	    cin >> t;
	    map[r][t] = 1;
	}
    }
    n++;
    while(true){
	bool flg = false;
	REP(k,n){
	    REP(i,n){
		REP(j,n){
		    if(map[i][j] > (map[i][k] + map[k][j])){
			flg = true;
			map[i][j] = map[i][k] + map[k][j];
		    }
		}
	    }
	}
	if(!flg) break;
    }

    cin >> p;
    REP(a,p){
	int s,d,v;
	cin >> s >> d >> v;
	if(map[s][d] < v) cout << map[s][d]+1 << endl;
	else cout << "NA" << endl;
    }
    return 0;
}

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

int main()
{
  int n;
  int node[100][100];
  cin>>n;
  for(int i=0;i<100;i++)
    for(int j=0;j<100;j++)
      node[i][j]=0;
  
  
  for(int x=0;x<n;x++)
    {
      int k,t;
      cin>>k>>t;
      for(int i=0;i<t;i++)
	{
	  int tmp;
	  cin>>tmp;
	  node[k][tmp]=1;
	}
    }
  int p;
  cin>>p;
  for(;p--;)
    {
      int s,d,v;
      cin>>s>>d>>v;
      queue<P> qu;
      while(!qu.empty())
	{
	  qu.pop();
	}
      int ans=-1;
      bool move[n+1];
      if(v>1)
	{
	  for(int i=0;i<=n;i++)
	    {
	      if(node[s][i]==1)
		qu.push(P(i,2));
	    }
	  for(int i=0;i<=n;i++)
	    {
	      move[i]=true;
	    }

	}
      while(!qu.empty())
	{
	  P a = qu.front();
	  qu.pop();
	  //	  cout<<"Queue:"<<a.first<<"  "<<a.second<<endl;
	  if(a.first==d)
	    {
	      ans = a.second;
	      break;
	    }
	  for(int i=1;i<=n;i++)
	    {
	      if(node[a.first][i]==1)
		{
		  if(move[i] && a.second+1<=v)
		    {
		      qu.push(P(i,a.second+1));
		      move[i]=false;
		    }
		}
	    }
	}
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
    }
}