Language: C++


import java.util.*;


public class Main{ 

	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		Graph graph = new Graph(scan.nextInt());
		int relationNum = scan.nextInt();
		for(int i=0; i<relationNum; i++)
			graph.setRelation(scan.nextInt(), scan.nextInt());
		int queryNum = scan.nextInt();
		for(int i=0; i<queryNum; i++)
			if(graph.bfs(scan.nextInt(), scan.nextInt())==-1)
				System.out.println("no");
			else
				System.out.println("yes");
		//graph.bfs(16);

	}

}
class Graph{
	int verNum;
	ArrayList<Node> vertices;
	public Graph(int n){
		vertices = new ArrayList<Node>();
		this.verNum = n;
		for(int i=0; i<verNum;i++)
			vertices.add(new Node(i));
	}
	public void setRelation(int a, int b){
		vertices.get(a).addNext(vertices.get(b));
	}
	public Graph(int[][] matrix){
		vertices = new ArrayList<Node>();
		for(int i=0; i<matrix.length;i++)
			vertices.add(new Node(i+1));
		for(int i=0; i<matrix.length;i++)
			for(int j=0; j<matrix.length; j++)
				if(matrix[i][j]!=0)
					vertices.get(i).addNext(vertices.get(j));
	}
	void walk(){
		int time = 0;
		for(int i=0; i<vertices.size();i++){
			time = vertices.get(i).walk(time);
		}
	}
	void nearest(){	
		//for(int i=0; i<vertices.size();i++)
			//System.out.println((i+1) + " " + bfs(i+1));
	}
	void clearHistory(){
		for(int i=0; i<vertices.size();i++)
					vertices.get(i).disSteped();	
	}
	void print(){

		for(int i=0; i<vertices.size();i++)
			vertices.get(i).result();
	}

	int bfs(int start, int target){
		ArrayDeque<Node> queue = new ArrayDeque<Node>();
		clearHistory();
		queue.addFirst(vertices.get(start));
		int dist = 0;
		int childCount = 1;
		int nextChildrenCount=0;
		while(!queue.isEmpty()){
			Node temp = queue.removeLast();
			if(temp.id==target)
				return dist;
			childCount--;

			for(Node n : temp.nextNode){
				if(!n.steped){
					n.steped = true;
					nextChildrenCount++;
					queue.addFirst(n);
				}
			}
			if(childCount==0){
				dist++;
				childCount = nextChildrenCount;
				nextChildrenCount=0;
			}
			//System.out.println("queue size : " + queue.size());
		}
		return -1;

	}
}

class Node{
	int id;
	int startTime;
	int endTime;
	boolean steped = false;
	ArrayList<Boolean> stepedNext = new ArrayList<Boolean>(); 
	ArrayList<Node> nextNode = new ArrayList<Node>();
	public Node(int id){
		this.id = id;
	}
	public void addNext(Node n){
		nextNode.add(n);
		stepedNext.add(false);
	}
	public void disSteped(){
		this.steped = false;
	}
	
	public int walk(int time){
		if(this.steped)
			return time;
		time++;//
		this.startTime = time;
		this.steped = true;
		int end = time;
		for(int i=0; i<stepedNext.size(); i++){
			time = nextNode.get(i).walk(time);
		}
		time++;//
		this.endTime = time;
		return endTime;
	}
	boolean remainNext(){
		return stepedNext.contains(false);
	}
	void result(){
		System.out.println(this.id + " " + startTime + " " + endTime);
	}
}
class AdjacencyMatrices{
	int pointNum;
	int[][] matrix;
	public AdjacencyMatrices(int n){
		pointNum = n;
		matrix = new int[n][];
		for(int i=0; i<n; i++)
			matrix[i] = new int[n];
	}
	public void readList(Scanner scan){
		for(int i=0; i<pointNum; i++){
			int index = scan.nextInt()-1;
			int dim = scan.nextInt();
			for(int j=0; j< dim; j++)
				matrix[index][scan.nextInt()-1] = 1;
		}
	}
	public int[][] getMatrix(){
		return matrix;
	}
	public void printMatrix(){
		StringBuilder temp = new StringBuilder();
		for(int i=0; i<pointNum; i++){
			for(int j=0; j<pointNum; j++){
				temp.append(matrix[i][j]);
				if(j!=pointNum-1)
					temp.append(" ");
			}
			temp.append("\n");
		}
		System.out.print(new String(temp));
	}

}

Compiler output:
a.cc:2:1: error: 'import' does not name a type
    2 | import java.util.*;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: expected unqualified-id before 'public'
    5 | public class Main{
      | ^~~~~~
a.cc:26:9: error: 'ArrayList' does not name a type
   26 |         ArrayList<Node> vertices;
      |         ^~~~~~~~~
a.cc:27:15: error: expected ':' before 'Graph'
   27 |         public Graph(int n){
      |               ^~~~~~
      |               :
a.cc:33:15: error: expected ':' before 'void'
   33 |         public void setRelation(int a, int b){
      |               ^~~~~
      |               :
a.cc:36:15: error: expected ':' before 'Graph'
   36 |         public Graph(int[][] matrix){
      |               ^~~~~~
      |               :
a.cc:36:28: error: multidimensional array must have bounds for all dimensions except the first
   36 |         public Graph(int[][] matrix){
      |                            ^
a.cc:36:30: error: expected ',' or '...' before 'matrix'
   36 |         public Graph(int[][] matrix){
      |                              ^~~~~~
a.cc:95:2: error: expected ';' after class definition
   95 | }
      |  ^
      |  ;
a.cc: In constructor 'Graph::Graph(int)':
a.cc:28:17: error: 'vertices' was not declared in this scope
   28 |                 vertices = new ArrayList<Node>();
      |                 ^~~~~~~~
a.cc:28:32: error: 'ArrayList' does not name a type
   28 |                 vertices = new ArrayList<Node>();
      |                                ^~~~~~~~~
a.cc:28:42: error: 'Node' was not declared in this scope
   28 |                 vertices = new ArrayList<Node>();
      |                                          ^~~~
a.cc:28:48: error: expected primary-expression before ')' token
   28 |                 vertices = new ArrayList<Node>();
      |                                                ^
a.cc:29:22: error: request for member 'verNum' in '(Graph*)this', which is of pointer type 'Graph*' (maybe you meant to use '->' ?)
   29 |                 this.verNum = n;
      |                      ^~~~~~
a.cc:31:42: error: expected type-specifier before 'Node'
   31 |                         vertices.add(new Node(i));
      |                                          ^~~~
a.cc: In member function 'void Graph::setRelation(int, int)':
a.cc:34:17: error: 'vertices' was not declared in this scope
   34 |                 vertices.get(a).addNext(vertices.get(b));
      |                 ^~~~~~~~
a.cc: In constructor 'Graph::Graph(...)':
a.cc:37:17: error: 'vertices' was not declared in this scope
   37 |                 vertices = new ArrayList<Node>();
      |                 ^~~~~~~~
a.cc:37:32: error: 'ArrayList' does not name a type
   37 |                 vertices = new ArrayList<Node>();
      |                                ^~~~~~~~~
a.cc:37:42: error: 'Node' was not declared in this scope
   37 |                 vertices = new ArrayList<Node>();
      |                                          ^~~~
a.cc:37:48: error: expected primary-expression before ')' token
   37 |                 vertices = new ArrayList<Node>();
      |                                                ^
a.cc:38:32: error: 'matrix' was not declared in this scope
   38 |                 for(int i=0; i<matrix.length;i++)
      |                                ^~~~~~
a.cc:39:42: error: expected type-specifier before 'Node'
   39 |                         vertices.add(new Node(i+1));
      |                                          ^~~~
a.cc:40:32: error: 'matrix' was not declared in this scope
   40 |                 for(int i=0; i<matrix.length;i++)
      |                                ^~~~~~
a.cc: In member function 'void Graph::walk()':
a.cc:47:32: error: 'vertices' was not declared in this scope
   47 |                 for(int i=0; i<vertices.size();i++){
      |                                ^~~~~~~~
a.cc: In member function 'void Graph::clearHistory()':
a.cc:56:32: error: 'vertices' was not declared in this scope
   56 |                 for(int i=0; i<vertices.size();i++)
      |                                ^~~~~~~~
a.cc: In member function 'void Graph::print()':
a.cc:61:32: error: 'vertices' was not declared in this scope
   61 |                 for(int i=0; i<vertices.size();i++)
      |                                ^~~~~~~~
a.cc: In member function 'int Graph::bfs(int, int)':
a.cc:66:17: error: 'ArrayDeque' was not declared in this scope
   66 |                 ArrayDeque<Node> queue = new ArrayDeque<Node>();
      |                 ^~~~~~~~~~
a.cc:66:28: error: 'Node' was not declared in this scope
   66 |                 ArrayDeque<Node> queue = new ArrayDeque<Node>();
      |                            ^~~~
a.cc:66:34: error: 'queue' was not declared in this scope
   66 |                 ArrayDeque<Node> queue = new ArrayDeque<Node>();
      |                                  ^~~~~
a.cc:66:46: error: 'ArrayDeque' does not name a type
   66 |                 ArrayDeque<Node> queue = new ArrayDeque<Node>();
      |                                              ^~~~~~~~~~
a.cc:66:63: error: expected primary-expression before ')' token
   66 |                 ArrayDeque<Node> queue = new ArrayDeque<Node>();
      |                                                               ^
a.cc:68:32: error: 'vertices' was not declared in this scope
   68 |                 queue.addFirst(vertices.get(start));
      |                                ^~~~~~~~
a.cc:73:30: error: expected ';' before 'temp'
   73 |                         Node temp = queue.removeLast();
      |                              ^~~~
a.cc:74:28: error: 'temp' was not declared in this scope
   74 |                         if(temp.id==target)
      |                            ^~~~
a.cc:78:34: error: expected ';' before 'n'
   78 |                         for(Node n : temp.nextNode){
      |                                  ^
a.cc:85:25: error: expected primary-expression before 'if'
   85 |                         if(childCount==0){
      |                         ^~
a.cc:85:25: error: expected ';' before 'if'
a.cc:85:25: error: expected primary-expression before 'if'
a.cc:85:25: error: expected ')' before 'if'
a.cc:78:28: note: to match this '('
   78 |                         for(Node n : temp.nextNode){
      |                            ^
a.cc: At global scope:
a.cc:101:9: error: 'boolean' does not name a type; did you mean 'bool'?
  101 |         boolean steped = false;
      |         ^~~~~~~
      |         bool
a.cc:102:9: error: 'ArrayList' does not name a type
  102 |         ArrayList<Boolean> stepedNext = new ArrayList<Boolean>();
      |         ^~~~~~~~~
a.cc:103:9: error: 'ArrayList' does not name a type
  103 |         ArrayList<Node> nextNode = new ArrayList<Node>();
      |         ^~~~~~~~~
a.cc:104:15: error: expected ':' before 'Node'
  104 |         public Node(int id){
      |               ^~~~~
      |               :
a.cc:107:15: error: expected ':' before 'void'
  107 |         public void addNext(Node n){
      |               ^~~~~
      |               :
a.cc:111:15: error: expected ':' before 'void'
  111 |         public void disSteped(){
      |               ^~~~~
      |               :
a.cc:115:15: error: expected ':' before 'int'
  115 |         public int walk(int time){
      |               ^~~~
      |               :
a.cc:129:9: error: 'boolean' does not name a type; did you mean 'bool'?
  129 |         boolean remainNext(){
      |         ^~~~~~~
      |         bool
a.cc:135:2: error: expected ';' after class definition
  135 | }
      |  ^
      |  ;
a.cc: In constructor 'Node::Node(int)':
a.cc:105:22: error: request for member 'id' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  105 |                 this.id = id;
      |                      ^~
a.cc: In member function 'void Node::addNext(Node)':
a.cc:108:17: error: 'nextNode' was not declared in this scope
  108 |                 nextNode.add(n);
      |                 ^~~~~~~~
a.cc:109:17: error: 'stepedNext' was not declared in this scope
  109 |                 stepedNext.add(false);
      |                 ^~~~~~~~~~
a.cc: In member function 'void Node::disSteped()':
a.cc:112:22: error: request for member 'steped' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  112 |                 this.steped = false;
      |                      ^~~~~~
a.cc: In member function 'int Node::walk(int)':
a.cc:116:25: error: request for member 'steped' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  116 |                 if(this.steped)
      |                         ^~~~~~
a.cc:119:22: error: request for member 'startTime' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  119 |                 this.startTime = time;
      |                      ^~~~~~~~~
a.cc:120:22: error: request for member 'steped' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  120 |                 this.steped = true;
      |                      ^~~~~~
a.cc:122:32: error: 'stepedNext' was not declared in this scope
  122 |                 for(int i=0; i<stepedNext.size(); i++){
      |                                ^~~~~~~~~~
a.cc:123:32: error: 'nextNode' was not declared in this scope
  123 |                         time = nextNode.get(i).walk(time);
      |                                ^~~~~~~~
a.cc:126:22: error: request for member 'endTime' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  126 |                 this.endTime = time;
      |                      ^~~~~~~
a.cc: In member function 'void Node::result()':
a.cc:133:17: error: 'System' was not declared in this scope
  133 |                 System.out.println(this.id + " " + startTime + " " + endTime);
      |                 ^~~~~~
a.cc:133:41: error: request for member 'id' in '(Node*)this', which is of pointer type 'Node*' (maybe you meant to use '->' ?)
  133 |                 System.out.println(this.id + " " + startTime + " " + endTime);
      |