Appearance
question:def fantasy_paths(n, m, q, gold, roads, queries): Determine if there are paths between cities that pass through cities with a certain gold threshold. pass # Test Cases from solution import fantasy_paths def test_fantasy_paths(): n = 5 m = 5 q = 3 gold = [1, 2, 2, 4, 3] roads = [(0, 1), (0, 2), (2, 3), (3, 4), (1, 4)] queries = [(0, 4, 3), (1, 3, 2), (0, 2, 5)] result = fantasy_paths(n, m, q, gold, roads, queries) assert result == ["NO", "YES", "NO"] def test_fantasy_paths_no_path(): n = 4 m = 2 q = 1 gold = [2, 3, 1, 4] roads = [(0, 1), (2, 3)] queries = [(0, 3, 2)] result = fantasy_paths(n, m, q, gold, roads, queries) assert result == ["NO"] def test_fantasy_paths_simple_yes(): n = 3 m = 2 q = 1 gold = [1, 3, 2] roads = [(0, 1), (1, 2)] queries = [(0, 2, 1)] result = fantasy_paths(n, m, q, gold, roads, queries) assert result == ["YES"] def test_fantasy_paths_simple_no(): n = 3 m = 2 q = 1 gold = [1, 3, 2] roads = [(0, 1), (1, 2)] queries = [(0, 2, 3)] result = fantasy_paths(n, m, q, gold, roads, queries) assert result == ["NO"]
answer:def fantasy_paths(n, m, q, gold, roads, queries): from collections import defaultdict, deque def bfs(graph, start, end, threshold): visited = set() queue = deque([start]) while queue: node = queue.popleft() if node == end: return "YES" for neighbor in graph[node]: if neighbor not in visited and gold[neighbor] >= threshold: visited.add(neighbor) queue.append(neighbor) return "NO" def build_graph(threshold): graph = defaultdict(list) for u, v in roads: if gold[u] >= threshold and gold[v] >= threshold: graph[u].append(v) graph[v].append(u) return graph result = [] for u, v, g_threshold in queries: current_graph = build_graph(g_threshold) result.append(bfs(current_graph, u, v, g_threshold)) return result def process_input(): import sys input = sys.stdin.read data = input().split() index = 0 n = int(data[index]) m = int(data[index + 1]) q = int(data[index + 2]) index += 3 gold = list(map(int, data[index:index + n])) index += n roads = [] for _ in range(m): u = int(data[index]) - 1 v = int(data[index + 1]) - 1 roads.append((u, v)) index += 2 queries = [] for _ in range(q): u = int(data[index]) - 1 v = int(data[index + 1]) - 1 g_threshold = int(data[index + 2]) queries.append((u, v, g_threshold)) index += 3 result = fantasy_paths(n, m, q, gold, roads, queries) for r in result: print(r)
question:def is_interesting_sequence(n, k, sequence): Determines if the given sequence is 'interesting'. A sequence is 'interesting' if there exists at least one pair of indices (i, j) such that i < j and the absolute difference between the elements at these indices is exactly 1. Parameters: n (int): Length of the sequence k (int): Upper bound of the sequence elements sequence (list): List of integers representing the sequence Returns: str: "YES" if the sequence is interesting, otherwise "NO" def solve(t, test_cases): Solves multiple test cases and determines if each sequence is 'interesting'. Parameters: t (int): Number of test cases test_cases (list): List of tuples, each containing (n, k, sequence) for each test case Returns: list: List of results ("YES" or "NO") for each test case
answer:def is_interesting_sequence(n, k, sequence): Determines if the given sequence is 'interesting'. A sequence is 'interesting' if there exists at least one pair of indices (i, j) such that i < j and the absolute difference between the elements at these indices is exactly 1. Parameters: n (int): Length of the sequence k (int): Upper bound of the sequence elements sequence (list): List of integers representing the sequence Returns: str: "YES" if the sequence is interesting, otherwise "NO" for i in range(n): for j in range(i+1, n): if abs(sequence[i] - sequence[j]) == 1: return "YES" return "NO" def solve(t, test_cases): Solves multiple test cases and determines if each sequence is 'interesting'. Parameters: t (int): Number of test cases test_cases (list): List of tuples, each containing (n, k, sequence) for each test case Returns: list: List of results ("YES" or "NO") for each test case results = [] for i in range(t): n, k, sequence = test_cases[i] results.append(is_interesting_sequence(n, k, sequence)) return results
question:def min_max_intensity(n: int, m: int, roads: List[Tuple[int, int, int]]) -> int: Find the minimum possible maximum magic intensity of the roads used to connect all cities in a magical land. Each city should be connected either directly or indirectly. Args: n (int): The number of cities. m (int): The number of roads. roads (List[Tuple[int, int, int]]): Descriptions of the roads, where each road is represented by a tuple (u, v, w) denoting a road between city u and city v with magic intensity w. Returns: int: The minimum possible maximum magic intensity. Example: >>> min_max_intensity(4, 5, [(1, 2, 5), (1, 3, 3), (2, 3, 2), (2, 4, 4), (3, 4, 1)]) 3 >>> min_max_intensity(3, 3, [(1, 2, 2), (2, 3, 2), (1, 3, 2)]) 2
answer:def find(parent, i): if parent[i] == i: return i else: return find(parent, parent[i]) def union(parent, rank, x, y): rootX = find(parent, x) rootY = find(parent, y) if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else: parent[rootY] = rootX rank[rootX] += 1 def kruskal(n, roads): # Kruskal's algorithm to find the MST (minimum spanning tree) # Sort all the roads in increasing order of their intensity roads.sort(key=lambda x: x[2]) parent = [] rank = [] # Initializing parent and rank arrays for node in range(n): parent.append(node) rank.append(0) result = [] # This will store the MST i = 0 # Index variable to traverse sorted roads e = 0 # Index variable for the result[] # Number of edges in MST will be equal to n-1 while e < n - 1: # Pick the smallest intensity edge u, v, w = roads[i] i = i + 1 x = find(parent, u - 1) y = find(parent, v - 1) # If including this edge doesn't cause cycle, include it in result # and increment the index of result for next edge if x != y: e = e + 1 result.append((u, v, w)) union(parent, rank, x, y) # The maximum intensity in the MST will be our answer max_intensity = max([w for u, v, w in result]) return max_intensity def min_max_intensity(n, m, roads): return kruskal(n, roads) # Example usage: # min_max_intensity(4, 5, [(1, 2, 5), (1, 3, 3), (2, 3, 2), (2, 4, 4), (3, 4, 1)])
question:def max_sweetness(n: int, sweetness: List[int]) -> int: Returns the maximum total sweetness Michael can achieve by eating a subarray of chocolates once. Parameters: n (int): Number of chocolates. sweetness (list of int): Sweetness values of the chocolates. Returns: int: The maximum sweetness achievable. pass from typing import List def test_max_sweetness_example(): assert max_sweetness(5, [1, -2, 3, 4, -1]) == 7 def test_max_sweetness_all_positive(): assert max_sweetness(5, [1, 2, 3, 4, 5]) == 15 def test_max_sweetness_all_negative(): assert max_sweetness(5, [-1, -2, -3, -4, -5]) == 0 def test_max_sweetness_mixed(): assert max_sweetness(6, [-1, 2, 3, -5, 4, 2]) == 6 def test_max_sweetness_single_element(): assert max_sweetness(1, [5]) == 5 assert max_sweetness(1, [-5]) == 0 def test_max_sweetness_large_numbers(): assert max_sweetness(3, [1000000000, -1000000000, 1000000000]) == 1000000000
answer:def max_sweetness(n, sweetness): Returns the maximum total sweetness Michael can achieve by eating a subarray of chocolates once. Parameters: n (int): Number of chocolates. sweetness (list of int): Sweetness values of the chocolates. Returns: int: The maximum sweetness achievable. max_ending_here = max_so_far = 0 for s in sweetness: max_ending_here = max(0, max_ending_here + s) max_so_far = max(max_so_far, max_ending_here) return max_so_far