<?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=0477_-_Lanturi_1</id>
	<title>0477 - Lanturi 1 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0477_-_Lanturi_1"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0477_-_Lanturi_1&amp;action=history"/>
	<updated>2026-05-01T12:51:00Z</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=0477_-_Lanturi_1&amp;diff=8724&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 trei vârfuri &lt;code&gt;p q r&lt;/code&gt;. Să se determine toate lanțurile elementare cu extremitățile în &lt;code&gt;p&lt;/code&gt; și &lt;code&gt;q&lt;/code&gt; care nu conțin vârful &lt;code&gt;r&lt;/code&gt;.  = Date de intrare = Fişierul de intrare &lt;code&gt;lanturi1IN.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 î...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0477_-_Lanturi_1&amp;diff=8724&amp;oldid=prev"/>
		<updated>2023-12-30T13:33:10Z</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 trei vârfuri &amp;lt;code&amp;gt;p q r&amp;lt;/code&amp;gt;. Să se determine toate lanțurile elementare cu extremitățile în &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; care nu conțin vârful &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;lanturi1IN.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 î...&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 trei vârfuri &amp;lt;code&amp;gt;p q r&amp;lt;/code&amp;gt;. Să se determine toate lanțurile elementare cu extremitățile în &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; care nu conțin vârful &amp;lt;code&amp;gt;r&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;lanturi1IN.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 trei numere &amp;lt;code&amp;gt;p q r&amp;lt;/code&amp;gt;, cu semnificația precizată.&lt;br /&gt;
&lt;br /&gt;
= Date de ieşire =&lt;br /&gt;
Fişierul de ieşire &amp;lt;code&amp;gt;lanturi1OUT.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;, care nu conțin vârful &amp;lt;code&amp;gt;r&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 , r ≤ n&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt; sunt diferite&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;lanturi1IN.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 3&lt;br /&gt;
&amp;lt;code&amp;gt;lanturi1OUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 2 1 4 5 &lt;br /&gt;
 2 4 5 &lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;lanturi1IN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 100 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 3&lt;br /&gt;
&amp;lt;code&amp;gt;lanturi1OUT.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 is_valid_edge(u, v, path, r, visited):&lt;br /&gt;
    return v != r and v not in path and not visited[v]&lt;br /&gt;
&lt;br /&gt;
def generate_paths(graph, p, q, r, n):&lt;br /&gt;
    def backtrack(current, path, visited):&lt;br /&gt;
        visited[current] = True&lt;br /&gt;
&lt;br /&gt;
        if current == q:&lt;br /&gt;
            result.append(path[:])&lt;br /&gt;
            visited[current] = False&lt;br /&gt;
            return&lt;br /&gt;
&lt;br /&gt;
        for neighbor in graph[current]:&lt;br /&gt;
            if is_valid_edge(current, neighbor, path, r, visited):&lt;br /&gt;
                path.append(neighbor)&lt;br /&gt;
                backtrack(neighbor, path, visited)&lt;br /&gt;
                path.pop()&lt;br /&gt;
&lt;br /&gt;
        visited[current] = False&lt;br /&gt;
&lt;br /&gt;
    result = []&lt;br /&gt;
    visited = {i: False for i in range(1, n + 1)}&lt;br /&gt;
    backtrack(p, [p], visited)&lt;br /&gt;
    return result&lt;br /&gt;
&lt;br /&gt;
def check_restrictions(n, m, edges, p, q, r):&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 20 and 1 &amp;lt;= p &amp;lt;= n and 1 &amp;lt;= q &amp;lt;= n and 1 &amp;lt;= r &amp;lt;= n and p != q != r):&lt;br /&gt;
        with open(&amp;#039;lanturi1OUT.txt&amp;#039;, &amp;#039;w&amp;#039;) as output_file:&lt;br /&gt;
            output_file.write(&amp;quot;Datele nu corespund restrictiilor impuse\n&amp;quot;)&lt;br /&gt;
        return False&lt;br /&gt;
&lt;br /&gt;
    for edge in edges:&lt;br /&gt;
        u, v = edge&lt;br /&gt;
        if not (1 &amp;lt;= u &amp;lt;= n and 1 &amp;lt;= v &amp;lt;= n):&lt;br /&gt;
            with open(&amp;#039;lanturi1OUT.txt&amp;#039;, &amp;#039;w&amp;#039;) as output_file:&lt;br /&gt;
                output_file.write(&amp;quot;Datele nu corespund restrictiilor impuse\n&amp;quot;)&lt;br /&gt;
            return False&lt;br /&gt;
&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;#039;lanturi1IN.txt&amp;#039;, &amp;#039;r&amp;#039;) as input_file:&lt;br /&gt;
        n, m = map(int, input_file.readline().split())&lt;br /&gt;
        edges = [tuple(map(int, input_file.readline().split())) for _ in range(m)]&lt;br /&gt;
        &lt;br /&gt;
        line = input_file.readline().split()&lt;br /&gt;
        if len(line) &amp;lt; 3:&lt;br /&gt;
            with open(&amp;#039;lanturi1OUT.txt&amp;#039;, &amp;#039;w&amp;#039;) as output_file:&lt;br /&gt;
                output_file.write(&amp;quot;Datele nu corespund restrictiilor impuse\n&amp;quot;)&lt;br /&gt;
            return&lt;br /&gt;
&lt;br /&gt;
        p, q, r = map(int, line)&lt;br /&gt;
&lt;br /&gt;
    if not check_restrictions(n, m, edges, p, q, r):&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    graph = {i: [] for i in range(1, n + 1)}&lt;br /&gt;
    for edge in edges:&lt;br /&gt;
        u, v = edge&lt;br /&gt;
        graph[u].append(v)&lt;br /&gt;
        graph[v].append(u)&lt;br /&gt;
&lt;br /&gt;
    paths = generate_paths(graph, p, q, r, n)&lt;br /&gt;
    unique_paths = [list(item) for item in set(tuple(path) for path in paths)]&lt;br /&gt;
    unique_paths.sort()&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;#039;lanturi1OUT.txt&amp;#039;, &amp;#039;w&amp;#039;) as output_file:&lt;br /&gt;
        for path in unique_paths:&lt;br /&gt;
            output_file.write(&amp;quot; &amp;quot;.join(map(str, path)) + &amp;quot;\n&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>Rus Marius</name></author>
	</entry>
</feed>