import java.awt.Color; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Comparator; import java.util.HashSet; import java.util.Set; public class kon { static int time = 0; static Set<City> result = null; static boolean sayNo = false; static { time = 0; sayNo = false; result = null; } private static class City { final int id; int timeIn; int timeOut; Set<City> neighbours; Color color; City parent; public City(int id) { this.id = id; neighbours = new HashSet<>(); color = Color.WHITE; parent = null; } @Override public int hashCode() { return id; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; City other = (City) obj; if (id != other.id) return false; return true; } } public static void main(String[] args) { // TODO Auto-generated method stub makeResult(); System.exit(0); } private static void makeResult() { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer st = new StreamTokenizer(bufferedReader); try { st.nextToken(); int n = (int) st.nval; st.nextToken(); int m = (int) st.nval; City[] cities = new City[n + 1]; for (int i = 1; i <= n; i++) { cities[i] = new City(i); } int a, b = 0; for (int i = 1; i <= m; i++) { st.nextToken(); a = (int) st.nval; st.nextToken(); b = (int) st.nval; cities[a].neighbours.add(cities[b]); } bufferedReader.close(); for (int i = 1; i <= n; i++) { if (cities[i].color == Color.WHITE) { dfsVisit(cities[i]); if (sayNo) { System.out.println(0); return; } } } if (result == null) { System.out.println("NIE"); return; } else { System.out.println(result.size()); result.stream().sorted(Comparator.comparing(x -> x.id)).forEach(x -> System.out.print(x.id + " ")); } } catch (Exception e) { e.printStackTrace(); } } private static void dfsVisit(City c) { c.color = Color.GRAY; c.timeIn = time + 1; time = time + 1; City ancestor = null; for (City city : c.neighbours) { // main procedure if (city.color == Color.GRAY && ((ancestor == null) || (ancestor.timeIn < city.timeIn))) { ancestor = city; } } if (ancestor != null) { // there is a cycle Set<City> cycle = new HashSet<>(); cycle.add(ancestor); City tmp = c; while (tmp != ancestor) { cycle.add(tmp); tmp = tmp.parent; } if (result == null) { result = cycle; } else { result.retainAll(cycle); if (result.isEmpty()) { sayNo = true; return; } } } for (City city : c.neighbours) { // main procedure if (city.color == Color.WHITE) { city.parent = c; dfsVisit(city); if (sayNo) { return; } } } // here, case when I foun black one!?!?!? c.color = Color.BLACK; c.timeOut = time + 1; } }
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 | import java.awt.Color; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Comparator; import java.util.HashSet; import java.util.Set; public class kon { static int time = 0; static Set<City> result = null; static boolean sayNo = false; static { time = 0; sayNo = false; result = null; } private static class City { final int id; int timeIn; int timeOut; Set<City> neighbours; Color color; City parent; public City(int id) { this.id = id; neighbours = new HashSet<>(); color = Color.WHITE; parent = null; } @Override public int hashCode() { return id; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; City other = (City) obj; if (id != other.id) return false; return true; } } public static void main(String[] args) { // TODO Auto-generated method stub makeResult(); System.exit(0); } private static void makeResult() { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer st = new StreamTokenizer(bufferedReader); try { st.nextToken(); int n = (int) st.nval; st.nextToken(); int m = (int) st.nval; City[] cities = new City[n + 1]; for (int i = 1; i <= n; i++) { cities[i] = new City(i); } int a, b = 0; for (int i = 1; i <= m; i++) { st.nextToken(); a = (int) st.nval; st.nextToken(); b = (int) st.nval; cities[a].neighbours.add(cities[b]); } bufferedReader.close(); for (int i = 1; i <= n; i++) { if (cities[i].color == Color.WHITE) { dfsVisit(cities[i]); if (sayNo) { System.out.println(0); return; } } } if (result == null) { System.out.println("NIE"); return; } else { System.out.println(result.size()); result.stream().sorted(Comparator.comparing(x -> x.id)).forEach(x -> System.out.print(x.id + " ")); } } catch (Exception e) { e.printStackTrace(); } } private static void dfsVisit(City c) { c.color = Color.GRAY; c.timeIn = time + 1; time = time + 1; City ancestor = null; for (City city : c.neighbours) { // main procedure if (city.color == Color.GRAY && ((ancestor == null) || (ancestor.timeIn < city.timeIn))) { ancestor = city; } } if (ancestor != null) { // there is a cycle Set<City> cycle = new HashSet<>(); cycle.add(ancestor); City tmp = c; while (tmp != ancestor) { cycle.add(tmp); tmp = tmp.parent; } if (result == null) { result = cycle; } else { result.retainAll(cycle); if (result.isEmpty()) { sayNo = true; return; } } } for (City city : c.neighbours) { // main procedure if (city.color == Color.WHITE) { city.parent = c; dfsVisit(city); if (sayNo) { return; } } } // here, case when I foun black one!?!?!? c.color = Color.BLACK; c.timeOut = time + 1; } } |