<?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=3133_%E2%80%93_Arbori_nr</id>
	<title>3133 – Arbori nr - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=3133_%E2%80%93_Arbori_nr"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=3133_%E2%80%93_Arbori_nr&amp;action=history"/>
	<updated>2026-05-01T04:54:38Z</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=3133_%E2%80%93_Arbori_nr&amp;diff=8927&amp;oldid=prev</id>
		<title>Corjuc Eunice: Pagină nouă: = Cerința = Se dă un arbore cu &lt;code&gt;n&lt;/code&gt; noduri şi rădăcina &lt;code&gt;r&lt;/code&gt;, nodurile fiind etichetate cu numerele de la &lt;code&gt;1&lt;/code&gt; la &lt;code&gt;n&lt;/code&gt;. Se cere să se afle pentru fiecare nod &lt;code&gt;i&lt;/code&gt;, câte noduri din subarborele cu rădăcina &lt;code&gt;i&lt;/code&gt; au eticheta mai mică decât &lt;code&gt;i&lt;/code&gt;.  = Date de intrare = Fișierul de intrare &lt;code&gt;arbori_nrIN.txt&lt;/code&gt; conține pe prima linie numerele &lt;code&gt;n&lt;/code&gt; şi &lt;code&gt;r&lt;/code&gt;, iar pe următoarele...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=3133_%E2%80%93_Arbori_nr&amp;diff=8927&amp;oldid=prev"/>
		<updated>2024-01-03T21:22:26Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerința = Se dă un arbore cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri şi rădăcina &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, nodurile fiind etichetate cu numerele de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;. Se cere să se afle pentru fiecare nod &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, câte noduri din subarborele cu rădăcina &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; au eticheta mai mică decât &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;.  = Date de intrare = Fișierul de intrare &amp;lt;code&amp;gt;arbori_nrIN.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;r&amp;lt;/code&amp;gt;, iar pe următoarele...&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ă un arbore cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri şi rădăcina &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;, nodurile fiind etichetate cu numerele de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;. Se cere să se afle pentru fiecare nod &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, câte noduri din subarborele cu rădăcina &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; au eticheta mai mică decât &amp;lt;code&amp;gt;i&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;arbori_nrIN.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;r&amp;lt;/code&amp;gt;, iar pe următoarele &amp;lt;code&amp;gt;n-1&amp;lt;/code&amp;gt; linii câte o pereche de numere &amp;lt;code&amp;gt;u&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;v&amp;lt;/code&amp;gt;, reprezentând faptul că între nodurile cu etichetele &amp;lt;code&amp;gt;u&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;v&amp;lt;/code&amp;gt; există o muchie.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Fișierul de ieșire &amp;lt;code&amp;gt;arbori_nrOUT.txt&amp;lt;/code&amp;gt; va conține pe prima linie &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; numere naturale, al &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;-lea număr reprezentând numărul de noduri din subarborele cu eticheta &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, ce au etichetele mai mici decât &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Restricții și precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;3 ≤ n ≤ 100.000&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ u,v ≤ n&amp;lt;/code&amp;gt;, distincte&lt;br /&gt;
&lt;br /&gt;
== Exemplul 1 ==&lt;br /&gt;
arbori_nrIN.txt:&lt;br /&gt;
&lt;br /&gt;
5 2&lt;br /&gt;
&lt;br /&gt;
2 3&lt;br /&gt;
&lt;br /&gt;
5 4&lt;br /&gt;
&lt;br /&gt;
5 1&lt;br /&gt;
&lt;br /&gt;
2 5&lt;br /&gt;
&lt;br /&gt;
arbori_nrOUT.txt:&lt;br /&gt;
&lt;br /&gt;
0 1 0 0 2&lt;br /&gt;
&lt;br /&gt;
Explicație:&lt;br /&gt;
&lt;br /&gt;
În arbore &amp;lt;code&amp;gt;1,3,4&amp;lt;/code&amp;gt; sunt frunze, deci au câte &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; noduri în subarborii cu rădăcinile &amp;lt;code&amp;gt;1, 3&amp;lt;/code&amp;gt; respectiv &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;2&amp;lt;/code&amp;gt; are în subarbore nodul &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; cu eticheta mai mică decât &amp;lt;code&amp;gt;2&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; are în subarbore două noduri cu etichetele mai mici decât &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt;, nodurile &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
arbori_nrIN.txt:&lt;br /&gt;
&lt;br /&gt;
999999999 2&lt;br /&gt;
&lt;br /&gt;
2 3&lt;br /&gt;
&lt;br /&gt;
5 4&lt;br /&gt;
&lt;br /&gt;
5 1&lt;br /&gt;
&lt;br /&gt;
2 5&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
Error: n should be between 3 and 100000 (inclusive).&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 validate_conditions(n):&lt;br /&gt;
    if not (3 &amp;lt;= n &amp;lt;= 100000):&lt;br /&gt;
        print(&amp;quot;Error: n should be between 3 and 100000 (inclusive).&amp;quot;)&lt;br /&gt;
        return False&lt;br /&gt;
    &lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
def update(aib, pos, val):&lt;br /&gt;
    while pos &amp;lt;= len(aib):&lt;br /&gt;
        aib[pos] += val&lt;br /&gt;
        pos += pos &amp;amp; -pos&lt;br /&gt;
&lt;br /&gt;
def query(aib, pos):&lt;br /&gt;
    sum_val = 0&lt;br /&gt;
    while pos &amp;gt; 0:&lt;br /&gt;
        sum_val += aib[pos]&lt;br /&gt;
        pos -= pos &amp;amp; -pos&lt;br /&gt;
    return sum_val&lt;br /&gt;
&lt;br /&gt;
def dfs1(node, dad, G, aib, aiblant, rez, lant):&lt;br /&gt;
    update(aib, node, 1)&lt;br /&gt;
    update(aiblant, node, 1)&lt;br /&gt;
    rez[node] += query(aib, node - 1)&lt;br /&gt;
    for next_node in G[node]:&lt;br /&gt;
        if next_node != dad:&lt;br /&gt;
            dfs1(next_node, node, G, aib, aiblant, rez, lant)&lt;br /&gt;
    update(aiblant, node, -1)&lt;br /&gt;
    lant[node] = query(aiblant, node)&lt;br /&gt;
&lt;br /&gt;
def dfs2(node, dad, G, aib, rez):&lt;br /&gt;
    update(aib, node, 1)&lt;br /&gt;
    rez[node] += query(aib, node - 1)&lt;br /&gt;
    for next_node in G[node][::-1]:&lt;br /&gt;
        if next_node != dad:&lt;br /&gt;
            dfs2(next_node, node, G, aib, rez)&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;arbori_nrIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as f:&lt;br /&gt;
        n, r = map(int, f.readline().split())&lt;br /&gt;
        if not validate_conditions(n):&lt;br /&gt;
            return&lt;br /&gt;
        u, v = 0, 0  # Initialize u and v&lt;br /&gt;
        G = [[] for _ in range(n + 1)]&lt;br /&gt;
        rez = [0] * (n + 1)&lt;br /&gt;
        lant = [0] * (n + 1)&lt;br /&gt;
        aib = [0] * (4 * n + 1)&lt;br /&gt;
        aiblant = [0] * (4 * n + 1)&lt;br /&gt;
        for _ in range(n - 1):&lt;br /&gt;
            x, y = map(int, f.readline().split())&lt;br /&gt;
            G[x].append(y)&lt;br /&gt;
            G[y].append(x)&lt;br /&gt;
            # Update u and v based on the first edge&lt;br /&gt;
            if u == 0:&lt;br /&gt;
                u, v = x, y&lt;br /&gt;
&lt;br /&gt;
    dfs1(r, 0, G, aib, aiblant, rez, lant)&lt;br /&gt;
&lt;br /&gt;
    for i in range(4 * n):&lt;br /&gt;
        aib[i] = 0&lt;br /&gt;
&lt;br /&gt;
    dfs2(r, 0, G, aib, rez)&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;quot;arbori_nrOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as g:&lt;br /&gt;
        for i in range(1, n + 1):&lt;br /&gt;
            g.write(f&amp;quot;{i - rez[i] + lant[i] - 1} &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>Corjuc Eunice</name></author>
	</entry>
</feed>