<?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=4071_-_Ciclu_L</id>
	<title>4071 - Ciclu L - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4071_-_Ciclu_L"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4071_-_Ciclu_L&amp;action=history"/>
	<updated>2026-05-01T06:38:58Z</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=4071_-_Ciclu_L&amp;diff=9127&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; noduri și un număr &lt;code&gt;L&lt;/code&gt;. Să se determine un ciclu elementar de lungime &lt;code&gt;L&lt;/code&gt;.  = Date de intrare = Fişierul de intrare &lt;code&gt;ciclulIN.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 noduri 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 pereche de nu...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4071_-_Ciclu_L&amp;diff=9127&amp;oldid=prev"/>
		<updated>2024-01-06T18:49:54Z</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; noduri și un număr &amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;. Să se determine un ciclu elementar de lungime &amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;ciclulIN.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 noduri 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 nu...&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; noduri și un număr &amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;. Să se determine un ciclu elementar de lungime &amp;lt;code&amp;gt;L&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;ciclulIN.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 noduri 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;L&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;ciclulOUT.txt&amp;lt;/code&amp;gt; va conține un singur ciclu elementar de lungime &amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;. Acesta va începe și se va termina cu același nod.&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;
* &amp;lt;code&amp;gt;1 ≤ L ≤ n&amp;lt;/code&amp;gt;&lt;br /&gt;
* pentru toate datele de test, va exista cel puțin un ciclu de lungime &amp;lt;code&amp;gt;L&amp;lt;/code&amp;gt;&lt;br /&gt;
* lungimea unui ciclu este egală cu numărul de muchii.&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;ciclulIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 6 7&lt;br /&gt;
 1 2&lt;br /&gt;
 2 3&lt;br /&gt;
 2 5&lt;br /&gt;
 3 6&lt;br /&gt;
 4 6&lt;br /&gt;
 5 4&lt;br /&gt;
 3 5&lt;br /&gt;
 4&lt;br /&gt;
&amp;lt;code&amp;gt;ciclulOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 3 5 4 6 3&lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;ciclulIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 21 7&lt;br /&gt;
 1 2&lt;br /&gt;
 2 3&lt;br /&gt;
 2 5&lt;br /&gt;
 3 6&lt;br /&gt;
 4 6&lt;br /&gt;
 5 4&lt;br /&gt;
 3 5&lt;br /&gt;
 4&lt;br /&gt;
&amp;lt;code&amp;gt;ciclulOUT.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;
N = 235&lt;br /&gt;
&lt;br /&gt;
def check_constraints(n, m, k):&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 20) or not (1 &amp;lt;= m &amp;lt;= n*(n-1)//2) or not (1 &amp;lt;= k &amp;lt;= n):&lt;br /&gt;
        fout.write(&amp;quot;Datele nu corespund restrictiilor impuse\n&amp;quot;)&lt;br /&gt;
        exit(0)&lt;br /&gt;
&lt;br /&gt;
def cond(j):&lt;br /&gt;
    if j &amp;gt; 1 and a[X[j-1]][X[j]] == 0:&lt;br /&gt;
        return 0&lt;br /&gt;
    if j == k and a[X[j]][X[1]] == 0:&lt;br /&gt;
        return 0&lt;br /&gt;
    return 1&lt;br /&gt;
&lt;br /&gt;
def afis():&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(X[1]) + &amp;#039;\n&amp;#039;)&lt;br /&gt;
    exit(0)&lt;br /&gt;
&lt;br /&gt;
def back(j):&lt;br /&gt;
    for i in range(1, n+1):&lt;br /&gt;
        if not p[i]:&lt;br /&gt;
            X[j] = i&lt;br /&gt;
            p[i] = 1&lt;br /&gt;
            if cond(j):&lt;br /&gt;
                if j == k:&lt;br /&gt;
                    afis()&lt;br /&gt;
                else:&lt;br /&gt;
                    back(j + 1)&lt;br /&gt;
            p[i] = 0&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    fin = open(&amp;quot;ciclulIN.txt&amp;quot;, &amp;quot;r&amp;quot;)&lt;br /&gt;
    fout = open(&amp;quot;ciclulOUT.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    a = [[0] * 50 for _ in range(50)]&lt;br /&gt;
    n, m = map(int, fin.readline().split())&lt;br /&gt;
&lt;br /&gt;
    check_constraints(n, m, n)  # Assuming k is the same as n&lt;br /&gt;
&lt;br /&gt;
    for _ in range(m):&lt;br /&gt;
        x, y = map(int, fin.readline().split())&lt;br /&gt;
        a[x][y] = a[y][x] = 1&lt;br /&gt;
&lt;br /&gt;
    k = int(fin.readline())&lt;br /&gt;
    check_constraints(n, m, k)&lt;br /&gt;
&lt;br /&gt;
    X = [0] * 50&lt;br /&gt;
    p = [0] * 50&lt;br /&gt;
&lt;br /&gt;
    back(1)&lt;br /&gt;
&lt;br /&gt;
    fin.close()&lt;br /&gt;
    fout.close()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rus Marius</name></author>
	</entry>
</feed>