You are viewing a single comment's thread. Return to all comments →
can anyone please explain me how is it working?
import java.util.Arrays; import java.util.Scanner; public class KittysCalc { public static final long constant = 1000000007; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int queries = sc.nextInt(); int[] parents = new int[n+1]; long[] children = new long[n+1]; boolean[] valuesSet = new boolean[n+1]; long valuesSum = 0; long sum = 0; int a, b; for(int i = 0; i < n-1; i++) { a = sc.nextInt(); b = sc.nextInt(); if(a < b) { parents[b] = a; } else { parents[a] = b; } } parents[1] = 0; // for(int item : parents){ // System.out.print(item+", "); // } // System.out.println(); for(int i = 0; i < queries; i++) { int k = sc.nextInt(); Arrays.fill(valuesSet, false); Arrays.fill(children, 0); valuesSum = 0; for(int j = 0; j < k; j++) { a = sc.nextInt(); valuesSum += a; valuesSet[a] = true; } sum = 0; for (int j = n; j > 0; j--) { long c = children[j]; if (valuesSet[j]) { c += j; } long x = 0; if (c > 0) { x = c * (valuesSum - c); sum += x; } System.out.println("x-> "+x); children[parents[j]] += c; System.out.println("childs - "); for(long item : children){ System.out.print(item+", "); } System.out.println("j-> "+j); } System.out.println(sum); } sc.close(); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Kitty's Calculations on a Tree
You are viewing a single comment's thread. Return to all comments →
can anyone please explain me how is it working?