Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.IntStream;
  3.  
  4. public class GraphBase {
  5.  
  6.     public static void main(String[] args) {
  7.         new GraphBase();
  8.     }
  9.  
  10.     int x, y;
  11.     Stack<V> Q;
  12.  
  13.     GraphBase() {
  14.         Scanner W = new Scanner(System.in);
  15.         int a = W.nextInt(), ma = W.nextInt();
  16.         V[] ar = new V[a];
  17.         for (int v = 0; v < a; v++) {
  18.             ar[v] = new V(v);
  19.         }
  20.         while (--ma >= 0) {
  21.             ar[W.nextInt()].oS.add(ar[W.nextInt()]);
  22.         }
  23.         x = y = 1;
  24.         Q = new Stack<>();
  25.         for (V s   : ar) {
  26.             if (s  .sB == 0) {
  27.                 TV(s  );
  28.             }
  29.         }
  30.         y--;
  31.         boolean[]  aQ = new boolean[y];
  32.         int[] tz = new int[y];
  33.         Arrays.fill(tz, Integer.MAX_VALUE);
  34.         for (V L  : ar) {
  35.             tz[ L .z - 1] = Math.min(tz[ L .z - 1],  L .K);
  36.             for (V R   :  L .oS) {
  37.                 if ( L .z !=  R  .z) {
  38.                     aQ[ R  .z - 1] = true;
  39.                 }
  40.             }
  41.         }
  42.         IntStream.range(0, y).filter(gu7 -> !aQ[gu7]).forEach(gu7 -> System.out.println(tz[gu7] + " "));
  43.     }
  44.  
  45.     class V {
  46.  
  47.         int K, sB, z, oM;
  48.         ArrayList<V> oS;
  49.  
  50.         V(int ag6) {
  51.             K = ag6;
  52.             sB = 0;
  53.             z = 0;
  54.             oS = new ArrayList<>();
  55.         }
  56.  
  57.     }
  58.  
  59.     void TV(V ag) {
  60.         ag.sB = ag.oM = x;
  61.         x++;
  62.         Q.push(ag);
  63.         for (V pm : ag.oS) {
  64.             if (pm.sB == 0) {
  65.                 TV(pm);
  66.             }
  67.             if (pm.z == 0 && ag.oM > pm.oM) {
  68.                 ag.oM = pm.oM;
  69.             }
  70.         }
  71.         if (ag.sB == ag.oM) {
  72.             V mh;
  73.             do {
  74.                 mh = Q.pop();
  75.                 mh.z = y;
  76.             } while (mh != ag);
  77.             y++;
  78.         }
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement