Advertisement
Kali_prasad

A20250305b_meesho

Mar 7th, 2025
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. import java.util.*;
  2. //A20250305b_meesho
  3. /*
  4.  
  5.  
  6. given you a tree
  7. each node having a integer value
  8. your task is to find the count of valid vertical paths in that tree
  9.  
  10. a path is valid
  11.  
  12. if (sum of all ancestor nodes) % m is 0
  13.  
  14.  
  15.  
  16. */
  17.  
  18. /*
  19.  
  20. //important thing to be noted here is
  21.  
  22. in vertical paths only ancestor nodes and current node exist
  23.  
  24. assume this is your tree and m = 2
  25.  
  26.                 1
  27.                / \
  28.                0  1
  29.                   |
  30.                   2
  31.                   |
  32.                   1
  33.                   |
  34.                   8
  35.                   |
  36.                   8
  37.                   |
  38.                   0
  39.  
  40. The valid paths (they can be subarrays or subpaths in a vertical path)
  41.     0
  42.  
  43.     1 1
  44.  
  45.         2
  46.     1 1 2
  47.  
  48.       1 2 1
  49.  
  50.             8
  51.       1 2 1 8
  52.  
  53.               8
  54.             8 8
  55.       1 2 1 8 8
  56.  
  57.                 0
  58.               8 0
  59.             8 8 0
  60.       1 2 1 8 8 0            
  61.  
  62. total 14 valid subpaths
  63.  
  64.  
  65. this can be solved by simple dfs
  66. to know the current valid subpaths
  67. we just need to know how many times the same reminder came as similar to current reminder
  68. we can add that to the whole sum
  69.  
  70. */
  71.  
  72. /*
  73.  
  74. //inputs for m and node values and tree connections
  75. 2
  76. 1 0 1 2 1 8 8 0
  77. 8 7
  78. 0 1
  79. 0 2
  80. 2 3
  81. 3 4
  82. 4 5
  83. 5 6
  84. 6 7
  85.  
  86. expected output -
  87. The result is 14
  88.  
  89.  
  90. */
  91.  
  92.  
  93. @SuppressWarnings({"unused","unchecked"})
  94. public class A20250305b_meesho  {
  95.  
  96.     static Scanner sc = new Scanner(System.in);
  97.  
  98.     private static int[] getArray() {
  99.         String[] sArr = sc.nextLine().split(" ");
  100.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  101.         return arr;
  102.     }
  103.  
  104.     private static char[] getCharArray() {
  105.         String[] sArr = sc.nextLine().split(" ");
  106.         char[] cArr = new char[sArr.length];
  107.         for (int i = 0; i < sArr.length; i++) {
  108.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  109.         }
  110.         return cArr;
  111.     }
  112.  
  113.     private static int getMax(int[] arr) {
  114.         int currMax = Integer.MIN_VALUE;
  115.         for (int curr : arr) {
  116.             currMax = Math.max(currMax, curr);
  117.         }
  118.         return currMax;
  119.     }
  120.  
  121.     public static void getCount(ArrayList<Integer>[] adjList,
  122.     int[] a,int currInd,int[] visited,int[] nodeValues,HashMap<Integer,Integer> remfMap,int m){
  123.         int currVal = nodeValues[currInd];
  124.         a[1] += currVal; //currSum
  125.         int currReminder = a[1] % m;
  126.         int currFreq = remfMap.getOrDefault(currReminder,0);
  127.         a[0] += currFreq; //storing the answer
  128.  
  129.         currFreq += 1;//adding curr to the answer
  130.         remfMap.put(currReminder,currFreq);
  131.         visited[currInd] = 1;
  132.         for(int child:adjList[currInd]){
  133.             if(visited[child]==1)
  134.                 continue;
  135.             getCount(adjList,a,child,visited,nodeValues,remfMap,m);
  136.  
  137.         }
  138.  
  139.         visited[currInd] = 0;
  140.         currFreq -= 1;
  141.         remfMap.put(currReminder,currFreq);
  142.         a[1] -= currVal;//adjusting the currSum
  143.  
  144.         return;
  145.  
  146.     }
  147.  
  148.    
  149.     public static void main(String args[]) {
  150.         // prepare the inputs
  151.  
  152.         int m = getArray()[0];
  153.         int[] nodeValues = getArray();
  154.         int[] tempArr = getArray();
  155.  
  156.         int nodes = tempArr[0];
  157.         int edges = tempArr[1];
  158.         ArrayList<Integer>[] adjList =(ArrayList<Integer>[]) new ArrayList[nodes];//explicit type conversion
  159.         for(int i =0;i<nodes;i+=1){
  160.             adjList[i] = new ArrayList<>();
  161.         }
  162.         int temp = edges;
  163.         //fill up the graph
  164.         while(temp!=0){
  165.             temp-=1;
  166.             tempArr = getArray();
  167.             int n1 = tempArr[0];
  168.             int n2 = tempArr[1];
  169.             adjList[n1].add(n2);
  170.             adjList[n2].add(n1);
  171.         }
  172.         int[] ans = new int[2]; //0th ind is for final answer , 1st ind is currsum
  173.         int[] visited = new int[nodes];
  174.         HashMap<Integer,Integer> remfMap = new HashMap<>();//to store frequency of current reminder
  175.         remfMap.put(0,1);//default to put ,as empty subarray is a valid subarray or subpath
  176.         getCount(adjList,ans,0,visited,nodeValues,remfMap,m);
  177.         int res = ans[0];
  178.  
  179.  
  180.         System.out.println("The result is " + res);
  181.  
  182.     }
  183. }
  184.  
  185. class Pair{
  186.     int row;
  187.     int col;
  188.     public Pair(int i,int j){
  189.         this.row = i;
  190.         this.col = j;
  191.        
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement