Question

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}, ... , {?}.

Your program should support two operations on those sets: 1) union two sets, and 2) check whether two elements are contained by the same set.
Input:
In the first line, two integers ? (1 <= ? <= 1,000,000) and ? (1 <= ? <= 100,000) are given where ? is the number of operations that will be sequentially performed.

The following ? lines specify the operations.

A line having a format of “0 ? ?” indicates a union operation of set ? and set ? containing elements ? and ?, respectively (? ∈ ?, ? ∈ ?).

Similarly, a line beginning with ‘1’ ask you to check whether ? and ? are in the same set.
Output:
For each checking operation, you are required to answer ‘Y’ if two elements are in the same set.
Otherwise, print ‘N’.

Sample input
6 5
1 0 3
0 1 2
0 3 4
0 1 4
1 2 3

Sample Output
N
Y

2.

Given a weighted undirected graph, provide the cost of a minimum spanning tree.
Input:
In the first line, two integers ? (1 <= ? <= 10,000) and ? (1 <= ? <= 200,000) are given where ? and ? are the number of vertices and edges of the graph, respectively.

The following lines describes ? edges in a format of “? ? ?” indicating that the graph has an edge between vertices ? and ? with cost of ? .

Note that the vertex index begins with 1 and the cost ? can be negative but |?| < 1,000,000.
Output:
You are required to output the cost of a minimum spanning tree.

Sample input
3 3
1 2 1
1 3 2
2 3 3

Sample Output
3

3.

Given a weighted undirected graph, provide the cost of the second minimum spanning tree.

The second minimum spanning tree has the minimum cost except MSTs.

In the figure, the left and right subfigures show the MST and the second MST, respectively.

Input:
In the first line, two integers ? (1 <= ? <= 50,000) and ? (1 <= ? <= 200,000) are given where ? and ? are the number of vertices and edges of the graph, respectively.

The following lines describes ? edges in a format of “? ? ?” indicating that the graph has an edge between vertices ? and ? with cost of ?.

Note that the vertex index begins with 1 and edge weights are positive.

Output:
You are required to output the cost of the second minimum spanning tree. You can output "-1” if such tree does not exist.

Sample Input

7 12
1 2 8
1 3 5
2 3 10
44 2 4 2
2 5 18
3 4 3
3 6 16
4 5 12
4 6 30
4 7 14
5 7 4
6 7 26

Sample Output

44

4.

We have a network which is defined by the dependency and data transfer time among computers.

For instance, if a computer ? depends on a computer ? with the transfer time ?, then any data can be transferred from ? to ? in ? seconds.

Unfortunately, a computer in our network is infected with a virus. The virus will be propagated to the other computers which depend on the infected computers.
When a computer ? is just infected, the computer ? will be infected after ? seconds, if ? depends on ? with time ?.

We want to determine the number of computers that will be infected and the last time of infection from the first infection.

Input:
In the first line, three integers ? (1 <= ? <= 10,000), ? (1 <= ? <= 100,000), and ? are given where ?, ?, and ? are the number of computers, the number of dependencies, and the index of first infected computer, respectively.

The following lines describe ? dependencies in a format of “? ? ?” indicating that the computer ? depends on ? with transfer time of ?, where “?≠?" and 0 <= ? <= 1000.

Note that the vertex index begins with 1 and you can assume that the dependency is unique.

Output:
You are required to output the number of infected computers and time of the last infection from the first infection

Sample input
3 3 1
2 1 2
3 1 8
3 2 4

Sample Output

3 6

5.

We would like to cross over a river by stepstones. The position of a stepstone is represented by (?, ?).
You can jump to another stepstone, however, you have certain constraint due to your physical ability.
When you are at (?, ?), you are only allowed to jump to stones at (?′, ?′) where |? - ?′| <= 2 and |? - ?′| <= 2 .

For instance, you can jump from (0,0) to (2,2) or (2,1) , but you cannot move to (3,0).

The goal is reaching a line which is parallel to ?-axis. You are required to find a shortest path from (0,0) to the goal line in terms of the sum of distances.

The distance between two stones at (?, ?) and (?′, ?′) is the Euclidean distance \sqrt{(x'-x)^2+(y'-y)^2}

Input:
In the first line, two integers ? (1 <= ? <= 50,000), and ? are given where ? and ? are the number of stepstones and the ? coordinate of the goal line, respectively.

The following lines describe the position of ? stepstones in a format of “? ?” where ? and ? are integers.
Note that 0 <= ?, ?, ? <= 1,000,000

Output:
Provide the distance of shortest path by rounding off to the nearest integer.
(The exact distance in the following sample is 8.47 ⋯ . You need to answer 8 by rounding off the exact distance.)

Sample input
5 3
1 2
6 3
4 1
3 2
0 2

Sample Output

8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(1).

#include<stdio.h>
int find(int parent[],int i){
while(parent[i]>0)
i=parent[i];
return i;
}
void uni(int parent[],int p,int q){
int k = find(parent,p);
if(k == p)
parent[p] = q;
else
parent[k] = q;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int parent[n];
for(int i=0;i<n;i++)
parent[i] = -1;
for(int i=0;i<m;i++){
int c,d,e;
scanf("%d%d%d",&c,&d,&e);
switch(c){
case 0:
uni(parent,d,e);
break;
case 1:
if(find(parent,d) == find(parent,e))
printf("Y\n");
else
printf("N\n");
break;
}
}
return 0;
}

output:

6 5
1 0 3
0 1 2
0 3 4
0 1 4
1 2 3
N
Y

Add a comment
Know the answer?
Add Answer to:
a. You have 5 problems in this assignment. b. G++ compiler will be used to compile...
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
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