Question: I need help modifiying the code in the picture to meet the following requirements: Given a network of n computers, a set of the pairs
I need help modifiying the code in the picture to meet the following requirements:
Given a network of n computers, a set of the pairs of computers that are initially connected, and a sequence of steps where connections are destroyed, one by one, calculate the connectivity as defined in the problem specification aboveafter each connection is destroyed.
The Input must be read from standard input no file io use of file io will fail all the test cases and you will get zero:The first line of input contains three space separated integers, n n m m x and d d mrepresenting the number of computers in the enemy network, the number of connections between pairs of computers in the enemy network, and the number of those connections which you will destroy, respectively. The computers are numbered through n inclusive, and the connections are numbered through m respectively. The following m lines will each contain a pair of distinct integers, u and v u v n u v representing that computers u and v are connected, initially. Note: we denote the first of these lines as connection the second as connection and so forth. It is guaranteed that each of the pairs listed will be unique pairs; namely, the same two values of u and v will never appear on two different lines as separate connections. Of course, many individual computers will be connected to more than one other computer, so a particular value u may appear on more than one of these m lines. The following d lines will each contain a unique integer in between and m inclusive, representing a connection number that gets destroyed. These will appear in the order that they get destroyed.
The Output standard console output: Output d lines of output. The first line should be the initial connectivity of the network. The following d lines should have the connectivity of the network after each connection is destroy, one by one. Hints: Draw the sample input first and then see how the output numbers are calculated. You can use our existing disjoint set code and update it make sure to use path compression and union by rank approach. Otherwise, your code may fail larger grading test cases due to optimization issue
The union part of the cycle detection concept we have learned in the class could be useful. We mainly use the find and union operations of disjoint sets. As there is not an easy way to break a set when you
remove an edge, it would be best idea not to think in that direction
Instead, generate the result bottom up
a Make sets based on number of nodes
b Store all the edges in your data structure maybe a class or d array Maintaining status existnot exist
for each edge might be useful
i Dont use UNION operation during this process. If you do you will get stuck as you will not be
able to easily remove them
c Keep the list of index of the edges you are deleting and maintain proper edge status
d Also, have an array to store the result for each deletion
e For each deletion index, perform UNION and calculate result
f In this way, you will generate your result array and just print it
Also note that during this process, you will need to maintain the size of a set at the root of each set.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
