CodeChef : January Challenge 2017 – Capital Movement

By | January 10, 2017
Capital Movement

Source: CodeChef

Capital Movement: Chef is playing a video game. In a video game, there’s a advanced civilization that has a total of N planets under control. All of those planets are connected with N-1 teleports in such a way, that it’s possible to travel between any two planets using those teleports.

There’s a chance that some planet gets infected. In this case it takes 24 hours for civilization to find out infection and prevent it from spreading. During this time infection uses teleport one time and infect all the planets that can be achieved in one teleport jump. So, once infection is detected at planet V, scientists already know that all planets connected to V via teleport are also infected. All the neccessary teleports are disabled right away and medics start working on eliminating the infection.

Each planet has population. Planets are numbered from 1 to N and their populations are P1, P2, …, PN. It is known that all the Pi are distinct.

There’s a capital among all those planets. The capital is known to have the biggest population.

Once infection is detected at planet V, after disabling teleports on planet V and all connected to them, government has to establish a new capital if needed in the remaining not-infected planets. So, they list all the planets that are not connected to V and are not V. Then they pick the planet with biggest population. Your task is to find the number of this planet for every possible V.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains one integer N.

Next line contains N space-separated integers P1, P2, …, PN denoting the population of each planet.

Next N-1 lines contain two space-separated integers each V and U denoting that there’s a teleport between planet V and U.

Output

For each test case, output a single line containing N integers A1, A2, …, AN separated by a space. Here Aidenotes the number of the planet picked to be new capital in case infection starts spreading from the planet i. In case infection affects all the planets output 0.

Constraints

  • 1T5 , 1N50000  , 1Pi109
  • All Pi are distinct
  • 1VN ;  1UN

Example

Input:
1
6
5 10 15 20 25 30
1 3
2 3
3 4
4 5
4 6

Output:
6 6 6 2 6 5
Solution:

Code is passing first set of sub tasks.
However second and third set of tasks are having TLE.
I am using 2d array to keep track of the nodes of the map.
Please comment for any suggestions or Hints.

My Code:

import java.util.Scanner;
public class CapitalMovement {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		for (int i = 0; i < t; i++) {
			int n = scan.nextInt();
			int p[] = new int[n];
			for (int j = 0; j < n; j++)
				p[j] = scan.nextInt();
			int qq[] = p;

			int dArr[][] = new int[n][n];

			for (int in = 0; in < n; in++)
				for (int on = 0; on < n; on++) {
					if (in == on)
						dArr[in][on] = -1;
					else
						dArr[in][on] = -2;
				}

			for (int k = 0; k < n - 1; k++) {
				int a = scan.nextInt();
				int b = scan.nextInt();

				dArr[a - 1][b - 1] = 0;
				dArr[b - 1][a - 1] = 0;

			} // all inputs taken till here

			/*
			 * for(int in=0;in<n;in++) { System.out.println(); for(int
			 * on=0;on<n;on++) System.out.print(dArr[in][on]+" "); }
			 */
			// now traverse 2d arr

			for (int h = 0; h < n; h++) {
				int max = -1, maxIndex = -1;
				int tempS[] = qq;

				for (int gg = 0; gg < n; gg++) { if (dArr[h][gg] == -2) { if (tempS[gg] > max) {
							max = tempS[gg];
							maxIndex = gg + 1;
						}
					}
				}
				System.out.print(maxIndex + " ");
			}
		}
	}
}
/*
 * 
 * 1 6 5 10 15 20 25 30 1 3 2 3 3 4 4 5 4 6
 * 
 */