Advertisement
Cloude

Ladice 1

Mar 25th, 2020
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. import java.util.StringTokenizer;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.io.OutputStream;
  9.  
  10.  
  11. class Kattio extends PrintWriter {
  12. public Kattio(InputStream i) {
  13. super(new BufferedOutputStream(System.out));
  14. r = new BufferedReader(new InputStreamReader(i));
  15. }
  16. public Kattio(InputStream i, OutputStream o) {
  17. super(new BufferedOutputStream(o));
  18. r = new BufferedReader(new InputStreamReader(i));
  19. }
  20.  
  21. public int getInt() {
  22. return Integer.parseInt(nextToken());
  23. }
  24.  
  25. public double getDouble() {
  26. return Double.parseDouble(nextToken());
  27. }
  28.  
  29. public long getLong() {
  30. return Long.parseLong(nextToken());
  31. }
  32.  
  33. public String getWord() {
  34. return nextToken();
  35. }
  36.  
  37.  
  38.  
  39. private BufferedReader r;
  40. private String line;
  41. private StringTokenizer st;
  42. private String token;
  43.  
  44. private String peekToken() {
  45. if (token == null)
  46. try {
  47. while (st == null || !st.hasMoreTokens()) {
  48. line = r.readLine();
  49. if (line == null) return null;
  50. st = new StringTokenizer(line);
  51. }
  52. token = st.nextToken();
  53. } catch (IOException e) { }
  54. return token;
  55. }
  56.  
  57. private String nextToken() {
  58. String ans = peekToken();
  59. token = null;
  60. return ans;
  61. }
  62. }
  63.  
  64. class UnionFind {
  65. int[] p;
  66. int[] rank;
  67. boolean[] open;
  68. int[] end;
  69.  
  70. //1 based indexing!!!!
  71. UnionFind(int n) {
  72. p = new int[n+1];
  73. rank = new int[n+1];
  74. end = new int[n+1];
  75. open = new boolean[n+1];
  76. for (int i = 1; i < n+1; i++) {
  77. p[i] = i;
  78. rank[i] = 0;
  79. end[i] = 0;
  80. open[i] = true;
  81. }
  82. }
  83.  
  84. int findset (int i) {
  85. if (p[i] == i) {return i;}
  86. else {
  87. p[i] = findset(p[i]);
  88. return p[i];
  89. }
  90. }
  91.  
  92. void closeset (int i) {
  93. open[findset(i)] = false;
  94. }
  95.  
  96. boolean issetopen (int i) {
  97. return open[findset(i)];
  98. }
  99.  
  100. boolean issameset(int i, int j) {
  101. return findset(i) == findset(j);
  102. }
  103.  
  104. //adding left to right, right end preserved
  105. void unionset (int i, int j) {
  106. if (!issameset(i, j)) {
  107. int x = findset(i), y = findset(j);
  108. if (!open[x] || !open[y]) {
  109. open[x] = false;
  110. open[y] = false; //i can put a marker and then
  111. //change one value only but
  112. //this is okay also
  113. }
  114. else {
  115. end[x] = end[y]; //free end node only left for open + open
  116. }
  117. if (rank[x] > rank[y]) p[y] = x;
  118. else {
  119. p[x] = y;
  120. if (rank[x] == rank[y]) rank[y] += 1;
  121. }
  122. }
  123. else {open[findset(i)] = false;}
  124. }
  125.  
  126. }
  127.  
  128. public class Ladice {
  129.  
  130. public static void main(String[] args) {
  131. Kattio br = new Kattio(System.in);
  132. int n1 = br.getInt(), n2 = br.getInt();
  133. boolean[] occupied = new boolean[n2 + 1];
  134. UnionFind u = new UnionFind(n2);
  135.  
  136. for (int i = 0; i < n1; i++) {
  137. int a = br.getInt(), b = br.getInt();
  138.  
  139. if (!occupied[a]) {
  140. occupied[a] = true;
  141. u.unionset(a, b);
  142. br.println("LADICA");
  143. }
  144.  
  145. else if (!occupied[b]) {
  146. occupied[b] = true;
  147. u.unionset(b, a);
  148. br.println("LADICA");
  149. }
  150.  
  151. else if (u.issetopen(a)) {
  152. occupied[u.end[a]] = true;
  153. u.unionset(a, b);
  154. br.println("LADICA");
  155. }
  156.  
  157. else if (u.issetopen(b)) {
  158. occupied[u.end[b]] = true;
  159. u.unionset(b, a);
  160. br.println("LADICA");
  161. }
  162.  
  163. else {br.println("SMECE");}
  164. }
  165.  
  166. br.close();
  167.  
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement