<?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=0475_-_Lant</id>
	<title>0475 - Lant - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0475_-_Lant"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0475_-_Lant&amp;action=history"/>
	<updated>2026-05-01T06:37:11Z</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=0475_-_Lant&amp;diff=8711&amp;oldid=prev</id>
		<title>Rus Marius: Pagină nouă: = Cerinţa = Se dă lista muchiilor unui graf neorientat cu &lt;code&gt;n&lt;/code&gt; vârfuri și două vârfuri &lt;code&gt;p q&lt;/code&gt;. Să se determine toate lanțurile elementare cu extremitățile &lt;code&gt;p&lt;/code&gt; și &lt;code&gt;q&lt;/code&gt;.  = Date de intrare = Fişierul de intrare &lt;code&gt;lantIN.txt&lt;/code&gt; conţine pe prima linie numerele &lt;code&gt;n&lt;/code&gt; și &lt;code&gt;m&lt;/code&gt;, reprezentând numărul de vârfuri ale grafului și numărul de muchii date în continuare. Fiecare dintre următoarele &lt;code&gt;...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0475_-_Lant&amp;diff=8711&amp;oldid=prev"/>
		<updated>2023-12-30T12:45:22Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerinţa = Se dă lista muchiilor unui graf neorientat cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri și două vârfuri &amp;lt;code&amp;gt;p q&amp;lt;/code&amp;gt;. Să se determine toate lanțurile elementare cu extremitățile &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt;.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;lantIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie numerele &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, reprezentând numărul de vârfuri ale grafului și numărul de muchii date în continuare. Fiecare dintre următoarele &amp;lt;code&amp;gt;...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Cerinţa =&lt;br /&gt;
Se dă lista muchiilor unui graf neorientat cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; vârfuri și două vârfuri &amp;lt;code&amp;gt;p q&amp;lt;/code&amp;gt;. Să se determine toate lanțurile elementare cu extremitățile &amp;lt;code&amp;gt;p&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;lantIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie numerele &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, reprezentând numărul de vârfuri ale grafului și numărul de muchii date în continuare. Fiecare dintre următoarele &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; linii conține câte o pereche de numere &amp;lt;code&amp;gt;i j&amp;lt;/code&amp;gt;, cu semnificația că există muchie între &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;j&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Următoarea linie conține două numere &amp;lt;code&amp;gt;p q&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;lantOUT.txt&amp;lt;/code&amp;gt; va conține, în ordine lexicografică, toate lanțurile elementare cu extremitățile în &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;, respectiv &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt;, fiecare lanț fiind afișat pe câte o linie a fișierului, vârfurile dintr-un lanț fiind separate prin exact un spațiu.În cazul în care restricțiile nu sunt îndeplinite, se va afișa mesajul &amp;quot;Nu corespunde restricțiilor impuse&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ n ≤ 20&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ i , j ≤n&amp;lt;/code&amp;gt;&lt;br /&gt;
* muchiile se pot repeta în fișierul de intrare&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ p , q ≤ n&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;lantIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 5 8&lt;br /&gt;
 1 4 &lt;br /&gt;
 1 3 &lt;br /&gt;
 3 5 &lt;br /&gt;
 4 5 &lt;br /&gt;
 2 4 &lt;br /&gt;
 1 2 &lt;br /&gt;
 4 2 &lt;br /&gt;
 3 4&lt;br /&gt;
 2 5&lt;br /&gt;
&amp;lt;code&amp;gt;lantOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 2 1 3 4 5 &lt;br /&gt;
 2 1 3 5 &lt;br /&gt;
 2 1 4 3 5 &lt;br /&gt;
 2 1 4 5 &lt;br /&gt;
 2 4 1 3 5 &lt;br /&gt;
 2 4 3 5 &lt;br /&gt;
 2 4 5 &lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;lantIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 1000 100&lt;br /&gt;
 1 4 &lt;br /&gt;
 1 3 &lt;br /&gt;
 3 5 &lt;br /&gt;
 4 5 &lt;br /&gt;
 2 4 &lt;br /&gt;
 1 2 &lt;br /&gt;
 4 2 &lt;br /&gt;
 3 4&lt;br /&gt;
 2 5&lt;br /&gt;
&amp;lt;code&amp;gt;lantOUT.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;
def verifica_restrictii(n, muchii, p, q):&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 20):&lt;br /&gt;
        return False&lt;br /&gt;
    for u, v in muchii:&lt;br /&gt;
        if not (1 &amp;lt;= u &amp;lt;= n) or not (1 &amp;lt;= v &amp;lt;= n):&lt;br /&gt;
            return False&lt;br /&gt;
    if not (1 &amp;lt;= p &amp;lt;= n) or not (1 &amp;lt;= q &amp;lt;= n):&lt;br /&gt;
        return False&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def citeste_graf(file_path):&lt;br /&gt;
    with open(file_path, &amp;#039;r&amp;#039;) as file:&lt;br /&gt;
        n, m = map(int, file.readline().split())&lt;br /&gt;
        muchii = [tuple(map(int, file.readline().split())) for _ in range(m)]&lt;br /&gt;
&lt;br /&gt;
        try:&lt;br /&gt;
            p, q = map(int, file.readline().split())&lt;br /&gt;
        except ValueError:&lt;br /&gt;
            with open(&amp;quot;lantOUT.txt&amp;quot;, &amp;#039;w&amp;#039;) as file_out:&lt;br /&gt;
                file_out.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
            exit()&lt;br /&gt;
&lt;br /&gt;
    if not verifica_restrictii(n, muchii, p, q):&lt;br /&gt;
        with open(&amp;quot;lantOUT.txt&amp;quot;, &amp;#039;w&amp;#039;) as file_out:&lt;br /&gt;
            file_out.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        exit()&lt;br /&gt;
&lt;br /&gt;
    return n, muchii, p, q&lt;br /&gt;
&lt;br /&gt;
def dfs(v, tinta, graf, vizitat, path, rezultat):&lt;br /&gt;
    vizitat[v] = True&lt;br /&gt;
    path.append(v)&lt;br /&gt;
&lt;br /&gt;
    if v == tinta:&lt;br /&gt;
        rezultat.append(path.copy())&lt;br /&gt;
    else:&lt;br /&gt;
        for vecin in graf[v]:&lt;br /&gt;
            if not vizitat[vecin]:&lt;br /&gt;
                dfs(vecin, tinta, graf, vizitat, path, rezultat)&lt;br /&gt;
&lt;br /&gt;
    path.pop()&lt;br /&gt;
    vizitat[v] = False&lt;br /&gt;
&lt;br /&gt;
def gaseste_lanturi(n, muchii, p, q):&lt;br /&gt;
    graf = {i: [] for i in range(1, n + 1)}&lt;br /&gt;
    for u, v in muchii:&lt;br /&gt;
        graf[u].append(v)&lt;br /&gt;
        graf[v].append(u)&lt;br /&gt;
&lt;br /&gt;
    vizitat = {i: False for i in range(1, n + 1)}&lt;br /&gt;
    rezultat = []&lt;br /&gt;
&lt;br /&gt;
    dfs(p, q, graf, vizitat, [], rezultat)&lt;br /&gt;
&lt;br /&gt;
    rezultat = list(set(tuple(lant) for lant in rezultat))&lt;br /&gt;
    rezultat.sort()&lt;br /&gt;
&lt;br /&gt;
    return rezultat&lt;br /&gt;
&lt;br /&gt;
def scrie_lanturi(file_path, lanturi):&lt;br /&gt;
    with open(file_path, &amp;#039;w&amp;#039;) as file:&lt;br /&gt;
        for lant in lanturi:&lt;br /&gt;
            file.write(&amp;#039; &amp;#039;.join(map(str, lant)) + &amp;#039;\n&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    file_in = &amp;quot;lantIN.txt&amp;quot;&lt;br /&gt;
    file_out = &amp;quot;lantOUT.txt&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    n, muchii, p, q = citeste_graf(file_in)&lt;br /&gt;
    rezultat = gaseste_lanturi(n, muchii, p, q)&lt;br /&gt;
    scrie_lanturi(file_out, rezultat)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rus Marius</name></author>
	</entry>
</feed>