Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me find the problem and fix it def find ( u ) : global parent if ( u = = parent [ u

Please help me find the problem and fix it
def find(u):
global parent
if(u == parent[u]):
return u
parent[u]= find(parent[u])
return parent[u]
# Read in the number of vertices (n) and edges (m)
n = int(input())
m = int(input())
edges =[]
n_edges =[[0 for i in range(n)] for j in range(n)]
for i in range(m):
u, v, w = input().split()
u = int(u)
v = int(v)
w = float(w)
edges.append([u,v,w])
n_edges[u][v]=w
n_edges[u][v]=w
sorted_eg = sorted(edges, key=lambda x: x[2])
parent=[i for i in range(n)]
count = int(input())
costs =0
for i in range(count):
u,v = input().split()
u = int(u)
v = int(v)
parent_u = find(u)
parent_v = find(v)
parent[parent_u]=parent_v
costs +=n_edges[u][v]
q =0
for u, v, w in sorted_eg:
parent_u = find(u)
parent_v = find(v)
if parent_u != parent_v:
parent[parent_u]= parent_v
q += w
result = q +costs
print(round(result,2))
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions

Question

Identify four applications of HRM to healthcare organizations.

Answered: 1 week ago