|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | + |
| 7 | +class Main { |
| 8 | + |
| 9 | + // IO field |
| 10 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + static StringTokenizer st; |
| 13 | + |
| 14 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 15 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 16 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 17 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 18 | + |
| 19 | + // Additional field |
| 20 | + static int N; |
| 21 | + static boolean[][] know; |
| 22 | + static int[] vis; |
| 23 | + static boolean res = true; |
| 24 | + |
| 25 | + public static void main(String[] args) throws Exception { |
| 26 | + |
| 27 | + ready(); |
| 28 | + solve(); |
| 29 | + |
| 30 | + bwEnd(); |
| 31 | + } |
| 32 | + |
| 33 | + static void ready() throws Exception{ |
| 34 | + |
| 35 | + N = Integer.parseInt(br.readLine()); |
| 36 | + know = new boolean[N+1][N+1]; |
| 37 | + vis = new int[N+1]; |
| 38 | + nextLine(); |
| 39 | + for(int a = nextInt(), b = nextInt();a != -1;) { |
| 40 | + know[a][b] = true; |
| 41 | + know[b][a] = true; |
| 42 | + nextLine(); |
| 43 | + a = nextInt(); |
| 44 | + b = nextInt(); |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + static void solve() throws Exception{ |
| 50 | + |
| 51 | + for(int i=1;i<=N;i++) if(vis[i] == 0) { |
| 52 | + vis[i] = 1; |
| 53 | + dfs(i,1); |
| 54 | + } |
| 55 | + if(!res) bw.write("-1"); |
| 56 | + else { |
| 57 | + List<Integer> A = new ArrayList<>(); |
| 58 | + List<Integer> B = new ArrayList<>(); |
| 59 | + for(int i=1;i<=N;i++) if(vis[i] == 1) A.add(i); else B.add(i); |
| 60 | + bw.write("1\n"); |
| 61 | + for(int i:A) bw.write(i+" "); |
| 62 | + bw.write("-1\n"); |
| 63 | + for(int i:B) bw.write(i+" "); |
| 64 | + bw.write("-1\n"); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + static void dfs(int n, int team) throws Exception { |
| 70 | + if(!res) return; |
| 71 | + List<Integer> next = new ArrayList<>(); |
| 72 | + int opposite = team == 1 ? 2 : 1; |
| 73 | + for(int i=1;i<=N;i++) { |
| 74 | + if(know[n][i] || i == n) continue; |
| 75 | + if(vis[i] == 0) { |
| 76 | + vis[i] = opposite; |
| 77 | + next.add(i); |
| 78 | + continue; |
| 79 | + } |
| 80 | + else if(vis[i] == opposite) continue; |
| 81 | + res = false; |
| 82 | + return; |
| 83 | + } |
| 84 | + for(int i:next) { |
| 85 | + dfs(i,opposite); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +``` |
0 commit comments