Skip to content
🤔prompts chat🧠
🔍
question:def sum_of_connected_components_values(n: int, m: int, node_values: List[int], edges: List[Tuple[int, int]]) -> int: Calculate the sum of the values of all connected components in the graph. Parameters: - n: the number of nodes in the graph - m: the number of edges in the graph - node_values: list of integer values associated with each node - edges: list of tuples representing edges between nodes Returns: The sum of the maximum values of all connected components in the graph. >>> sum_of_connected_components_values(5, 3, [1, 2, 3, 4, 5], [(1, 2), (2, 3), (4, 5)]) 8 >>> sum_of_connected_components_values(4, 0, [2, 3, 1, 4], []) 10 >>> sum_of_connected_components_values(3, 2, [10, 20, 30], [(1, 2), (2, 3)]) 30 >>> sum_of_connected_components_values(6, 3, [1, 5, 3, 7, 2, 6], [(1, 2), (3, 4), (5, 6)]) 18 >>> sum_of_connected_components_values(1, 0, [42], []) 42 pass

answer:def sum_of_connected_components_values(n, m, node_values, edges): from collections import defaultdict, deque # Create adjacency list adj_list = defaultdict(list) for u, v in edges: adj_list[u - 1].append(v - 1) adj_list[v - 1].append(u - 1) def bfs(start_node): q = deque([start_node]) max_value = node_values[start_node] visited[start_node] = True while q: node = q.popleft() for neighbor in adj_list[node]: if not visited[neighbor]: visited[neighbor] = True q.append(neighbor) max_value = max(max_value, node_values[neighbor]) return max_value visited = [False] * n total_sum = 0 for i in range(n): if not visited[i]: total_sum += bfs(i) return total_sum

question:from collections import defaultdict from typing import List, Tuple def max_data_flow(n: int, m: int, links: List[Tuple[int, int, int]], s: int, t: int) -> int: Calculate the maximum potential data flow from a source server s to a destination server t. Args: n (int): Number of servers m (int): Number of communication links links (List[Tuple[int, int, int]]): List of tuples representing communication links s (int): Source server t (int): Destination server Returns: int: Maximum data flow from source to destination def test_max_data_flow_basic(): n = 4 m = 5 links = [(1, 2, 100), (1, 3, 100), (2, 3, 1), (2, 4, 100), (3, 4, 100)] s = 1 t = 4 assert max_data_flow(n, m, links, s, t) == 200 def test_max_data_flow_single_path(): n = 3 m = 2 links = [(1, 2, 5), (2, 3, 5)] s = 1 t = 3 assert max_data_flow(n, m, links, s, t) == 5 def test_max_data_flow_multiple_paths(): n = 6 m = 10 links = [(1, 2, 10), (2, 6, 10), (1, 3, 10), (3, 6, 10), (1, 4, 10), (4, 6, 10), (1, 5, 10), (5, 6, 7), (2, 3, 1), (4, 5, 1)] s = 1 t = 6 assert max_data_flow(n, m, links, s, t) == 37 def test_max_data_flow_large_capacity(): n = 5 m = 6 links = [(1, 2, 1000), (1, 3, 1000), (2, 3, 1000), (2, 4, 1000), (3, 5, 1000), (4, 5, 1000)] s = 1 t = 5 assert max_data_flow(n, m, links, s, t) == 2000

answer:from collections import deque, defaultdict def bfs(C, F, source, sink, parent): visited = set() queue = deque([source]) visited.add(source) while queue: u = queue.popleft() for v in C[u]: if v not in visited and C[u][v] - F[u][v] > 0: queue.append(v) visited.add(v) parent[v] = u if v == sink: return True return False def edmonds_karp(C, source, sink): Using Edmonds-Karp algorithm to find the maximum flow n = len(C) F = defaultdict(lambda: defaultdict(int)) parent = {} max_flow = 0 while bfs(C, F, source, sink, parent): path_flow = float('Inf') s = sink while s != source: path_flow = min(path_flow, C[parent[s]][s] - F[parent[s]][s]) s = parent[s] v = sink while v != source: u = parent[v] F[u][v] += path_flow F[v][u] -= path_flow v = parent[v] max_flow += path_flow return max_flow def max_data_flow(n, m, links, s, t): C = defaultdict(lambda: defaultdict(int)) for u, v, d in links: C[u][v] += d C[v][u] += d return edmonds_karp(C, s, t)

question:from typing import List, Tuple def solve_tree_queries(n: int, values: List[int], edges: List[Tuple[int, int]], queries: List[int]) -> List[int]: You are given a tree (connected acyclic undirected graph) with `n` nodes. Each node has an integer value assigned to it. Your task is to answer multiple queries on the tree. Each query asks for the maximum value within a given subtree of the tree. Args: n (int): The number of nodes in the tree. values (List[int]): The values assigned to the nodes. edges (List[Tuple[int, int]]): The edges between the nodes. queries (List[int]): The queries representing the nodes of the tree. Returns: List[int]: The maximum values in the subtrees rooted at the nodes specified in the queries. Examples: >>> solve_tree_queries(7, [1, 2, 3, 4, 5, 6, 7], [(1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)], [1, 3, 6]) [7, 7, 6] >>> solve_tree_queries(1, [42], [], [1]) [42] >>> solve_tree_queries(5, [5, 4, 3, 2, 1], [(1, 2), (2, 3), (3, 4), (4, 5)], [1, 3, 5]) [5, 3, 1] >>> solve_tree_queries(7, [1, 6, 7, 3, 4, 5, 2], [(1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)], [1, 2, 3, 4, 5, 6, 7]) [7, 6, 7, 3, 4, 5, 2]

answer:from collections import defaultdict def build_tree(n, edges): tree = defaultdict(list) for u, v in edges: tree[u].append(v) tree[v].append(u) return tree def dfs(tree, node, parent, values, subtree_max): # Initially, the maximum of subtree rooted at `node` is the value of the node itself max_value = values[node-1] for neighbor in tree[node]: if neighbor != parent: # Get the maximum value for the subtree rooted at the neighbor child_max = dfs(tree, neighbor, node, values, subtree_max) max_value = max(max_value, child_max) # Store the maximum value for the subtree rooted at node subtree_max[node] = max_value return max_value def preprocess_tree(n, values, edges): tree = build_tree(n, edges) subtree_max = [0] * (n + 1) dfs(tree, 1, -1, values, subtree_max) return subtree_max def solve_tree_queries(n, values, edges, queries): subtree_max = preprocess_tree(n, values, edges) result = [] for v in queries: result.append(subtree_max[v]) return result

question:def max_weight(capacity: int, weights: List[int]) -> int: Determine the highest possible weight a drone can carry given a list of package weights, without exceeding the drone's carrying capacity. Args: capacity (int): Maximum weight the drone can carry. weights (List[int]): List of package weights. Returns: int: The maximum weight the drone can carry without exceeding the capacity. Examples: >>> max_weight(10, [3, 5, 7, 2]) 10 >>> max_weight(15, [4, 8, 5, 6]) 15 >>> max_weight(7, [3, 6, 3, 4]) 7

answer:def max_weight(capacity, weights): Returns the maximum weight that a drone can carry without exceeding the given capacity. weights.sort(reverse=True) # Sort weights in descending order for better chances to reach capacity sooner n = len(weights) dp = [0] * (capacity + 1) for w in weights: for c in range(capacity, w-1, -1): dp[c] = max(dp[c], dp[c - w] + w) return dp[capacity]

Released under the chat License.

has loaded