<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4074_-_Distante</id>
	<title>4074 - Distante - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4074_-_Distante"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4074_-_Distante&amp;action=history"/>
	<updated>2026-05-01T03:56:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.universitas.ro/index.php?title=4074_-_Distante&amp;diff=8010&amp;oldid=prev</id>
		<title>Simina: Pagină nouă: == Enunț == Se consideră un graf neorientat conex cu &lt;code&gt;n&lt;/code&gt; noduri, numerotate de la &lt;code&gt;1&lt;/code&gt; la &lt;code&gt;n&lt;/code&gt;, şi &lt;code&gt;m&lt;/code&gt; muchii. Definim distanţa minimă dintre două noduri &lt;code&gt;x&lt;/code&gt; şi &lt;code&gt;y&lt;/code&gt; ca fiind numărul minim de muchii al unui lanţ elementar care uneşte &lt;code&gt;x&lt;/code&gt; cu &lt;code&gt;y&lt;/code&gt;.  = Cerinţa = Se dă o pereche de noduri &lt;code&gt;p q&lt;/code&gt;. Determinați nodurile &lt;code&gt;r&lt;/code&gt; cu proprietatea că distanța minimă dintr...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4074_-_Distante&amp;diff=8010&amp;oldid=prev"/>
		<updated>2023-12-13T16:17:26Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: == Enunț == Se consideră un graf neorientat conex cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri, numerotate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, şi &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii. Definim distanţa minimă dintre două noduri &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; ca fiind numărul minim de muchii al unui lanţ elementar care uneşte &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; cu &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;.  = Cerinţa = Se dă o pereche de noduri &amp;lt;code&amp;gt;p q&amp;lt;/code&amp;gt;. Determinați nodurile &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt; cu proprietatea că distanța minimă dintr...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Enunț ==&lt;br /&gt;
Se consideră un graf neorientat conex cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri, numerotate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, şi &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii. Definim distanţa minimă dintre două noduri &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; ca fiind numărul minim de muchii al unui lanţ elementar care uneşte &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; cu &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Cerinţa =&lt;br /&gt;
Se dă o pereche de noduri &amp;lt;code&amp;gt;p q&amp;lt;/code&amp;gt;. Determinați nodurile &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt; cu proprietatea că distanța minimă dintre &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt; este egală cu distanța minimă dintre &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fişierul de intrare &amp;lt;code&amp;gt;distanteIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie numere &amp;lt;code&amp;gt;n m p q&amp;lt;/code&amp;gt;. Fiecare dintre următoarele &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; linii va conţine câte două numere &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;, separate printr-un spaţiu, cu semnificaţia: există o muchie între nodul &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; şi nodul &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de ieşire =&lt;br /&gt;
Fişierul de ieşire &amp;lt;code&amp;gt;distanteOUT.txt&amp;lt;/code&amp;gt; va conține pe prime linie, separate prin câte un spațiu, modurile cerute în ordine crescătoare.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;n ≤ 1000&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1 =&lt;br /&gt;
&amp;lt;code&amp;gt;distanteIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 7 8 2 4&lt;br /&gt;
 1 2&lt;br /&gt;
 2 3&lt;br /&gt;
 2 6&lt;br /&gt;
 3 4&lt;br /&gt;
 3 6&lt;br /&gt;
 3 7&lt;br /&gt;
 4 5&lt;br /&gt;
 4 6&lt;br /&gt;
&amp;lt;code&amp;gt;distanteOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 3 6 7 &lt;br /&gt;
&lt;br /&gt;
= Exemplul 2 =&lt;br /&gt;
&amp;lt;code&amp;gt;distanteIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 1001 86 37 3&lt;br /&gt;
&amp;lt;code&amp;gt;distanteOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 Datele nu corespund restrictiilor impuse&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python3&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
from collections import deque&lt;br /&gt;
&lt;br /&gt;
def bfs(graf, start):&lt;br /&gt;
    distante = {nod: float(&amp;#039;inf&amp;#039;) for nod in graf}&lt;br /&gt;
    distante[start] = 0&lt;br /&gt;
    coada = deque([start])&lt;br /&gt;
&lt;br /&gt;
    while coada:&lt;br /&gt;
        nod_curent = coada.popleft()&lt;br /&gt;
&lt;br /&gt;
        for vecin in graf[nod_curent]:&lt;br /&gt;
            if distante[vecin] == float(&amp;#039;inf&amp;#039;):&lt;br /&gt;
                distante[vecin] = distante[nod_curent] + 1&lt;br /&gt;
                coada.append(vecin)&lt;br /&gt;
&lt;br /&gt;
    return distante&lt;br /&gt;
&lt;br /&gt;
def gaseste_noduri_potrivite(graf, p, q):&lt;br /&gt;
    distante_p = bfs(graf, p)&lt;br /&gt;
    distante_q = bfs(graf, q)&lt;br /&gt;
&lt;br /&gt;
    noduri_potrivite = []&lt;br /&gt;
    for nod in graf:&lt;br /&gt;
        if distante_p[nod] == distante_q[nod]:&lt;br /&gt;
            noduri_potrivite.append(nod)&lt;br /&gt;
&lt;br /&gt;
    return noduri_potrivite&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;distanteIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as fisier_intrare:&lt;br /&gt;
        n, m, p, q = map(int, fisier_intrare.readline().split())&lt;br /&gt;
&lt;br /&gt;
        if n &amp;gt; 1000:&lt;br /&gt;
            with open(&amp;quot;distanteOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier_iesire:&lt;br /&gt;
                fisier_iesire.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
            return&lt;br /&gt;
&lt;br /&gt;
        graf = {i: [] for i in range(1, n + 1)}&lt;br /&gt;
&lt;br /&gt;
        for _ in range(m):&lt;br /&gt;
            x, y = map(int, fisier_intrare.readline().split())&lt;br /&gt;
            graf[x].append(y)&lt;br /&gt;
            graf[y].append(x)&lt;br /&gt;
&lt;br /&gt;
    noduri_potrivite = gaseste_noduri_potrivite(graf, p, q)&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;quot;distanteOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier_iesire:&lt;br /&gt;
        if n &amp;lt;= 1000:&lt;br /&gt;
            fisier_iesire.write(&amp;quot; &amp;quot;.join(map(str, noduri_potrivite)))&lt;br /&gt;
        else:&lt;br /&gt;
            fisier_iesire.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Simina</name></author>
	</entry>
</feed>