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 from typing import List, Tuple def read_input() -> Tuple[int, List[int], List[Tuple[int, int]]]: # Lecture de la première ligne n, m, t = map(int, stdin.readline().strip().split()) # Lecture des positions initiales des tricheurs cheaters = list(map(int, stdin.readline().strip().split())) # Lecture des routes roads = [tuple(map(int, stdin.readline().strip().split())) for _ in range(m)] return n, cheaters, roads def bfs(n: int, start: int, roads: List[Tuple[int, int]]) -> List[int]: # Créez les listes d'adjacence des bâtiments adj = [[] for _ in range(n + 1)] for u, v in roads: adj[u].append(v) adj[v].append(u) # Initialisation des distances avec 'infini' inf = float('inf') distances = [inf] * (n + 1) distances[start] = 0 # File pour le BFS queue = deque([start]) while queue: current = queue.popleft() current_distance = distances[current] for neighbor in adj[current]: if distances[neighbor] == inf: # On vérifie si ce bâtiment a déjà été visité distances[neighbor] = current_distance + 1 queue.append(neighbor) return distances def solve_cheaters(n: int, cheaters: List[int], roads: List[Tuple[int, int]]) -> List[int]: # Distances du gagnant aux autres bâtiments winner_distances = bfs(n, 1, roads) # Distances des tricheurs aux autres bâtiments cheater_distances = [bfs(n, cheater, roads) for cheater in cheaters] # Trouver les bâtiments où le gagnant arrive avant tout tricheur result = [] for building in range(1, n + 1): if all(winner_distances[building] < cheater_distances[i][building] for i in range(len(cheaters))): result.append(building) return result def main(): n, cheaters, roads = read_input() result = solve_cheaters(n, cheaters, roads) stdout.write(' '.join(map(str, sorted(result))) + '\n') main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.