affiche juste le code, pas d'explications !
- User
System: affiche juste le code, pas d'explications ! User: Tu es un développeur python3 expérimenté, et tu dois concevoir un programme en python (avec des commentaires en français, sans utiliser de librairies externes, sans
if __name__=="__main__":, avec type hints, et en privilégiant les algorythmes a complexité constante (O(1)) et/ou complexité linéaire (O(n))). Le programme va recevoir les données en entrée sur la stdin, et doit répondre sur la stdout. Fabrique moi un programme python qui résout le problème (énoncé en markdown) suivant :Cheaters
You organize the Master Dev France in the city of Paris and you want to hide the winner's check in a building so that no cheater can get to that building before the winner. More precisely, the city of Paris is composed of
Nbuildings andMbidirectional roads, it is possible to move between any pair of buildings by using the roads. The winner is currently located in building number 1, while theTcheaters are located at buildingsu_1,u_2, ...,u_T. You must return the sorted list of all buildings where the winner can arrive strictly before the cheaters.Data
Input
Line 1: Three integers
N(1 <=N<= 10^4) the number of buildings,M(1 <=M<= 2 * 10^4) the number of roads,T(1 <=T<=N) the number of cheaters.Line 2:
Tdistinct integers between 2 andN: The initial positions of the cheaters.The following M lines: two integers
uandv, indicating that there is a route betweenuandv(1 <=u<v<= N).Output
If there are K buildings where the winner can arrive before the cheaters, you must return K integers sorted on one line: the list of buildings in question.
Example
For the input :
7 6 2 5 6 1 2 1 3 2 4 4 5 3 6 3 7The answer is:
1 2Assistant:
- Assistant
from sys import stdin, stdout from collections import deque def find_buildings(N: int, M: int, T: int, cheaters: list[int], roads: list[tuple[int, int]]) -> list[int]: # Initialiser le graphe comme un dictionnaire d'adjacence graph = {i: [] for i in range(1, N+1)} # Remplir le graphe avec les routes for u, v in roads: graph[u].append(v) graph[v].append(u) # Fonction pour faire BFS et trouver les distances depuis un point de départ def bfs(start: int) -> dict[int, int]: distances = {i: float('inf') for i in range(1, N+1)} queue = deque([start]) distances[start] = 0 while queue: node = queue.popleft() current_distance = distances[node] for neighbor in graph[node]: if distances[neighbor] == float('inf'): distances[neighbor] = current_distance + 1 queue.append(neighbor) return distances # Calculer les distances depuis le gagnant (bâtiment 1) winner_distances = bfs(1) # Calculer les distances depuis chaque tricheur cheater_distances = {} for cheater in cheaters: cheater_distances[cheater] = bfs(cheater) # Identifier les bâtiments où le gagnant arrive avant tous les tricheurs valid_buildings = [] for building in range(1, N+1): if all(winner_distances[building] < cheater_distances[cheater][building] for cheater in cheaters): valid_buildings.append(building) return sorted(valid_buildings) # Lecture des données d'entrée input_data = stdin.read().split() N = int(input_data[0]) M = int(input_data[1]) T = int(input_data[2]) cheaters = list(map(int, input_data[3:3+T])) roads = [] index = 3 + T for _ in range(M): u = int(input_data[index]) v = int(input_data[index+1]) roads.append((u, v)) index += 2 # Calculer le résultat result = find_buildings(N, M, T, cheaters, roads) # Afficher le résultat stdout.write(" ".join(map(str, result)) + "\n")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.