Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.IntStream;
- public class GraphBase {
- public static void main(String[] args) {
- new GraphBase();
- }
- int x, y;
- Stack<V> Q;
- GraphBase() {
- Scanner W = new Scanner(System.in);
- int a = W.nextInt(), ma = W.nextInt();
- V[] ar = new V[a];
- for (int v = 0; v < a; v++) {
- ar[v] = new V(v);
- }
- while (--ma >= 0) {
- ar[W.nextInt()].oS.add(ar[W.nextInt()]);
- }
- x = y = 1;
- Q = new Stack<>();
- for (V s : ar) {
- if (s .sB == 0) {
- TV(s );
- }
- }
- y--;
- boolean[] aQ = new boolean[y];
- int[] tz = new int[y];
- Arrays.fill(tz, Integer.MAX_VALUE);
- for (V L : ar) {
- tz[ L .z - 1] = Math.min(tz[ L .z - 1], L .K);
- for (V R : L .oS) {
- if ( L .z != R .z) {
- aQ[ R .z - 1] = true;
- }
- }
- }
- IntStream.range(0, y).filter(gu7 -> !aQ[gu7]).forEach(gu7 -> System.out.println(tz[gu7] + " "));
- }
- class V {
- int K, sB, z, oM;
- ArrayList<V> oS;
- V(int ag6) {
- K = ag6;
- sB = 0;
- z = 0;
- oS = new ArrayList<>();
- }
- }
- void TV(V ag) {
- ag.sB = ag.oM = x;
- x++;
- Q.push(ag);
- for (V pm : ag.oS) {
- if (pm.sB == 0) {
- TV(pm);
- }
- if (pm.z == 0 && ag.oM > pm.oM) {
- ag.oM = pm.oM;
- }
- }
- if (ag.sB == ag.oM) {
- V mh;
- do {
- mh = Q.pop();
- mh.z = y;
- } while (mh != ag);
- y++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement