Untitled
unknown
plain_text
a year ago
3.0 kB
5
Indexable
#1 4 2 4 5 9 #2 -1 #3 2 2 4 #4 -1 #5 -1 #6 16 2 4 6 8 9 11 14 15 16 17 19 20 21 22 23 25 #7 23 4 5 7 11 13 15 17 19 20 21 22 25 26 27 28 31 32 33 35 36 38 40 45 #8 67 22 27 38 40 48 52 53 59 63 65 69 70 72 74 76 80 85 92 93 99 102 110 111 115 116 120 121 123 124 132 134 136 139 140 143 148 149 151 153 154 156 157 159 161 162 164 165 166 167 168 171 173 174 178 179 180 181 183 186 187 188 189 193 197 198 199 200 #9 141 2 3 5 8 9 10 13 15 18 22 24 25 26 30 32 33 38 41 42 46 47 48 49 54 56 57 58 63 65 67 69 71 72 74 76 77 80 82 87 90 92 95 97 98 100 104 105 106 107 108 109 113 114 118 119 121 125 127 132 134 141 142 143 144 148 150 155 156 157 160 161 162 163 167 168 169 170 172 173 177 178 179 180 181 184 185 186 187 196 197 199 200 204 205 206 209 214 217 219 221 226 227 230 231 233 236 238 241 242 243 247 249 252 253 254 255 257 258 261 263 264 265 269 270 271 272 276 277 281 282 285 287 288 291 292 293 294 295 296 297 300 #10 155 2 3 4 6 7 8 12 13 14 16 22 25 28 29 30 31 32 34 35 39 42 43 44 51 53 54 55 57 58 59 61 62 63 64 70 71 73 76 81 82 83 87 88 89 91 93 96 97 100 101 102 105 112 113 114 116 117 118 120 121 122 123 124 125 126 129 132 133 135 136 138 139 141 142 145 147 149 150 151 154 155 163 167 171 172 173 174 177 178 179 181 184 185 187 194 196 197 198 199 200 201 203 204 205 206 207 212 213 214 215 217 220 223 224 225 228 229 230 232 234 236 238 240 244 245 246 247 251 254 255 257 258 259 261 262 263 264 265 266 268 269 272 273 274 278 279 281 283 285 290 291 292 296 299 300 public class Solution { static int[][] m; static int[] p; static int n, e, res; static Queue<Integer> q; public static void main(String[] args) throws Exception { System.setIn(new FileInputStream("src/Bipition/input.txt")); Scanner sc = new Scanner(System.in); for (int tc = 1; tc <= 10; tc++) { n = sc.nextInt(); e = sc.nextInt(); res = 0; m = new int[n + 1][n + 1]; p = new int[n + 1]; q = new Queue<>(); // Read edges for (int i = 1; i <= e; i++) { int u = sc.nextInt(); int v = sc.nextInt(); m[u][v] = 1; m[v][u] = 1; } BFS(); int dem = 0; for (int i = 1; i <= n; i++) { if (p[i] == 2) dem++; } if (res != -1) { System.out.print("#" + tc + " " + dem); for (int i = 1; i <= n; i++) { if (p[i] == 2) { System.out.print(" " + i); } } System.out.println(); } else { System.out.println("#" + tc + " " + res); } } } private static void BFS() throws Exception { q.reset(); for (int i = 1; i <= n; i++) { if (p[i] == 0) { q.enQueue(i); p[i] = 1; while (!q.isEmpty()) { int u = q.peek(); q.deQueue(); int k = p[u]; int k1 = 3 - k; for (int j = 1; j <= n; j++) { if (m[u][j] == 1) { if (p[j] == 0) { p[j] = k1; q.enQueue(j); } else { if (p[j] == k) { res = -1; break; } } } } if (res == -1) break; } } } } }
Editor is loading...
Leave a Comment