<?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=0438_-_Componente_Conexe</id>
	<title>0438 - Componente Conexe - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0438_-_Componente_Conexe"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0438_-_Componente_Conexe&amp;action=history"/>
	<updated>2026-05-02T03:10:21Z</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=0438_-_Componente_Conexe&amp;diff=7971&amp;oldid=prev</id>
		<title>Simina: Pagină nouă: = Cerinţa = Se dă lista muchiilor unui graf neorientat. Să se afișeze componentele conexe ale acestui graf.  = Date de intrare = Fişierul de intrare &lt;code&gt;componenteconexeIN.txt&lt;/code&gt; conţine pe prima linie numărul &lt;code&gt;n&lt;/code&gt;, reprezentând numărul de vârfuri ale grafului. Fiecare dintre următoarele linii conține câte o pereche de numere &lt;code&gt;i j&lt;/code&gt;, cu semnificația că există muchie între &lt;code&gt;i&lt;/code&gt; și &lt;code&gt;j&lt;/code&gt;.  = Date de ieşire = Fişie...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0438_-_Componente_Conexe&amp;diff=7971&amp;oldid=prev"/>
		<updated>2023-12-13T10:36:26Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerinţa = Se dă lista muchiilor unui graf neorientat. Să se afișeze componentele conexe ale acestui graf.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;componenteconexeIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie numărul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, reprezentând numărul de vârfuri ale grafului. Fiecare dintre următoarele 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;.  = Date de ieşire = Fişie...&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. Să se afișeze componentele conexe ale acestui graf.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fişierul de intrare &amp;lt;code&amp;gt;componenteconexeIN.txt&amp;lt;/code&amp;gt; conţine pe prima linie numărul &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, reprezentând numărul de vârfuri ale grafului. Fiecare dintre următoarele 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;
= Date de ieşire =&lt;br /&gt;
Fişierul de ieşire &amp;lt;code&amp;gt;componenteconexeOUT.txt&amp;lt;/code&amp;gt; va conţine pe prima linie numărul de componente conexe &amp;lt;code&amp;gt;nrc&amp;lt;/code&amp;gt;. Fiecare dintre următoarele &amp;lt;code&amp;gt;nrc&amp;lt;/code&amp;gt; linii va conține în ordine crescătoare, separate printr-un spațiu, vârfurile din componenta conexă curentă.&lt;br /&gt;
&lt;br /&gt;
Ordinea de afișare a componentelor conexe va fi cea crescătoare a vârfului cu eticheta minimă din fiecare componentă.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ n ≤ 100&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;
* în fișierul de intrare muchiile se pot repeta&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1 =&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexeIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 5&lt;br /&gt;
 1 4 &lt;br /&gt;
 3 5&lt;br /&gt;
 2 4&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexeOUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 2&lt;br /&gt;
 1 2 4&lt;br /&gt;
 3 5&lt;br /&gt;
&lt;br /&gt;
= Exemplul 2 =&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexeIN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 101&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexeOUT.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 DFS(nod, vizitat, componenta, lista_adiacenta):&lt;br /&gt;
    vizitat[nod] = True&lt;br /&gt;
    componenta.append(nod)&lt;br /&gt;
&lt;br /&gt;
    for vecin in lista_adiacenta[nod]:&lt;br /&gt;
        if not vizitat[vecin]:&lt;br /&gt;
            DFS(vecin, vizitat, componenta, lista_adiacenta)&lt;br /&gt;
&lt;br /&gt;
def gaseste_componente_conexe(n, muchii):&lt;br /&gt;
    lista_adiacenta = {i: [] for i in range(1, n + 1)}&lt;br /&gt;
&lt;br /&gt;
    for muchie in muchii:&lt;br /&gt;
        i, j = muchie&lt;br /&gt;
        lista_adiacenta[i].append(j)&lt;br /&gt;
        lista_adiacenta[j].append(i)&lt;br /&gt;
&lt;br /&gt;
    vizitat = {i: False for i in range(1, n + 1)}&lt;br /&gt;
    componente = []&lt;br /&gt;
&lt;br /&gt;
    for nod in range(1, n + 1):&lt;br /&gt;
        if not vizitat[nod]:&lt;br /&gt;
            componenta = []&lt;br /&gt;
            DFS(nod, vizitat, componenta, lista_adiacenta)&lt;br /&gt;
            componente.append(sorted(componenta))&lt;br /&gt;
&lt;br /&gt;
    return componente&lt;br /&gt;
&lt;br /&gt;
def verificare_restrictii(n, muchii):&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 100):&lt;br /&gt;
        return False&lt;br /&gt;
&lt;br /&gt;
    for i, j in muchii:&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;
&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;componenteconexeIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as fisier:&lt;br /&gt;
        n = int(fisier.readline().strip())&lt;br /&gt;
        muchii = [list(map(int, linie.strip().split())) for linie in fisier]&lt;br /&gt;
&lt;br /&gt;
    if not verificare_restrictii(n, muchii):&lt;br /&gt;
        with open(&amp;quot;componenteconexeOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier:&lt;br /&gt;
            fisier.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    componente = gaseste_componente_conexe(n, muchii)&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;quot;componenteconexeOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier:&lt;br /&gt;
        fisier.write(f&amp;quot;{len(componente)}\n&amp;quot;)&lt;br /&gt;
        for comp in componente:&lt;br /&gt;
            fisier.write(&amp;quot; &amp;quot;.join(map(str, comp)) + &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>Simina</name></author>
	</entry>
</feed>