Question

Algorithm implementation in Java please.

Josefine is currently setting up a new computer network at The University of Algorithms. The network consists of N computers

Input (stdin) 4 5 A 81 A13 Expected Output NO YES YES

Comments
    Answer #1
    Explanation::
    

    Code in JAVA is given below

    Union.java and computernetwork.java files are created inside the package named codejudge42

    Code in JAVA::

    Union.java Code::

    package codejudge42;

    import java.util.ArrayList;

    class Union{

    ArrayList<Integer> unionList;

    ArrayList<Integer> rootList;

    Union(int n) {

    unionList = new ArrayList<>();

    rootList = new ArrayList<>();

    for (int i = 0; i < n; i++){

    unionList.add(i);

    rootList.add(i, 1);

    }

    }

    public int find(int i){

    int rootI = i;

    while(rootI != unionList.get(rootI)){

    rootI = unionList.get(rootI);

    }

    while(i != rootI){

    int nextRoot = unionList.get(i);

    unionList.set(i, rootI);

    i=nextRoot;

    }

    return rootI;

    }

    public void unionLink(int i, int j){

    int i1 = find(i);

    int j1 = find(j);

    if (i1 == j1){

    return;

    }

    if (rootList.get(i1) < rootList.get(j1)){

    rootList.set(j1, rootList.get(j1) + rootList.get(i1));

    unionList.set(i1, j1);

    }

    else{

    rootList.set(i1, rootList.get(j1) + rootList.get(i1));

    unionList.set(j1, i1);

    }

    }

    public boolean isConnected(int i, int j){

    int iID = find(i);

    int jID = find(j);

    return iID == jID;

    }

    }

    computernetwork.java Code::

    package codejudge42;

    import java.util.ArrayList;

    import java.util.Scanner;

    public class computernetwork {

    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int m = scan.nextInt();

    int n = scan.nextInt();

    Union pcNet = new Union(m);

    for(int counter = 0; counter < n; counter++){

    String newline = scan.nextLine();

    if (newline.equals("")){newline = scan.nextLine();}

    Scanner intScan = new Scanner(newline.substring(1));

    int i,j;

    char c = newline.charAt(0);

    if (c == 'A'){

    i = intScan.nextInt();

    j = intScan.nextInt();

    pcNet.unionLink(i, j);

    }

    else {

    i = intScan.nextInt();

    j = intScan.nextInt();

    if((pcNet.isConnected(i, j))== true){

    System.out.println("YES");

    }

    else{

    System.out.println("NO");

    }

    }

    }

    }

    }

    OUTPUT::

    4 5

    A 0 1

    C 0 3

    NO

    C 0 1

    YES

    A 1 3

    C 0 3

    YES

    Please provide the feedback!!

    Thank You!!

    Add a comment
    Know the answer?
    Add Answer to:
    Josefine is currently setting up a new computer network at The University of Algorithms. The netw...
    Your Answer:

    Post as a guest

    Your Name:

    What's your source?

    Earn Coins

    Coins can be redeemed for fabulous gifts.

    Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
    Similar Homework Help Questions
    • a. You have 5 problems in this assignment. b. G++ compiler will be used to compile...

      a. You have 5 problems in this assignment. b. G++ compiler will be used to compile your source codes. c. Your program will be tested on Ubuntu 16.04. d. You are not allowed to use global variables in your implementation. e. Your program will get two arguments, input and output file names, from the command line: >> Your_Executable INPUT_FILE_NAME OUTPUT_FILE_NAME 1. Given a number ? , we initially have ?+1 different sets which are {0}, {1}, {2}, ... , {?}....

    • 1-15 3 0 Multiple-Choice Quiz I. Waich of the following are standard eopologies? (Select three) t....

      1-15 3 0 Multiple-Choice Quiz I. Waich of the following are standard eopologies? (Select three) t. What aspects of network cabling do the lEE committees establish? (Select three. . Sear Ring I Speed C Color of sheathing D. Cable types D. Dual-ring 2. Iohn was carrying on at the water cooler the other 7. What types of coas cabling have been used in day, trying to show off his knowledge of networking. computer networkingt (Select three.) He claimed that the...

    • Charlene is the owner of Charlene's Computer Care (3C) and has been in the business of...

      Charlene is the owner of Charlene's Computer Care (3C) and has been in the business of servicing computers and computer systems at affordable prices for just over five years. 3C's primary clients are local businesses but include some residential customers. Their advertised approach is to analyze the problem, develop a range of IT options, explain their specific recommendation to the customer, and finally estimate the time and costs associated with the service prior to commencing work. Their approach has proven...

    • Computer science organization and architecture! Help! Instructions The answers to questions below must be written in...

      Computer science organization and architecture! Help! Instructions The answers to questions below must be written in the file named you MUST replace which has been provided with this test. Important: login the FSUl of the filename with you actual FSU login ID. That file has lines that begin with Question N. where N corresponds to the question number. Below or beside each Question N. there are placeholder as described must be replaced with the correct answer in the specific formats,...

    • A university computer science department has a teaching assistant (TA) who helps undergraduate students with their...

      A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TA’s office is rather small and the TA can help only one student at a time in the office. There are two chairs in the hallway outside the office where students can sit and wait if the TA is currently helping another student. When there are no students who need help during office hours, the TA sits...

    • Dell Computer Please answer the question based on the below Dell case: Q- Conduct Value Chain...

      Dell Computer Please answer the question based on the below Dell case: Q- Conduct Value Chain "Primary activities and Support activities" analysis of Dell ? Dell Computer is one of the most extraordinary success stories in business history. Started in 1984 by Michael Dell in his dorm room when he was an undergraduate student at the University of Texas in Austin, Dell has become the world’s largest producer of computer systems. The company sells notebooks and desktop computers, network servers,...

    • File_Size_(MB) Transfer_Time_(sec) 48 17.3 59 30.3 81 27.8 74 24.3 22 8.3 21 12.9 31 16.4...

      File_Size_(MB) Transfer_Time_(sec) 48 17.3 59 30.3 81 27.8 74 24.3 22 8.3 21 12.9 31 16.4 49 18.6 99 32.5 22 19.6 51 29.2 99 31.6 68 26.7 69 23.6 98 44.6 32 23.8 87 35.1 81 29.6 29 14.6 97 32.9 Before taking the plunge into videoconferencing, a company ran tests of its current internal computer network. The goal of the tests was to measure how rapidly data moved through the network given the current demand on the network....

    • (Just need help with part F) File_size_(MB) Time_(sec) 77 31.1 93 35.1 85 35.3 94 35.7...

      (Just need help with part F) File_size_(MB) Time_(sec) 77 31.1 93 35.1 85 35.3 94 35.7 20 14.4 74 28.9 68 29.6 88 33.5 42 21.7 20 15.3 72 28.4 24 11.5 95 35.7 59 25.6 93 36.6 71 29.1 87 34.3 92 37.2 90 35.6 67 26.8 87 32.6 83 31.2 80 34.1 57 22.8 52 25.4 76 28.9 96 38.7 70 31.8 59 24.2 57 28.1 Before taking the plunge into videoconferencing, a company ran tests of its...

    • eEgg is considering the purchase of a new distributed network computer system to help handle its...

      eEgg is considering the purchase of a new distributed network computer system to help handle its warehouse inventories. The system costs $65,000 to purchase and install and $34,000 to operate each year. The system is estimated to be useful for 4 years. Management expects the new system to reduce the cost of managing inventories by $67,000 per year. The firm’s cost of capital (discount rate) is 12%. Required: 1. What is the net present value (NPV) of the proposed investment...

    • Question 1 Which of the following choices best describes the scatterplot shown below? Group of answer...

      Question 1 Which of the following choices best describes the scatterplot shown below? Group of answer choices Linear, negative, strong No form, weak Linear, positive, strong Curved, weak Question 2 Which of the following choices is most likely to be the correlation of the data in the scatterplot shown below? Group of answer choices -0.14 1.04 0.86 -0.92 Question 3 Most roller coasters get their speed by dropping down a steep initial incline, so it makes sense that we can...

    ADVERTISEMENT
    Free Homework Help App
    Download From Google Play
    Scan Your Homework
    to Get Instant Free Answers
    Need Online Homework Help?
    Ask a Question
    Get Answers For Free
    Most questions answered within 3 hours.
    ADVERTISEMENT
    ADVERTISEMENT
    ADVERTISEMENT