import java.util.*; public class kon { public static void main(String[] args) { Scanner s = new Scanner(System.in); int nodes = s.nextInt(); int edges = s.nextInt(); List<Integer>[] graph = new List[nodes]; List<Integer> empty = new ArrayList<>(); Arrays.fill(graph, empty); for (int i = 0; i < edges; i++) { int from = s.nextInt() - 1, to = s.nextInt() - 1; List<Integer> destinations = graph[from]; if (destinations == empty) { destinations = new ArrayList<>(); graph[from] = destinations; } destinations.add(to); } List<List<Integer>> scc = new Tarjan().getSCComponents(graph); if (scc.isEmpty()) { System.out.println("NIE"); } else if (scc.size() > 1) { System.out.println("0"); } else { List<Integer> c = scc.get(0); Set<Integer> anotherName = new HashSet<>(c); toCheck = new HashSet<>(c); candidates = new HashSet<>(c); while (!toCheck.isEmpty()) { blocked = new boolean[graph.length]; are = new List[graph.length]; stack = new ArrayList<>(); for (int i = 0; i < nodes; i++) { are[i] = new ArrayList<>(); } int i = toCheck.iterator().next(); findCycles(i, i, graph, anotherName); } if (candidates.isEmpty()) { System.out.println("0"); } else { System.out.println(candidates.size()); List<Integer> sorted = new ArrayList<>(candidates); Collections.sort(sorted); StringJoiner sj = new StringJoiner(" "); for (int i : sorted) { sj.add(Integer.toString(i + 1)); } System.out.println(sj); } } } static Set<Integer> candidates; static Set<Integer> toCheck; static List<Integer> stack; static boolean[] blocked; static List[] are; private static boolean findCycles(int v, int s, List<Integer>[] graph, Set<Integer> scc) { boolean f = false; stack.add(v); blocked[v] = true; for (int w : graph[v]) { if (!scc.contains(w)) continue; if (w == s) { List<Integer> cycle = new ArrayList<>(); for (int j = 0; j < stack.size(); j++) { int index = stack.get(j); cycle.add(index); toCheck.remove(index); } candidates.retainAll(cycle); f = true; } else if (!blocked[w]) { if (findCycles(w, s, graph, scc)) { f = true; } } } if (f) { unblock(v); } else { for (int i = 0; i < graph[v].size(); i++) { int w = graph[v].get(i); if (!are[w].contains(v)) { are[w].add(v); } } } stack.remove((Object) v); return f; } static private void unblock(int node) { blocked[node] = false; List Bnode = are[node]; while (Bnode.size() > 0) { Integer w = (Integer) Bnode.get(0); Bnode.remove(0); if (blocked[w]) { unblock(w); } } } static class Tarjan { private int V; private int preCount; private int[] low; private boolean[] visited; private List<Integer>[] graph; private List<List<Integer>> sccComp; private Stack<Integer> stackTar; public List<List<Integer>> getSCComponents(List<Integer>[] graph) { V = graph.length; this.graph = graph; low = new int[V]; visited = new boolean[V]; stackTar = new Stack<Integer>(); sccComp = new ArrayList<>(); for (int v = 0; v < V; v++) if (!visited[v]) dfs(v); return sccComp; } public void dfs(int v) { low[v] = preCount++; visited[v] = true; stackTar.push(v); int min = low[v]; for (int w : graph[v]) { if (!visited[w]) dfs(w); if (low[w] < min) min = low[w]; } if (min < low[v]) { low[v] = min; return; } List<Integer> component = new ArrayList<>(); int w; do { w = stackTar.pop(); component.add(w); low[w] = V; } while (w != v); if (component.size() > 1) sccComp.add(component); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import java.util.*; public class kon { public static void main(String[] args) { Scanner s = new Scanner(System.in); int nodes = s.nextInt(); int edges = s.nextInt(); List<Integer>[] graph = new List[nodes]; List<Integer> empty = new ArrayList<>(); Arrays.fill(graph, empty); for (int i = 0; i < edges; i++) { int from = s.nextInt() - 1, to = s.nextInt() - 1; List<Integer> destinations = graph[from]; if (destinations == empty) { destinations = new ArrayList<>(); graph[from] = destinations; } destinations.add(to); } List<List<Integer>> scc = new Tarjan().getSCComponents(graph); if (scc.isEmpty()) { System.out.println("NIE"); } else if (scc.size() > 1) { System.out.println("0"); } else { List<Integer> c = scc.get(0); Set<Integer> anotherName = new HashSet<>(c); toCheck = new HashSet<>(c); candidates = new HashSet<>(c); while (!toCheck.isEmpty()) { blocked = new boolean[graph.length]; are = new List[graph.length]; stack = new ArrayList<>(); for (int i = 0; i < nodes; i++) { are[i] = new ArrayList<>(); } int i = toCheck.iterator().next(); findCycles(i, i, graph, anotherName); } if (candidates.isEmpty()) { System.out.println("0"); } else { System.out.println(candidates.size()); List<Integer> sorted = new ArrayList<>(candidates); Collections.sort(sorted); StringJoiner sj = new StringJoiner(" "); for (int i : sorted) { sj.add(Integer.toString(i + 1)); } System.out.println(sj); } } } static Set<Integer> candidates; static Set<Integer> toCheck; static List<Integer> stack; static boolean[] blocked; static List[] are; private static boolean findCycles(int v, int s, List<Integer>[] graph, Set<Integer> scc) { boolean f = false; stack.add(v); blocked[v] = true; for (int w : graph[v]) { if (!scc.contains(w)) continue; if (w == s) { List<Integer> cycle = new ArrayList<>(); for (int j = 0; j < stack.size(); j++) { int index = stack.get(j); cycle.add(index); toCheck.remove(index); } candidates.retainAll(cycle); f = true; } else if (!blocked[w]) { if (findCycles(w, s, graph, scc)) { f = true; } } } if (f) { unblock(v); } else { for (int i = 0; i < graph[v].size(); i++) { int w = graph[v].get(i); if (!are[w].contains(v)) { are[w].add(v); } } } stack.remove((Object) v); return f; } static private void unblock(int node) { blocked[node] = false; List Bnode = are[node]; while (Bnode.size() > 0) { Integer w = (Integer) Bnode.get(0); Bnode.remove(0); if (blocked[w]) { unblock(w); } } } static class Tarjan { private int V; private int preCount; private int[] low; private boolean[] visited; private List<Integer>[] graph; private List<List<Integer>> sccComp; private Stack<Integer> stackTar; public List<List<Integer>> getSCComponents(List<Integer>[] graph) { V = graph.length; this.graph = graph; low = new int[V]; visited = new boolean[V]; stackTar = new Stack<Integer>(); sccComp = new ArrayList<>(); for (int v = 0; v < V; v++) if (!visited[v]) dfs(v); return sccComp; } public void dfs(int v) { low[v] = preCount++; visited[v] = true; stackTar.push(v); int min = low[v]; for (int w : graph[v]) { if (!visited[w]) dfs(w); if (low[w] < min) min = low[w]; } if (min < low[v]) { low[v] = min; return; } List<Integer> component = new ArrayList<>(); int w; do { w = stackTar.pop(); component.add(w); low[w] = V; } while (w != v); if (component.size() > 1) sccComp.add(component); } } } |