Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. import edu.princeton.cs.algs4.StdIn;
  2. import edu.princeton.cs.algs4.StdOut;
  3. import java.io.*;
  4. import java.util.LinkedList;
  5. import java.util.ListIterator;
  6.  
  7. public class DSufm {
  8. private int[] parent; // parent[i] = parent of i
  9. private int[] size; // size[i] = number of sites in subtree rooted at i
  10. private int count; // number of components
  11. private Node[] nodes;
  12.  
  13. public DSufm(int n) {
  14. count = n;
  15. nodes = new Node[n];
  16. for (int i = 0; i < n; i++) {
  17. Node node = new Node(i);
  18. nodes[i] = node;
  19. }
  20. }
  21.  
  22. public class Node{
  23. int value;
  24. LinkedList<Node> children;
  25. Node parent;
  26. Node right;
  27. Node left;
  28. int size;
  29.  
  30. public Node (int value){
  31. this.value = value;
  32. this.children = new LinkedList<Node>();
  33. size = 1;
  34. }
  35.  
  36. public void add(Node child){
  37. Node newest;
  38. Node oldNewest = null;
  39. if(!children.isEmpty()) {
  40. newest = children.getLast();
  41. newest.setRight(child);
  42. oldNewest = newest;
  43. }
  44. children.add(child);
  45. if (oldNewest != null){
  46. if(children.size()!=1){
  47. newest = children.getLast();
  48. newest.setLeft(oldNewest);
  49. }
  50. }
  51. }
  52.  
  53.  
  54. public void setRight(Node right){
  55. this.right = right;
  56. }
  57.  
  58. public void setLeft(Node left){
  59. this.left = left;
  60. }
  61.  
  62. public void setParent(Node parent){
  63. this.parent = parent;
  64. }
  65.  
  66. public int getSize(){
  67. return size;
  68. }
  69.  
  70. public Node getParent(){
  71. return parent;
  72. }
  73.  
  74. public Node getLeft(){
  75. return left;
  76. }
  77.  
  78. public Node getRight(){
  79. return right;
  80. }
  81.  
  82. public LinkedList<Node> getChildren(){
  83. return children;
  84. }
  85.  
  86. public void clearChildren(){
  87. children = new LinkedList<Node>();
  88. }
  89.  
  90. public void setSize(int n){
  91. this.size = n;
  92. }
  93.  
  94. public int getValue(){
  95. return value;
  96. }
  97. }
  98.  
  99. public void move(int p, int q){ //Linear time sadly :(
  100. Node rootP = find(p);
  101. Node rootQ = find(q);
  102. if(rootP == rootQ){
  103. return;
  104. }
  105.  
  106. Node nodeP = nodes[p];
  107. Node nodeQ = nodes[q];
  108. if(!nodeP.getChildren().isEmpty()){
  109. Node firstPChild = nodeP.getChildren().remove();
  110. firstPChild.setParent(nodeP.getParent());
  111. Node nextPChild = firstPChild.getRight();
  112. firstPChild.setRight(null);
  113. if (nextPChild != null){
  114. nextPChild.setLeft(null);}
  115. while(nextPChild != null){
  116. nextPChild.setParent(firstPChild);
  117. firstPChild.setSize(firstPChild.getSize()+1);
  118. nextPChild = nextPChild.getRight();
  119. }
  120. }
  121. nodeP.clearChildren();
  122. nodeP.setSize(1);
  123. nodeQ.setSize(nodeQ.getSize()+1);
  124. nodeP.setParent(nodeQ);
  125. nodeQ.add(nodeP);
  126. }
  127.  
  128.  
  129. public int count(){
  130. return count;
  131. }
  132.  
  133. public Node find(int p) {
  134. Node rootP = nodes[p];
  135. while (rootP.getParent() != null){
  136. rootP = rootP.getParent();
  137. }
  138. return rootP;
  139. }
  140.  
  141. public boolean connected(int p, int q) {
  142. return find(p) == find(q);
  143. }
  144.  
  145. public void union(int p, int q) {
  146. Node rootP = find(p);
  147. Node rootQ = find(q);
  148. if (rootP == rootQ) return;
  149.  
  150. // make smaller root point to larger one
  151. if (rootP.getSize() < rootQ.getSize()) {
  152. rootP.setParent(rootQ);
  153. rootQ.add(rootP);
  154. rootP.setSize(rootP.getSize()+rootQ.getSize());
  155. }else {
  156. rootQ.setParent(rootP);
  157. rootP.add(rootQ);
  158. rootQ.setSize(rootQ.getSize()+rootP.getSize());
  159. }
  160. count--;
  161. }
  162.  
  163. public static void main(String[] args) {
  164. int n = StdIn.readInt();
  165. int m = StdIn.readInt();
  166. DSufm uf = new DSufm(n);
  167. int line = 0;
  168. while (!StdIn.isEmpty()) {
  169. String str = StdIn.readString();
  170. int p = StdIn.readInt();
  171. int q = StdIn.readInt();
  172. if ("u".equals(str)) {
  173. uf.union(p, q);
  174. line++;
  175. //StdOut.println(line + ": Union");
  176. } else if ("m".equals(str)) {
  177. uf.move(p,q);
  178. line++;
  179. //StdOut.println(line + ": Move");
  180. } else {
  181. line++;
  182. //StdOut.println(uf.connected(p,q) ? line + ": yes":line + ": no");
  183. StdOut.println(uf.connected(p,q) ? "yes":"no");
  184. }
  185. }
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement