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>
#include <climits>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
	int n, p, r, k, t, s, d, v;
	cin >> n;
	int m[100][100] = {};
	rep(i,n) {
		cin >> r >> k;
		rep(j,k) {
			cin >> t;
			m[r-1][t-1] = 1;
		}
	}
	int ret[100][100] = {};
	rep(i,100) {
		ret[i][i] = 1;
	}
	rep(i,n) {
		queue<int> q;
		q.push(i);
		while(!q.empty()) {
			int a = q.front(); q.pop();
			rep(j,n) {
				if(m[a][j] && ret[i][j]==0) {
					q.push(j);
					ret[i][j] = ret[i][a]+1;
				}
			}
		}
	}
	cin >> p;
	rep(i,p) {
		cin >> s >> d >> v;
		--s,--d;
		if(ret[s][d] > v) cout<< "NA" << endl;
		else cout << ret[s][d] << endl;
	}
	return 0;
}

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

using namespace std;

int main(){
  while(1){
  int n;
    cin >> n;
    if(n == 0) break;
    
    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;
      }
    }


    int sumpake;
    cin >> sumpake;

    queue<int> Q;

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

    for(int i = 0; i < sumpake; i++){
      min = 0;
      cin >> from >> to >> ttl;
      myposi = from;
      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();
	    for(int k = 1; k <= n; k++){
	      if(data[myposi][k] == 1) {
		Q.push(k);
		sumin++;
	      }
	    }
	    
	  }
	  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<queue>
using namespace std;
int d[101][101]={};
int x[101];
int n;
void bfs(int);
int main(){
  int p;
  int na[100];
  int to;
  int th;
  int st,go,ttl;
  //syoki
  for(int i=0;i<n;i++){
    x[i]=10000;
  }
  //in
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> na[i] >> to;
    for(int j=0;j<to;j++){
      cin >> th;
      d[na[i]][th]=1;
    }
  
  }
  //in2
  cin >> p;
  for(int i=0;i<p;i++){
    for(int j=0;j<=n;j++){
      x[j]=10000;
    }
    
    cin>> st >> go >> ttl;
    x[st]=1;
    bfs(st);
    if(x[go]<=ttl && x[go]!=10000)cout << x[go] << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

///bfs///////////////////////////////////////////
void bfs(int s){
  queue<int>q;
  int u;
  q.push(s);
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<=n;v++){
      if(d[u][v]==1&&x[v]==10000){
	x[v]=x[u]+1;
	q.push(v);
      }
    }
  }
  return ;
}

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(G[s-1][d-1]==-1){
			cout << "NA" << endl;
			continue;
		}

		if(v>=G[s-1][d-1]+1)
			cout << G[s-1][d-1]+1 << endl;
		else
			cout << "NA" << endl;
	}

	return 0;
}

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

using namespace std;
int n,a,b,c,p;
vector <int> t[100][100];
int u[100][100];
int no[100],in[100];
int in2[100][100];
int INF = (1<<24);

int change(int x){
  for(int i=0;i<n;i++)if(no[i]==x)return i;
}
vector<int> x,z;


int main(){
  for(int i=0;i<100;i++)
    for(int j=0;j<100;j++){
      u[i][j]=(i==j?0: INF );
      if(i==j)continue;
      t[i][j].push_back(i);
      t[i][j].push_back(j);
    }
  cin>>n;

  for(int i=0;i<n;i++){
    cin>>no[i];
    cin>>in[i];
    for(int j=0;j<in[i];j++){
      cin>>in2[i][j];
    }
  }
  
  
  for(int i=0;i<n;i++){
    for(int j=0;j<in[i];j++){
     u[i][change(in2[i][j])]=1;
    }
  }

  


  for(int k=0;k<n;k++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	if(u[i][k]+u[k][j]<u[i][j]){
	  u[i][j]=u[i][k]+u[k][j];

	  /*
	  t[i][j].clear();
	  x=t[i][k];
	  z=t[k][j];
	  for(int l=0;l<(int)x.size();l++)t[i][j].push_back(x[l]);
	  t[i][j].push_back(no[j]);
	  for(int l=0;l<(int)z.size();l++)t[i][j].push_back(z[l]);
	  */
	}
      }
    }
  }
  cin>>p;
  while(p--){
    cin>>a>>b>>c;
    if(u[change(a)][change(b)]+1>c)cout<<"NA"<<endl;
    else{
      cout<<u[change(a)][change(b)]+1<<endl;
      /*cout<<t[change(a)][change(b)].size()<<endl;
      
      for(int i=0;i<(int)t[change(a)][change(b)].size();i++){
	if(i)cout<<' ';
	cout<<t[change(a)][change(b)][i];
      }
      cout<<endl;
      */
    }
  }
  return 0;
}