<?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=4073_-_Componente_Conexe_5</id>
	<title>4073 - Componente Conexe 5 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4073_-_Componente_Conexe_5"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4073_-_Componente_Conexe_5&amp;action=history"/>
	<updated>2026-05-01T04:00:49Z</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=4073_-_Componente_Conexe_5&amp;diff=7984&amp;oldid=prev</id>
		<title>Simina: Pagină nouă: = Cerinţa = Se dă lista muchiilor unui graf neorientat cu &lt;code&gt;n&lt;/code&gt; noduri și &lt;code&gt;m&lt;/code&gt; muchii și un șir de &lt;code&gt;q&lt;/code&gt; noduri. Să se determine pentru fiecare nod &lt;code&gt;x&lt;/code&gt; din șir numărul de noduri din componenta conexă din care face parte &lt;code&gt;x&lt;/code&gt;.  = Date de intrare = Fişierul de intrare &lt;code&gt;componenteconexe5IN.txt&lt;/code&gt; conține pe prima linie numerele &lt;code&gt;n m&lt;/code&gt;, reprezentând numărul de noduri și numărul de muchii ale graful...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4073_-_Componente_Conexe_5&amp;diff=7984&amp;oldid=prev"/>
		<updated>2023-12-13T12:33:48Z</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 &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii și un șir de &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; noduri. Să se determine pentru fiecare nod &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; din șir numărul de noduri din componenta conexă din care face parte &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt;.  = Date de intrare = Fişierul de intrare &amp;lt;code&amp;gt;componenteconexe5IN.txt&amp;lt;/code&amp;gt; conține pe prima linie numerele &amp;lt;code&amp;gt;n m&amp;lt;/code&amp;gt;, reprezentând numărul de noduri și numărul de muchii ale graful...&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 &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; muchii și un șir de &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; noduri. Să se determine pentru fiecare nod &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; din șir numărul de noduri din componenta conexă din care face parte &amp;lt;code&amp;gt;x&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;componenteconexe5IN.txt&amp;lt;/code&amp;gt; conține pe prima linie numerele &amp;lt;code&amp;gt;n m&amp;lt;/code&amp;gt;, reprezentând numărul de noduri și numărul de muchii ale grafului. 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;q&amp;lt;/code&amp;gt;, iar ultima linie a fișierului de intrare conține un șir de &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; noduri, seprate prin câte un spațiu.&lt;br /&gt;
&lt;br /&gt;
= Date de ieşire =&lt;br /&gt;
Fişierul de ieşire &amp;lt;code&amp;gt;componenteconexe5OUT.txt&amp;lt;/code&amp;gt; va conține &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt; linii; fiecare linie va conține numărul de noduri din componenta conexă a nodului corespunzător din șir.&lt;br /&gt;
&lt;br /&gt;
= Restricţii şi precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ n ≤ 1000&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 ≤ q ≤ 1000&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ x ≤ n&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1 =&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexe5IN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 5 3&lt;br /&gt;
 1 2&lt;br /&gt;
 3 5&lt;br /&gt;
 2 4&lt;br /&gt;
 3&lt;br /&gt;
 2 5 1&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexe5OUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 3&lt;br /&gt;
 2&lt;br /&gt;
 3&lt;br /&gt;
&lt;br /&gt;
= Exemplul 2 =&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexe5IN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 1001 1001&lt;br /&gt;
&amp;lt;code&amp;gt;componenteconexe5OUT.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 verificare_restrictii(n, m, q, noduri_interes):&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 1000 and 1 &amp;lt;= m &amp;lt;= n * (n - 1) // 2 and 1 &amp;lt;= q &amp;lt;= 1000):&lt;br /&gt;
        return False&lt;br /&gt;
    for nod in noduri_interes:&lt;br /&gt;
        if not (1 &amp;lt;= nod &amp;lt;= n):&lt;br /&gt;
            return False&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def dfs(nod, graf, vizitat, componenta):&lt;br /&gt;
    vizitat[nod] = True&lt;br /&gt;
    componenta.append(nod)&lt;br /&gt;
    for vecin in graf[nod]:&lt;br /&gt;
        if not vizitat[vecin]:&lt;br /&gt;
            dfs(vecin, graf, vizitat, componenta)&lt;br /&gt;
&lt;br /&gt;
def gaseste_componente_conexe(graf, noduri_interes):&lt;br /&gt;
    componente = []&lt;br /&gt;
    vizitat = {nod: False for nod in graf}&lt;br /&gt;
    for nod in noduri_interes:&lt;br /&gt;
        if not vizitat[nod]:&lt;br /&gt;
            componenta = []&lt;br /&gt;
            dfs(nod, graf, vizitat, componenta)&lt;br /&gt;
            componente.append(componenta)&lt;br /&gt;
    return componente&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    # Citirea datelor de intrare&lt;br /&gt;
    with open(&amp;quot;componenteconexe5IN.txt&amp;quot;, &amp;quot;r&amp;quot;) as fisier:&lt;br /&gt;
        try:&lt;br /&gt;
            n, m = map(int, fisier.readline().split())&lt;br /&gt;
            graf = {i: [] for i in range(1, n + 1)}&lt;br /&gt;
            for _ in range(m):&lt;br /&gt;
                muchie = list(map(int, fisier.readline().split()))&lt;br /&gt;
                if len(muchie) != 2 or not (1 &amp;lt;= muchie[0] &amp;lt;= n and 1 &amp;lt;= muchie[1] &amp;lt;= n):&lt;br /&gt;
                    raise ValueError(&amp;quot;Datele nu corespund formatului asteptat.&amp;quot;)&lt;br /&gt;
                graf[muchie[0]].append(muchie[1])&lt;br /&gt;
                graf[muchie[1]].append(muchie[0])&lt;br /&gt;
            q = int(fisier.readline())&lt;br /&gt;
            noduri_interes = list(map(int, fisier.readline().split()))&lt;br /&gt;
        except ValueError as e:&lt;br /&gt;
            with open(&amp;quot;componenteconexe5OUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier_out:&lt;br /&gt;
                fisier_out.write(str(e))&lt;br /&gt;
            return&lt;br /&gt;
&lt;br /&gt;
    # Verificarea restricțiilor&lt;br /&gt;
    if not verificare_restrictii(n, m, q, noduri_interes):&lt;br /&gt;
        with open(&amp;quot;componenteconexe5OUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier_out:&lt;br /&gt;
            fisier_out.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    # Identificarea componentelor conexe&lt;br /&gt;
    componente = gaseste_componente_conexe(graf, set(noduri_interes))&lt;br /&gt;
&lt;br /&gt;
    # Scrierea rezultatelor în fișierul de ieșire&lt;br /&gt;
    with open(&amp;quot;componenteconexe5OUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as fisier_out:&lt;br /&gt;
        for nod in noduri_interes:&lt;br /&gt;
            for componenta in componente:&lt;br /&gt;
                if nod in componenta:&lt;br /&gt;
                    fisier_out.write(str(len(componenta)) + &amp;quot;\n&amp;quot;)&lt;br /&gt;
                    break&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Simina</name></author>
	</entry>
</feed>