<?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=0441_-_Componente_Conexe_1</id>
	<title>0441 - Componente Conexe 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=0441_-_Componente_Conexe_1"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0441_-_Componente_Conexe_1&amp;action=history"/>
	<updated>2026-05-01T07:54:56Z</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=0441_-_Componente_Conexe_1&amp;diff=7972&amp;oldid=prev</id>
		<title>Simina: Pagină nouă: = Cerinţa = Se dă lista muchiilor unui graf neorientat. Să se determine numărul minim de muchii care trebuie adăugate pentru ca graful să devină conex, precum și un set de asemenea muchii.  = Date de intrare = Fişierul de intrare &lt;code&gt;componenteconexe1IN.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...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0441_-_Componente_Conexe_1&amp;diff=7972&amp;oldid=prev"/>
		<updated>2023-12-13T10:48:04Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerinţa = Se dă lista muchiilor unui graf neorientat. Să se determine numărul minim de muchii care trebuie adăugate pentru ca graful să devină conex, precum și un set de asemenea muchii.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;componenteconexe1IN.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...&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 determine numărul minim de muchii care trebuie adăugate pentru ca graful să devină conex, precum și un set de asemenea muchii.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fişierul de intrare &amp;lt;code&amp;gt;componenteconexe1IN.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;componenteconexe1OUT.txt&amp;lt;/code&amp;gt; va conţine pe prima linie numărul &amp;lt;code&amp;gt;NR&amp;lt;/code&amp;gt; de muchii ce trebuie adăugate. Fiecare dintre următoarele &amp;lt;code&amp;gt;NR&amp;lt;/code&amp;gt; linii va conține câte o muchie &amp;lt;code&amp;gt;x y&amp;lt;/code&amp;gt;, care trebuie adăugată pentru ca graful să devină conex.&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;componenteconexe1IN.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;componenteconexe1OUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 1&lt;br /&gt;
 1 3&lt;br /&gt;
&lt;br /&gt;
= Exemplul 2 =&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexe1IN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 101&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexe1OUT.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 gaseste_componente_conexe(n, muchii):&lt;br /&gt;
    componente_conexe = []&lt;br /&gt;
&lt;br /&gt;
    def gaseste_componenta(v):&lt;br /&gt;
        for componenta in componente_conexe:&lt;br /&gt;
            if v in componenta:&lt;br /&gt;
                return componenta&lt;br /&gt;
        return None&lt;br /&gt;
&lt;br /&gt;
    for muchie in muchii:&lt;br /&gt;
        i, j = muchie&lt;br /&gt;
        componente_i = gaseste_componenta(i)&lt;br /&gt;
        componente_j = gaseste_componenta(j)&lt;br /&gt;
&lt;br /&gt;
        if componente_i is not None and componente_j is not None:&lt;br /&gt;
            if componente_i != componente_j:&lt;br /&gt;
                componente_i.update(componente_j)&lt;br /&gt;
                componente_conexe.remove(componente_j)&lt;br /&gt;
        elif componente_i is not None:&lt;br /&gt;
            componente_i.add(j)&lt;br /&gt;
        elif componente_j is not None:&lt;br /&gt;
            componente_j.add(i)&lt;br /&gt;
        else:&lt;br /&gt;
            componente_conexe.append({i, j})&lt;br /&gt;
&lt;br /&gt;
    return componente_conexe&lt;br /&gt;
&lt;br /&gt;
def determina_muchii_aditionale(n, muchii):&lt;br /&gt;
    componente_conexe = gaseste_componente_conexe(n, muchii)&lt;br /&gt;
&lt;br /&gt;
    nr_muchii_aditionale = len(componente_conexe) - 1&lt;br /&gt;
    muchii_aditionale = []&lt;br /&gt;
&lt;br /&gt;
    for i in range(len(componente_conexe) - 1):&lt;br /&gt;
        muchie = (min(componente_conexe[i]), min(componente_conexe[i + 1]))&lt;br /&gt;
        muchii_aditionale.append(muchie)&lt;br /&gt;
&lt;br /&gt;
    return nr_muchii_aditionale, muchii_aditionale&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;componenteconexe1IN.txt&amp;quot;, &amp;quot;r&amp;quot;) as file_in:&lt;br /&gt;
        n = int(file_in.readline().strip())&lt;br /&gt;
        muchii = [tuple(map(int, line.split())) for line in file_in.readlines()]&lt;br /&gt;
&lt;br /&gt;
    if not verificare_restrictii(n, muchii):&lt;br /&gt;
        with open(&amp;quot;componenteconexe1OUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as file_out:&lt;br /&gt;
            file_out.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    nr_muchii_aditionale, muchii_aditionale = determina_muchii_aditionale(n, muchii)&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;quot;componenteconexe1OUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as file_out:&lt;br /&gt;
        file_out.write(str(nr_muchii_aditionale) + &amp;quot;\n&amp;quot;)&lt;br /&gt;
        for muchie in muchii_aditionale:&lt;br /&gt;
            file_out.write(f&amp;quot;{muchie[0]} {muchie[1]}\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>