Generation

generate functionSat, 17 Jun 2023

There are 'N' cities with 'M' bidirectional roads between them. You are given an array 'T' of length 'M', such that the 'i-th' road has traffic 'T[i]'. You must gather some of your 'N' friends for a party. The 'i-th' friend lives in the city 'i'. You are given an array 'F' of length 'N' such that the 'i-th' friend has a fun factor of 'F[ i ]'. Your total fun at the party is bitwise OR of the fun factors of all the friends you gather. You start at city '0'. Your "traffic tolerance" is the maximum traffic you can withstand. Any roads with traffic greater than your tolerance cannot be used as that will ruin your mood, and thus the party will be canceled. Return the minimum traffic tolerance with which you can have at least 'X' total fun at the party. If you can never have that much fun at the party, return '-1'. Input Format: The first line of input contains a single integer 'T', which denotes the number of test cases. Then 'T' test cases follow. For each test case: The first line contains three integers, 'N', 'M', and 'X', denoting the number of cities, the number of roads, and the total fun requirement, respectively. The following 'M' lines contain two integers 'u, v' denoting a road between city 'u' and city 'v'. The next line contains 'M' integers, denoting the array 'T'. The last line contains 'N' integers, denoting the array 'F'. Output Format: For each test case, return the minimum traffic tolerance with which you can have at least 'X' total fun at the party and return -1 if it is impossible. Note: You don't need to print anything. Just implement the given function.

def Tolerance(N, M, X, T, F): # Write your code here max_traffic = max(T) min_tolerance = (2**31) - 1 for i in range(max_traffic, 0, -1): directed_graph = {} for u, v in enumerate(range(M)): if T[u] <= i: if u not in directed_graph: directed_graph[u] = [] directed_graph[u].append(v) total_fun = 0 is_possible = True visited = [False]*N dfs(0, directed_graph, total_fun, X, F, visited, is_possible) if is_possible: min_tolerance = min(min_tolerance, i) break if min_tolerance == (2**31) - 1: return -1 return min_tolerance def dfs(ind, directed_graph

Questions about programming?Chat with your personal AI assistant