Advertisement
saurav_kalsoor

Order Prosperity - JAVA

Jun 22nd, 2022
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Pair{
  4.     int ff;
  5.     int ss;
  6.     public Pair(int f,int s) {
  7.         ff = f;
  8.         ss = s;
  9.     }
  10. }
  11.  
  12. class CustomSort implements Comparator<Pair>{
  13.  
  14.     @Override
  15.     public int compare(Pair a, Pair b) {
  16.         if(a.ss!=b.ss) {
  17.             return b.ss-a.ss;
  18.         }
  19.         return b.ff-a.ff;
  20.     }
  21.    
  22. }
  23.  
  24.  
  25. public class Solution {
  26.    
  27.     static Scanner sc = new Scanner(System.in);
  28.  
  29.     public static void dfsUtil(ArrayList<Integer>[] A,int src,int par,int[] citiesCanBeVisited) {
  30.         int ans = 0;
  31.        
  32.         for(int i : A[src]) {
  33.             if(i!=par) {
  34.                 dfsUtil(A,i,src,citiesCanBeVisited);
  35.                 ans+=(citiesCanBeVisited[i]+1);
  36.             }
  37.         }
  38.         citiesCanBeVisited[src] = ans;
  39.     }
  40.    
  41.     public static ArrayList<Integer> orderProsperity(int N, ArrayList<Integer>[] A){
  42.         ArrayList<Integer> result = new ArrayList<>();
  43.        
  44.         int[] citiesVisited = new int[N+1];
  45.         for(int i=0;i<=N;i++) {
  46.             citiesVisited[i] = -1;
  47.         }
  48.        
  49.         Queue<Integer> q = new LinkedList<>();
  50.         q.add(1);
  51.         citiesVisited[1] = 0;
  52.        
  53.         while(!q.isEmpty()) {
  54.             int temp = q.remove();
  55.             for(int i : A[temp]) {
  56.                 if(citiesVisited[i] == -1) {
  57.                     citiesVisited[i] = citiesVisited[temp]+1;
  58.                     q.add(i);
  59.                 }
  60.             }
  61.         }
  62.        
  63.         int[] citiesCanBeVisited = new int[N+1];
  64.         for(int i=0;i<=N;i++) {
  65.             citiesCanBeVisited[i] = 0;
  66.         }
  67.        
  68.         dfsUtil(A,1,-1,citiesCanBeVisited);
  69.                
  70.         ArrayList<Pair> dummy = new ArrayList<>();
  71.        
  72.         for(int i=1;i<=N;i++) {
  73.             dummy.add(new Pair(i,citiesCanBeVisited[i] * citiesVisited[i]));
  74.         }
  75.        
  76.         Collections.sort(dummy, new CustomSort());
  77.        
  78.        
  79.         for(Pair i : dummy) {
  80.             result.add(i.ff);
  81.         }
  82.        
  83.         return result;
  84.     }
  85.    
  86.     public static void main(String[] args) {
  87.         int N;
  88.         N = sc.nextInt();
  89.        
  90.         ArrayList<Integer>[] A = new ArrayList[N+1];
  91.        
  92.         for(int i=0;i<=N;i++) A[i] = new ArrayList<>();
  93.        
  94.         for(int i=0;i<N-1;i++) {
  95.             int temp1 = sc.nextInt();
  96.             int temp2 = sc.nextInt();
  97.            
  98.             A[temp1].add(temp2);
  99.             A[temp2].add(temp1);
  100.         }
  101.        
  102.         ArrayList<Integer> result= orderProsperity(N,A);
  103.        
  104.         for(int i : result) {
  105.             System.out.print(i + " ");
  106.         }
  107.        
  108.         System.out.println();
  109.     }
  110. }
  111.  
  112.  
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement