import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.StringTokenizer; import java.util.TreeSet; public class kon { static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>(); static HashMap<Integer, HashSet<Integer>> mapT = new HashMap<Integer, HashSet<Integer>>(); static HashMap<Integer, HashSet<Integer>> mapS = new HashMap<Integer, HashSet<Integer>>(); static int n, m; static boolean[] visited; static int[] cycled; static int nr = 0; static int[] nrs; static int[] silaIn; static int[] silaOut; static HashMap<Integer, HashSet<Integer>> sss = new HashMap<Integer, HashSet<Integer>>(); public static boolean cycle(int node, int ex) { // System.out.println("cycle(" + node + "," + ex); cycled[node] = 1; for(Integer neigh : mapS.get(node)) { // System.out.println("neigh" + neigh + " " + cycled[neigh]); if(neigh == ex) { continue; } if(cycled[neigh] == 1) { // System.out.println("true"); return true; } if(cycled[neigh] == 0) { if(cycle(neigh, ex)) { return true; } } } cycled[node] = 2; return false; } public static void visit(int node) { visited[node] = true; for(Integer neigh : map.get(node)) { if(!visited[neigh]) { visit(neigh); } } // System.out.println(node); nrs[nr] = node; // System.out.println("post" + node); nr++; } public static void visit2(int node, int sssNum) { visited[node] = true; // System.out.println("node" + node); for(Integer neigh : mapT.get(node)) { if(!visited[neigh]) { visit2(neigh, sssNum); } } sss.get(sssNum).add(node); } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer; tokenizer = new StringTokenizer(reader.readLine()); n = Integer.parseInt(tokenizer.nextToken()); m = Integer.parseInt(tokenizer.nextToken()); visited = new boolean[n]; nrs = new int[n]; silaIn = new int[n]; silaOut = new int[n]; cycled = new int[n]; for(int i = 0; i < n; i++) { silaIn[i] = 0; silaOut[i] = 0; visited[i] = false; map.put(i, new HashSet<Integer>()); mapT.put(i, new HashSet<Integer>()); } for(int i = 0; i < m; i++) { tokenizer = new StringTokenizer(reader.readLine()); int a = Integer.parseInt(tokenizer.nextToken()); int b = Integer.parseInt(tokenizer.nextToken()); map.get(a-1).add(b-1); mapT.get(b-1).add(a-1); } for(int i = 0; i < n; i++) { if(!visited[i]) { visit(i); } } for(int i = 0; i < n; i++) visited[i] = false; int sssCnt = 0; for(int i = 0; i < n; i++) { if(!visited[nrs[n-1-i]]) { sss.put(sssCnt, new HashSet<Integer>()); visit2(nrs[n-1-i], sssCnt); sssCnt++; } } HashSet<Integer> s = null; for(HashSet<Integer> ss : sss.values()) { if(ss.size() > 1) { if(s == null) { s = ss; } else { System.out.println("NIE"); System.exit(0); } } } if(s == null) { System.out.println("NIE"); System.exit(0); } int silaO = 0; int silaI = 0; for(Integer node : s) { // silaOut[node] = map.get(node).size(); for(Integer neigh : map.get(node)) { if(s.contains(neigh)) { silaOut[node]++; silaO++; silaIn[neigh]++; silaI++; } } // System.out.println("s elem " + node + " so=" + silaOut[node] + " si=" + silaIn[node]); } // System.out.println(s.size() + " " + silaO + " " + silaI); for(Integer node : s) { mapS.put(node, new HashSet<Integer>()); } for(Integer node : s) { for(Integer neigh : map.get(node)) { if(s.contains(neigh)) { mapS.get(node).add(neigh); } } } HashSet<Integer> ret = new HashSet<Integer>(); int cnt = 0; for(Integer node : s) { // TODO: zbyt brutalne // System.out.println((cnt++) + "/" + s.size()); System.out.flush(); if(silaO - silaOut[node] - silaIn[node] <= s.size()-1-1) { ret.add(node); } else { for(Integer i : s) cycled[i] = 0; boolean cycle = false; for(Integer i : s) { // System.out.print(" " + i); if(i == node) { continue; } if(cycled[i] == 0) { if(cycle(i, node)) { // System.out.println("cycle" + i + "," + node); cycle = true; // System.out.println("breakinf on node " + node); break; } } } if(!cycle) { // System.out.println(" no cycle without " + node); ret.add(node); } } // for(Integer i : s) visited[i] = false; //if(!cycle(node)) { // ret.add(node); //} } System.out.println(ret.size()); StringBuilder sb = new StringBuilder(); boolean emptySb = true; for(Integer cross : ret) { if(!emptySb) { sb.append(" "); } sb.append(cross+1); emptySb = false; } System.out.println(sb.toString()); System.exit(0); } }
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.StringTokenizer; import java.util.TreeSet; public class kon { static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>(); static HashMap<Integer, HashSet<Integer>> mapT = new HashMap<Integer, HashSet<Integer>>(); static HashMap<Integer, HashSet<Integer>> mapS = new HashMap<Integer, HashSet<Integer>>(); static int n, m; static boolean[] visited; static int[] cycled; static int nr = 0; static int[] nrs; static int[] silaIn; static int[] silaOut; static HashMap<Integer, HashSet<Integer>> sss = new HashMap<Integer, HashSet<Integer>>(); public static boolean cycle(int node, int ex) { // System.out.println("cycle(" + node + "," + ex); cycled[node] = 1; for(Integer neigh : mapS.get(node)) { // System.out.println("neigh" + neigh + " " + cycled[neigh]); if(neigh == ex) { continue; } if(cycled[neigh] == 1) { // System.out.println("true"); return true; } if(cycled[neigh] == 0) { if(cycle(neigh, ex)) { return true; } } } cycled[node] = 2; return false; } public static void visit(int node) { visited[node] = true; for(Integer neigh : map.get(node)) { if(!visited[neigh]) { visit(neigh); } } // System.out.println(node); nrs[nr] = node; // System.out.println("post" + node); nr++; } public static void visit2(int node, int sssNum) { visited[node] = true; // System.out.println("node" + node); for(Integer neigh : mapT.get(node)) { if(!visited[neigh]) { visit2(neigh, sssNum); } } sss.get(sssNum).add(node); } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer; tokenizer = new StringTokenizer(reader.readLine()); n = Integer.parseInt(tokenizer.nextToken()); m = Integer.parseInt(tokenizer.nextToken()); visited = new boolean[n]; nrs = new int[n]; silaIn = new int[n]; silaOut = new int[n]; cycled = new int[n]; for(int i = 0; i < n; i++) { silaIn[i] = 0; silaOut[i] = 0; visited[i] = false; map.put(i, new HashSet<Integer>()); mapT.put(i, new HashSet<Integer>()); } for(int i = 0; i < m; i++) { tokenizer = new StringTokenizer(reader.readLine()); int a = Integer.parseInt(tokenizer.nextToken()); int b = Integer.parseInt(tokenizer.nextToken()); map.get(a-1).add(b-1); mapT.get(b-1).add(a-1); } for(int i = 0; i < n; i++) { if(!visited[i]) { visit(i); } } for(int i = 0; i < n; i++) visited[i] = false; int sssCnt = 0; for(int i = 0; i < n; i++) { if(!visited[nrs[n-1-i]]) { sss.put(sssCnt, new HashSet<Integer>()); visit2(nrs[n-1-i], sssCnt); sssCnt++; } } HashSet<Integer> s = null; for(HashSet<Integer> ss : sss.values()) { if(ss.size() > 1) { if(s == null) { s = ss; } else { System.out.println("NIE"); System.exit(0); } } } if(s == null) { System.out.println("NIE"); System.exit(0); } int silaO = 0; int silaI = 0; for(Integer node : s) { // silaOut[node] = map.get(node).size(); for(Integer neigh : map.get(node)) { if(s.contains(neigh)) { silaOut[node]++; silaO++; silaIn[neigh]++; silaI++; } } // System.out.println("s elem " + node + " so=" + silaOut[node] + " si=" + silaIn[node]); } // System.out.println(s.size() + " " + silaO + " " + silaI); for(Integer node : s) { mapS.put(node, new HashSet<Integer>()); } for(Integer node : s) { for(Integer neigh : map.get(node)) { if(s.contains(neigh)) { mapS.get(node).add(neigh); } } } HashSet<Integer> ret = new HashSet<Integer>(); int cnt = 0; for(Integer node : s) { // TODO: zbyt brutalne // System.out.println((cnt++) + "/" + s.size()); System.out.flush(); if(silaO - silaOut[node] - silaIn[node] <= s.size()-1-1) { ret.add(node); } else { for(Integer i : s) cycled[i] = 0; boolean cycle = false; for(Integer i : s) { // System.out.print(" " + i); if(i == node) { continue; } if(cycled[i] == 0) { if(cycle(i, node)) { // System.out.println("cycle" + i + "," + node); cycle = true; // System.out.println("breakinf on node " + node); break; } } } if(!cycle) { // System.out.println(" no cycle without " + node); ret.add(node); } } // for(Integer i : s) visited[i] = false; //if(!cycle(node)) { // ret.add(node); //} } System.out.println(ret.size()); StringBuilder sb = new StringBuilder(); boolean emptySb = true; for(Integer cross : ret) { if(!emptySb) { sb.append(" "); } sb.append(cross+1); emptySb = false; } System.out.println(sb.toString()); System.exit(0); } } |