Guest User

Untitled

a guest
Aug 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import java.io.OutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.PrintWriter;
  5. import java.util.Arrays;
  6. import java.util.HashMap;
  7. import java.io.InputStreamReader;
  8. import java.util.ArrayList;
  9. import java.util.StringTokenizer;
  10. import java.util.Map;
  11. import java.io.BufferedReader;
  12. import java.io.InputStream;
  13.  
  14. /**
  15. * Built using CHelper plug-in
  16. * Actual solution is at the top
  17. *
  18. * @author MaxHeap
  19. */
  20. public class Main {
  21. public static void main(String[] args) {
  22. InputStream inputStream = System.in;
  23. OutputStream outputStream = System.out;
  24. FastReader in = new FastReader(inputStream);
  25. PrintWriter out = new PrintWriter(outputStream);
  26. BreakingBad solver = new BreakingBad();
  27. solver.solve(1, in, out);
  28. out.close();
  29. }
  30.  
  31. static class BreakingBad {
  32. int[] color;
  33. Map<String, Integer> toInt;
  34. Map<Integer, String> toString;
  35. ArrayList<Integer>[] g;
  36.  
  37. public void solve(int testNumber, FastReader in, PrintWriter out) {
  38. int n = in.nextInt();
  39. color = new int[n];
  40. toInt = new HashMap<>();
  41. toString = new HashMap<>();
  42. g = new ArrayList[n];
  43. for (int i = 0; i < n; i++) {
  44. String cur = in.next();
  45. toInt.put(cur, i);
  46. toString.put(i, cur);
  47. g[i] = new ArrayList<>();
  48. }
  49.  
  50. Arrays.fill(color, -1);
  51.  
  52. int m = in.nextInt();
  53. for (int i = 0; i < m; i++) {
  54. String from = in.next();
  55. String to = in.next();
  56. int fromI = toInt.get(from);
  57. int toI = toInt.get(to);
  58. g[fromI].add(toI);
  59. g[toI].add(fromI);
  60. }
  61.  
  62. boolean ans = true;
  63.  
  64. for (int i = 0; i < n; i++) {
  65. if (color[i] == -1)
  66. ans &= dfs(i, 0);
  67. }
  68.  
  69. if (ans) {
  70. StringBuilder walter = new StringBuilder();
  71. StringBuilder jesse = new StringBuilder();
  72. for (int i = 0; i < n; i++) {
  73. if (color[i] == 1) {
  74. walter.append(toString.get(i) + " ");
  75. } else {
  76. jesse.append(toString.get(i) + " ");
  77. }
  78. }
  79. out.println(walter.toString().trim());
  80. out.println(jesse.toString().trim());
  81. } else {
  82. out.println("impossible");
  83. }
  84. }
  85.  
  86. private boolean dfs(int src, int c) {
  87. color[src] = c;
  88. for (int child : g[src]) {
  89. if (color[child] == -1) {
  90. dfs(child, 1 - c);
  91. } else if (color[child] == color[src]) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97.  
  98. }
  99.  
  100. static class FastReader {
  101. BufferedReader reader;
  102. StringTokenizer st;
  103.  
  104. public FastReader(InputStream stream) {
  105. reader = new BufferedReader(new InputStreamReader(stream));
  106. st = null;
  107. }
  108.  
  109. public String next() {
  110. while (st == null || !st.hasMoreTokens()) {
  111. try {
  112. String line = reader.readLine();
  113. if (line == null) {
  114. return null;
  115. }
  116. st = new StringTokenizer(line);
  117. } catch (Exception e) {
  118. throw new RuntimeException();
  119. }
  120. }
  121. return st.nextToken();
  122. }
  123.  
  124. public int nextInt() {
  125. return Integer.parseInt(next());
  126. }
  127.  
  128. }
  129. }
Add Comment
Please, Sign In to add comment