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 :The Loneliness of the Long Distance Runner
For the Cross-Country event, the organising committee has installed a series of radio-frequency gates throughout the race. These enabled the detection of runners passing through and the live updating of the rankings. However, not all the gates have the same range and some have been placed far too close together, so that their emission areas overlap. This overlap can lead to some interference between gates and gives false results.
One way of solving the problem is to assign different radio frequencies to two gates that are too close together. However, the tags placed on the athletes can only react to a limited number of frequencies and more sophisticated ones are expensive. To minimise the costs of organising the race, determine the minimum number of frequencies so that no two gates have the same frequency as those close to them.
Data
Input
Line 1 : Two integers
PandI, representing respectively the number of gates and the number of pairs of gates sufficiently close together to cause interferences.Line 2 to I+1 : Two integers
AandB, representing the identifiers of two interfering gates.The integer
Pis between0and10000included.The integer
Iis betweenP-1and(P*P-P)/2included.For each line, both integers
AandBare between0andP-1and such that `AIf
Ais interfering withB, thenBis interfering withA.The whole race course is covered by the gates and each gate covers a single continuous section of the race.
We consider that the trajectory of the race is strictly straight and that the emission zone of the gates are circular. Thus, no interference can be due to the sinuosity of the path of the race.
Output
The minimum number of frequencies that can be affected to the gates such that no two distinct gates can interfere.
- Assistant
from sys import stdin, stdout from collections import defaultdict import itertools def is_safe(node: int, color: int, graph: defaultdict, colors: list[int]) -> bool: # Vérifie si la couleur peut être assignée à la porte for neighbor in graph[node]: if colors[neighbor] == color: return False return True def graph_coloring(node: int, m: int, P: int, graph: defaultdict, colors: list[int]) -> bool: # Si toutes les portes sont colorées, on a terminé if node == P: return True # Essaye d'assigner une couleur à la porte actuelle for color in range(m): if is_safe(node, color, graph, colors): colors[node] = color if graph_coloring(node + 1, m, P, graph, colors): return True colors[node] = -1 # Backtrack return False def minimum_frequencies(P: int, I: int, pairs: list[tuple[int, int]]) -> int: # Création d'un graphe pour représenter les interférences entre les portes graph = defaultdict(list) for A, B in pairs: graph[A].append(B) graph[B].append(A) # Chercher le nombre minimal de couleurs en essayant à partir de 1 for m in itertools.count(1): colors = [-1] * P if graph_coloring(0, m, P, graph, colors): return m return P # Par sécurité, mais on devrait jamais arriver ici # Lecture des données d'entrée input_data = stdin.read().strip().splitlines() P, I = map(int, input_data[0].split()) pairs = [tuple(map(int, line.split())) for line in input_data[1:I + 1]] # Calcul et affichage du résultat result = minimum_frequencies(P, I, pairs) stdout.write(f"{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.