Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import edu.princeton.cs.algs4.StdIn;
- import edu.princeton.cs.algs4.StdOut;
- import java.io.*;
- import java.util.LinkedList;
- import java.util.ListIterator;
- public class DSufm {
- private int[] parent; // parent[i] = parent of i
- private int[] size; // size[i] = number of sites in subtree rooted at i
- private int count; // number of components
- private Node[] nodes;
- public DSufm(int n) {
- count = n;
- nodes = new Node[n];
- for (int i = 0; i < n; i++) {
- Node node = new Node(i);
- nodes[i] = node;
- }
- }
- public class Node{
- int value;
- LinkedList<Node> children;
- Node parent;
- Node right;
- Node left;
- int size;
- public Node (int value){
- this.value = value;
- this.children = new LinkedList<Node>();
- size = 1;
- }
- public void add(Node child){
- Node newest;
- Node oldNewest = null;
- if(!children.isEmpty()) {
- newest = children.getLast();
- newest.setRight(child);
- oldNewest = newest;
- }
- children.add(child);
- if (oldNewest != null){
- if(children.size()!=1){
- newest = children.getLast();
- newest.setLeft(oldNewest);
- }
- }
- }
- public void setRight(Node right){
- this.right = right;
- }
- public void setLeft(Node left){
- this.left = left;
- }
- public void setParent(Node parent){
- this.parent = parent;
- }
- public int getSize(){
- return size;
- }
- public Node getParent(){
- return parent;
- }
- public Node getLeft(){
- return left;
- }
- public Node getRight(){
- return right;
- }
- public LinkedList<Node> getChildren(){
- return children;
- }
- public void clearChildren(){
- children = new LinkedList<Node>();
- }
- public void setSize(int n){
- this.size = n;
- }
- public int getValue(){
- return value;
- }
- }
- public void move(int p, int q){ //Linear time sadly :(
- Node rootP = find(p);
- Node rootQ = find(q);
- if(rootP == rootQ){
- return;
- }
- Node nodeP = nodes[p];
- Node nodeQ = nodes[q];
- if(!nodeP.getChildren().isEmpty()){
- Node firstPChild = nodeP.getChildren().remove();
- firstPChild.setParent(nodeP.getParent());
- Node nextPChild = firstPChild.getRight();
- firstPChild.setRight(null);
- if (nextPChild != null){
- nextPChild.setLeft(null);}
- while(nextPChild != null){
- nextPChild.setParent(firstPChild);
- firstPChild.setSize(firstPChild.getSize()+1);
- nextPChild = nextPChild.getRight();
- }
- }
- nodeP.clearChildren();
- nodeP.setSize(1);
- nodeQ.setSize(nodeQ.getSize()+1);
- nodeP.setParent(nodeQ);
- nodeQ.add(nodeP);
- }
- public int count(){
- return count;
- }
- public Node find(int p) {
- Node rootP = nodes[p];
- while (rootP.getParent() != null){
- rootP = rootP.getParent();
- }
- return rootP;
- }
- public boolean connected(int p, int q) {
- return find(p) == find(q);
- }
- public void union(int p, int q) {
- Node rootP = find(p);
- Node rootQ = find(q);
- if (rootP == rootQ) return;
- // make smaller root point to larger one
- if (rootP.getSize() < rootQ.getSize()) {
- rootP.setParent(rootQ);
- rootQ.add(rootP);
- rootP.setSize(rootP.getSize()+rootQ.getSize());
- }else {
- rootQ.setParent(rootP);
- rootP.add(rootQ);
- rootQ.setSize(rootQ.getSize()+rootP.getSize());
- }
- count--;
- }
- public static void main(String[] args) {
- int n = StdIn.readInt();
- int m = StdIn.readInt();
- DSufm uf = new DSufm(n);
- int line = 0;
- while (!StdIn.isEmpty()) {
- String str = StdIn.readString();
- int p = StdIn.readInt();
- int q = StdIn.readInt();
- if ("u".equals(str)) {
- uf.union(p, q);
- line++;
- //StdOut.println(line + ": Union");
- } else if ("m".equals(str)) {
- uf.move(p,q);
- line++;
- //StdOut.println(line + ": Move");
- } else {
- line++;
- //StdOut.println(uf.connected(p,q) ? line + ": yes":line + ": no");
- StdOut.println(uf.connected(p,q) ? "yes":"no");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement