<?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=0478_-_Ciclu</id>
	<title>0478 - Ciclu - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0478_-_Ciclu"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0478_-_Ciclu&amp;action=history"/>
	<updated>2026-05-01T12:10:03Z</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=0478_-_Ciclu&amp;diff=9126&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 un vârf &lt;code&gt;p&lt;/code&gt;. Să se determine un ciclu elementar care conține vârful &lt;code&gt;p&lt;/code&gt;.  = Date de intrare = Fişierul de intrare &lt;code&gt;cicluIN.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;m&lt;/code&gt; linii conține câte o...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0478_-_Ciclu&amp;diff=9126&amp;oldid=prev"/>
		<updated>2024-01-06T18:45:09Z</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 un vârf &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;. Să se determine un ciclu elementar care conține vârful &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;cicluIN.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...&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 un vârf &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;. Să se determine un ciclu elementar care conține vârful &amp;lt;code&amp;gt;p&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;cicluIN.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 numărul &amp;lt;code&amp;gt;p&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;cicluOUT.txt&amp;lt;/code&amp;gt; va conține un singur ciclu elementar care conține vârful &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;. Acesta va începe și se va termina cu vârful &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;.Î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 ≤ n&amp;lt;/code&amp;gt;&lt;br /&gt;
* pentru toate datele de test, va exista cel puțin un ciclu care conține vârful &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;cicluIN.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&lt;br /&gt;
&amp;lt;code&amp;gt;cicluOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 2 1 3 4 2&lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;cicluIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 21 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&lt;br /&gt;
&amp;lt;code&amp;gt;cicluOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 &amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;&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 check_restrictions(n, m, edges, p):&lt;br /&gt;
    # Check constraints&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 20):&lt;br /&gt;
        return False&lt;br /&gt;
    for i, j in edges:&lt;br /&gt;
        if not (1 &amp;lt;= i &amp;lt;= n) or not (1 &amp;lt;= j &amp;lt;= n):&lt;br /&gt;
            return False&lt;br /&gt;
    if not (1 &amp;lt;= p &amp;lt;= n):&lt;br /&gt;
        return False&lt;br /&gt;
&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def afis(k):&lt;br /&gt;
    global gasit&lt;br /&gt;
    for i in range(1, k + 1):&lt;br /&gt;
        fout.write(str(x[i]) + &amp;quot; &amp;quot;)&lt;br /&gt;
    fout.write(str(p) + &amp;quot; &amp;quot;)&lt;br /&gt;
    gasit = 1&lt;br /&gt;
&lt;br /&gt;
def OK(k):&lt;br /&gt;
    if a[x[k - 1]][x[k]] != 1:&lt;br /&gt;
        return 0&lt;br /&gt;
    for i in range(1, k):&lt;br /&gt;
        if x[k] == x[i]:&lt;br /&gt;
            return 0&lt;br /&gt;
    return 1&lt;br /&gt;
&lt;br /&gt;
def back(k):&lt;br /&gt;
    global gasit&lt;br /&gt;
    for i in range(1, n + 1):&lt;br /&gt;
        x[k] = i&lt;br /&gt;
        if not gasit and OK(k):&lt;br /&gt;
            if a[x[k]][p] == 1 and k &amp;gt; 2:&lt;br /&gt;
                afis(k)&lt;br /&gt;
            back(k + 1)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    fin = open(&amp;quot;cicluIN.txt&amp;quot;, &amp;quot;r&amp;quot;)&lt;br /&gt;
    fout = open(&amp;quot;cicluOUT.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    n, m = map(int, fin.readline().split())&lt;br /&gt;
    edges = [tuple(map(int, fin.readline().split())) for _ in range(m)]&lt;br /&gt;
&lt;br /&gt;
    # Read the line and strip any leading or trailing whitespace&lt;br /&gt;
    p_line = fin.readline().strip()&lt;br /&gt;
&lt;br /&gt;
    # Check if the line is not empty before converting to an integer&lt;br /&gt;
    if p_line:&lt;br /&gt;
        p = int(p_line)&lt;br /&gt;
    else:&lt;br /&gt;
        fout.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        fin.close()&lt;br /&gt;
        fout.close()&lt;br /&gt;
        exit()&lt;br /&gt;
&lt;br /&gt;
    # Check restrictions&lt;br /&gt;
    if not check_restrictions(n, m, edges, p):&lt;br /&gt;
        fout.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
    else:&lt;br /&gt;
        a = [[0] * (n + 1) for _ in range(n + 1)]&lt;br /&gt;
&lt;br /&gt;
        for i, j in edges:&lt;br /&gt;
            a[i][j] = a[j][i] = 1&lt;br /&gt;
&lt;br /&gt;
        x = [0] * 205&lt;br /&gt;
        gasit = 0&lt;br /&gt;
&lt;br /&gt;
        x[1] = p&lt;br /&gt;
        back(2)&lt;br /&gt;
&lt;br /&gt;
    fin.close()&lt;br /&gt;
    fout.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rus Marius</name></author>
	</entry>
</feed>