Language: C++

import java.util.LinkedList;
import java.util.Scanner;



public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int size = sc.nextInt();
		AdjListGraph g = new AdjListGraph(size, true);
		for (int i = 0; i < size; i++){
			int v = sc.nextInt()-1;
			int d = sc.nextInt();
			for(int j=0; j<d; j++){
				g.addEdge(v, sc.nextInt()-1);
			}
		}
		g.BFS(0);
	}

}


class Pair{
	int key;int value;
	Pair(int K, int V){key = K; value = V;}
	int getKey(){return key;}
	int getValue(){return value;}
}	

class AdjListGraph { // http://theoryofprogramming.com/adjacency-list-in-java/
	private boolean digraph;
	private final LinkedList<Pair>[] adjList;
	@SuppressWarnings("unchecked")
	AdjListGraph(int numV, boolean flag){
		adjList = (LinkedList<Pair>[]) new LinkedList[numV];
		for (int i = 0; i < adjList.length; i++) {
			adjList[i] = new LinkedList<>();
		}
		digraph = flag;
	}
	boolean directed(){return digraph;} //return the graph is directed or not
	int getNumOfVertices(){return adjList.length;} //return the num of vertices
	public void addEdge(int startV, int endV, int weight) {
		adjList[startV].add(new Pair(endV, weight));
		if(!digraph){adjList[endV].add(new Pair(startV, weight));}
	} //add an edge to the graph. 
	public void addEdge(int startV, int endV) {
		addEdge(startV, endV, 1); 
	} //weight is set to 1 as a default
	public LinkedList<Integer> getEdgesFromVertexNoWeight(int startV) {
		LinkedList< Integer > edgeList= (LinkedList< Integer >) new LinkedList<Integer>();
		for(Pair pair : adjList[startV]){
			edgeList.add(pair.getKey());
		}
		return edgeList;
	} // get the list of the edges leaving startV. This is for no weight graph. 
	public void print() {
		int i = 0;
		for (LinkedList<Pair> list : adjList) {
			System.out.print("adjList[" + i + "] -> ");
			for (Pair edge : list) {System.out.print(edge.getKey() + "(" + edge.getValue() + ")");}
			i++;
			System.out.println();
		}
	}
	void BFS(int src){
		boolean visited[] = new boolean[getNumOfVertices()];		
		int dist[] = new int[getNumOfVertices()];
		LinkedList<Integer> queue = new LinkedList<Integer>();
		visited[src]=true;	
		queue.add(src);	
		while (queue.size() != 0){
			int tmp = queue.poll();
			LinkedList<Integer> neighbors =  getEdgesFromVertexNoWeight(tmp);
			for(Integer i : neighbors){
				if(!visited[i]){
					visited[i] = true;
					dist[i] = dist[tmp]+1;
					queue.add(i);
				}
			}
		}
		for(int i=0; i< dist.length ; i++){
			if(i!=src && dist[i]==0){
				dist[i]=-1;
			}
		}
		for(int i=0; i< dist.length ; i++){
			System.out.println(Integer.toString(i+1) + " " +  dist[i]);
		}
//		System.out.println(Arrays.toString(dist));
	}
}




Compiler output:
a.cc:35:9: error: stray '@' in program
   35 |         @SuppressWarnings("unchecked")
      |         ^
a.cc:1:1: error: 'import' does not name a type
    1 | import java.util.LinkedList;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:1: error: 'import' does not name a type
    2 | import java.util.Scanner;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:1: error: expected unqualified-id before 'public'
    6 | public class Main {
      | ^~~~~~
a.cc:30:8: error: expected ';' after class definition
   30 | }
      |  ^      
      |        ;
a.cc:33:16: error: expected ':' before 'boolean'
   33 |         private boolean digraph;
      |                ^~~~~~~~
      |                :
a.cc:33:17: error: 'boolean' does not name a type; did you mean 'bool'?
   33 |         private boolean digraph;
      |                 ^~~~~~~
      |                 bool
a.cc:34:16: error: expected ':' before 'final'
   34 |         private final LinkedList<Pair>[] adjList;
      |                ^~~~~~
      |                :
a.cc:34:17: error: 'final' does not name a type
   34 |         private final LinkedList<Pair>[] adjList;
      |                 ^~~~~
a.cc:35:27: error: expected identifier before string constant
   35 |         @SuppressWarnings("unchecked")
      |                           ^~~~~~~~~~~
a.cc:35:27: error: expected ',' or '...' before string constant
a.cc:35:10: error: ISO C++ forbids declaration of 'SuppressWarnings' with no type [-fpermissive]
   35 |         @SuppressWarnings("unchecked")
      |          ^~~~~~~~~~~~~~~~
a.cc:35:38: error: expected ';' at end of member declaration
   35 |         @SuppressWarnings("unchecked")
      |                                      ^
      |                                       ;
a.cc:36:32: error: 'boolean' has not been declared
   36 |         AdjListGraph(int numV, boolean flag){
      |                                ^~~~~~~
a.cc:43:9: error: 'boolean' does not name a type; did you mean 'bool'?
   43 |         boolean directed(){return digraph;} //return the graph is directed or not
      |         ^~~~~~~
      |         bool
a.cc:45:15: error: expected ':' before 'void'
   45 |         public void addEdge(int startV, int endV, int weight) {
      |               ^~~~~
      |               :
a.cc:49:15: error: expected ':' before 'void'
   49 |         public void addEdge(int startV, int endV) {
      |               ^~~~~
      |               :
a.cc:52:15: error: expected ':' before 'LinkedList'
   52 |         public LinkedList<Integer> getEdgesFromVertexNoWeight(int startV) {
      |               ^~~~~~~~~~~
      |               :
a.cc:52:16: error: 'LinkedList' does not name a type
   52 |         public LinkedList<Integer> getEdgesFromVertexNoWeight(int startV) {
      |                ^~~~~~~~~~
a.cc:59:15: error: expected ':' before 'void'
   59 |         public void print() {
      |               ^~~~~
      |               :
a.cc:95:2: error: expected ';' after class definition
   95 | }
      |  ^
      |  ;
a.cc: In constructor 'AdjListGraph::AdjListGraph(int, int)':
a.cc:37:17: error: 'adjList' was not declared in this scope
   37 |                 adjList = (LinkedList<Pair>[]) new LinkedList[numV];
      |                 ^~~~~~~
a.cc:37:28: error: 'LinkedList' was not declared in this scope
   37 |                 adjList = (LinkedList<Pair>[]) new LinkedList[numV];
      |                            ^~~~~~~~~~
a.cc:37:43: error: expected primary-expression before '>' token
   37 |                 adjList = (LinkedList<Pair>[]) new LinkedList[numV];
      |                                           ^
a.cc: In lambda function:
a.cc:37:46: error: expected '{' before ')' token
   37 |                 adjList = (LinkedList<Pair>[]) new LinkedList[numV];
      |                                              ^
a.cc: In constructor 'AdjListGraph::AdjListGraph(int, int)':
a.cc:39:42: error: 'LinkedList' does not name a type
   39 |                         adjList[i] = new LinkedList<>();
      |                                          ^~~~~~~~~~
a.cc:39:53: error: expected primary-expression before '>' token
   39 |                         adjList[i] = new LinkedList<>();
      |                                                     ^
a.cc:39:55: error: expected primary-expression before ')' token
   39 |                         adjList[i] = new LinkedList<>();
      |                                                       ^
a.cc:41:17: error: 'digraph' was not declared in this scope
   41 |                 digraph = flag;
      |                 ^~~~~~~
a.cc: In member function 'int AdjListGraph::getNumOfVertices()':
a.cc:44:39: error: 'adjList' was not declared in this scope
   44 |         int getNumOfVertices(){return adjList.length;} //return the num of vertices
      |                                       ^~~~~~~
a.cc: In member function 'void AdjListGraph::addEdge(int, int, int)':
a.cc:46:17: error: 'adjList' was not declared in this scope
   46 |                 adjList[startV].add(new Pair(endV, weight));
      |                 ^~~~~~~
a.cc:46:58: error: 'Pair::Pair(int, int)' is private within this context
   46 |                 adjList[startV].add(new Pair(endV, weight));
      |                                                          ^
a.cc:27:9: note: declared private here
   27 |         Pair(int K, int V){key = K; value = V;}
      |         ^~~~
a.cc:47:21: error: 'digraph' was not declared in this scope
   47 |                 if(!digraph){adjList[endV].add(new Pair(startV, weight));}
      |                     ^~~~~~~
a.cc:47:71: error: 'Pair::Pair(int, int)' is private within this context
   47 |                 if(!digraph){adjList[endV].add(new Pair(startV, weight));}
      |                                                                       ^
a.cc:27:9: note: declared private here
   27 |         Pair(int K, int V){key = K; value = V;}
      |         ^~~~
a.cc: In member function 'void AdjListGraph::print()':
a.cc:61:22: error: 'LinkedList' was not declared in this scope
   61 |                 for (LinkedList<Pair> list : adjList) {
      |                      ^~~~~~~~~~
a.cc:61:37: error: expected primary-expression before '>' token
   61 |                 for (LinkedList<Pair> list : adjList) {
      |                                     ^
a.cc:61:44: error: found ':' in nested-name-specifier, expected '::'
   61 |                 for (LinkedList<Pair> list : adjList) {
      |                                            ^
      |                                            ::
a.cc:61:39: error: 'list' has not been declared
   61 |                 for (LinkedList<Pair> list : adjList) {
      |                                       ^~~~
a.cc:67:9: error: expected primary-expression before '}' token
   67 |         }
      |         ^
a.cc:67:9: error: expected ';' before '}' token
a.cc:67:9: error: expected primary-expression before '}' token
a.cc:67:9: error: expected ')' before '}' token
a.cc:61:21: note: to match this '('
   61 |                 for (LinkedList<Pair> list : adjList) {
      |                     ^
a.cc:67:9: error: expected primary-expression before '}' token
   67 |         }
      |         ^
a.cc: In member function 'void AdjListGraph::BFS(int)':
a.cc:69:17: error: 'boolean' was not declared in this scope; did you mean 'bool'?
   69 |                 boolean visited[] = new boolean[getNumOfVertices()];
      |                 ^~~~~~~
      |                 bool
a.cc:70:30: error: initializer fails to determine size of 'dist'
   70 |                 int dist[] = new int[getNumOfVertices()];
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:70:30: error: array must be initialized with a brace-enclosed initializer
a.cc:71:17: error: 'LinkedList' was not declared in this scope
   71 |                 LinkedList<Integer> queue = new LinkedList<Integer>();
      |                 ^~~~~~~~~~
a.cc:71:28: error: 'Integer' was not declared in this scope
   71 |                 LinkedList<Integer> queue = new LinkedList<Integer>();
      |                            ^~~~~~~
a.cc:71:37: error: 'queue' was not declared in this scope
   71 |                 LinkedList<Integer> queue = new LinkedList<Integer>();
      |                                     ^~~~~
a.cc:71:49: error: 'LinkedList' does not name a type
   71 |                 LinkedList<Integer> queue = new LinkedList<Integer>();
      |                                                 ^~~~~~~~~~
a.cc:71:69: error: expected primary-expression before ')' token
   71 |                 LinkedList<Integer> queue = new LinkedList<Integer>();
      |                                                                     ^
a.cc:72:17: error: 'visited' was not declared in this scope
   72 |                 visited[src]=true;
      |                 ^~~~~~~
a.cc:76:45: error: 'neighbors' was not declared in this scope
   76 |                         LinkedList<Integer> neighbors =  getEdgesFromVertexNoWeight(tmp);
      |                                             ^~~~~~~~~
a.cc:76:58: error: 'getEdgesFromVertexNoWeight' was not declared in this scope
   76 |                         LinkedList<Integer> neighbors =  getEdgesFromVertexNoWeight(tmp);
      |                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:77:37: error: expected ';' before 'i'
   77 |                         for(Integer i : neighbors){
      |                                     ^
a.cc:84:17: error: expected primary-expression before '}' token
   84 |                 }
      |                 ^
a.cc:84:17: error: expected ';' before '}' token
a.cc:84:17: error: expected primary-expression before '}' token
a.cc:84:17: error: expected ')' before '}' token
a.cc:77:28: note: to match this '('
   77 |                         for(Integer i : neighbors){
      |                            ^
a.cc:84:17: error: expected primary-expression before '}' token
   84 |                 }
      |                 ^
a.cc:85:38: error: request for member 'length' in 'dist', which is of non-class type 'in