Language: Java

import java.util.*;
import java.io.*;

public class Main
{
  static int[] sum = new int[200000];
  public static void main(String args[])throws IOException
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tokenizer1 = new StringTokenizer(br.readLine());
    
    int n = Integer.parseInt(tokenizer1.nextToken());
    int q = Integer.parseInt(tokenizer1.nextToken());
    
    int[][] matrix = new int[n][n];
    int c1,c2;
    
    for(int i=0;i<n-1;i++)
    {
      StringTokenizer tokenizer2 = new StringTokenizer(br.readLine());
      int c1  = Integer.parseInt(tokenizer2.nextToken());
      int c2 = Integer.parseInt(tokenizer2.nextToken());
      
      matrix[c1][c2] = 1;
    }
    
    
    for(int i=0;i<q;i++)
    {
       StringTokenizer tokenizer3 = new StringTokenizer(br.readLine());
       int c1  = Integer.parseInt(tokenizer3.nextToken());
       int c2 = Integer.parseInt(tokenizer3.nextToken());
       BFS(new Pair(c1,c2),matrix,n);
       
    }
    for(int i=0;i<n;i++)
    {
      System.out.print(sum[i]+" ");
    }
  }
  
  public static void BFS(Pair pair,int[][] matrix,int size)
  {
     LinkedList<Integer> adj[];
    adj = new LinkedList[v]; 
        for (int i=0; i<size; ++i) 
            adj[i] = new LinkedList();
    for(int i=0;i<size;i++)
    {
      for(int j=0;j<size;j++)
      {
        if(matrix[i][j] == 1)
        {
          adj[i].add(j);
        }
      }
    }
    boolean visited = new boolean[size];
    LinkedList<Integer> queue = new LinkedList<Integer>();
    visited[pair.node] = 1;
    queue.add(pair.node);
    int value = pair.value;
    int s;
    
    while(queue.size() != 0)
    {
      s = queue.poll();
      sum[s] += value;
      Iterator<Integer> it = new adj[s].listIterator();
      while(it.hasNext())
      {
        int n1 = it.next();
        if(!visited[n1])
        {
          visited[n1] = true;
          queue.add(n1);
        }
      }
      
    }
  }
}

class Pair
{
  public int node;
  public int value;
  Pair(int node,int value)
  {
    this.node = node;
    this.value = value;
  }
}

Compiler output:
Main.java:21: error: variable c1 is already defined in method main(String[])
      int c1  = Integer.parseInt(tokenizer2.nextToken());
          ^
Main.java:22: error: variable c2 is already defined in method main(String[])
      int c2 = Integer.parseInt(tokenizer2.nextToken());
          ^
Main.java:31: error: variable c1 is already defined in method main(String[])
       int c1  = Integer.parseInt(tokenizer3.nextToken());
           ^
Main.java:32: error: variable c2 is already defined in method main(String[])
       int c2 = Integer.parseInt(tokenizer3.nextToken());
           ^
Main.java:45: error: cannot find symbol
    adj = new LinkedList[v]; 
                         ^
  symbol:   variable v
  location: class Main
Main.java:58: error: incompatible types: boolean[] cannot be converted to boolean
    boolean visited = new boolean[size];
                      ^
Main.java:60: error: array required, but boolean found
    visited[pair.node] = 1;
           ^
Main.java:69: error: cannot find symbol
      Iterator<Integer> it = new adj[s].listIterator();
                                 ^
  symbol:   class adj
  location: class Main
Main.java:73: error: array required, but boolean found
        if(!visited[n1])
                   ^
Main.java:75: error: array required, but boolean found
          visited[n1] = true;
                 ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
10 errors