fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class OddEvenTreeHard {
  5.  
  6. int[] col;
  7. boolean bad;
  8.  
  9. void dfs(int cur, int to, String[] dist)
  10. {
  11. if(col[cur] == -1)
  12. {
  13. col[cur] = to;
  14. }
  15. else
  16. {
  17. if(col[cur] != to) {
  18. bad = true;
  19. }
  20. return;
  21. }
  22. for(int i = 0; i < dist.length; i++)
  23. {
  24. if(dist[cur].charAt(i) == 'E' || dist[i].charAt(cur) == 'E')
  25. dfs(i, to, dist);
  26. if(dist[cur].charAt(i) == 'O' || dist[i].charAt(cur) == 'O')
  27. dfs(i, 1-to, dist);
  28. }
  29. }
  30.  
  31. public int[] getTree(String[] dist)
  32. {
  33. int[] noSolution = new int[1];
  34. noSolution[0] = -1;
  35.  
  36. int n = dist.length;
  37. col = new int[n];
  38. bad = false;
  39.  
  40. int currentCol = 0;
  41. for(int i = 0; i < n; i++)
  42. col[i] = -1;
  43. for(int i = 0; i < n; i++)
  44. if(col[i] == -1)
  45. {
  46. dfs(i, currentCol, dist);
  47. currentCol = 1 - currentCol;
  48. }
  49.  
  50. List<Integer> part0 = new ArrayList<Integer>();
  51. List<Integer> part1 = new ArrayList<Integer>();
  52. if(bad)
  53. return noSolution;
  54. for(int i = 0; i < n; i++)
  55. if(col[i] == 0)
  56. part0.add(i);
  57. else
  58. part1.add(i);
  59.  
  60. if(part0.size() == 0 || part1.size() == 0)
  61. return noSolution;
  62.  
  63. int[] ret = new int[(n-1)*2];
  64. int p = 0;
  65. for(int i = 0; i < part1.size(); i++)
  66. {
  67. ret[p] = part0.get(0);
  68. p ++;
  69. ret[p] = part1.get(i);
  70. p ++;
  71. }
  72. for(int i = 1; i < part0.size(); i++)
  73. {
  74. ret[p] = part0.get(i);
  75. p ++;
  76. ret[p] = part1.get(0);
  77. p ++;
  78. }
  79. return ret;
  80. }
  81. }
  82.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class OddEvenTreeHard is public, should be declared in a file named OddEvenTreeHard.java
public class OddEvenTreeHard {
       ^
1 error
stdout
Standard output is empty