<?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=1691_-_Arbore_1</id>
	<title>1691 - Arbore 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=1691_-_Arbore_1"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1691_-_Arbore_1&amp;action=history"/>
	<updated>2026-05-01T06:38:44Z</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=1691_-_Arbore_1&amp;diff=9142&amp;oldid=prev</id>
		<title>Rus Marius: Pagină nouă: = Cerința = Se dă un arbore (graf conex aciclic) cu &lt;code&gt;N&lt;/code&gt; noduri. Vrem să eliminăm noduri (împreună cu muchiile adiacente) din arborele dat, astfel încât numărul de componente conexe ale grafului rămas să fie maxim. Aflați care este numărul maxim de componente conexe pe care le putem obține și câte submulțimi distincte de noduri se pot elimina din arbore astfel încât să rămână la final acest număr maxim de componente conexe.  = Date de intrare...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1691_-_Arbore_1&amp;diff=9142&amp;oldid=prev"/>
		<updated>2024-01-06T20:55:00Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerința = Se dă un arbore (graf conex aciclic) cu &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; noduri. Vrem să eliminăm noduri (împreună cu muchiile adiacente) din arborele dat, astfel încât numărul de componente conexe ale grafului rămas să fie maxim. Aflați care este numărul maxim de componente conexe pe care le putem obține și câte submulțimi distincte de noduri se pot elimina din arbore astfel încât să rămână la final acest număr maxim de componente conexe.  = Date de intrare...&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 (graf conex aciclic) cu &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt; noduri. Vrem să eliminăm noduri (împreună cu muchiile adiacente) din arborele dat, astfel încât numărul de componente conexe ale grafului rămas să fie maxim. Aflați care este numărul maxim de componente conexe pe care le putem obține și câte submulțimi distincte de noduri se pot elimina din arbore astfel încât să rămână la final acest număr maxim de componente conexe.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;arbore1IN.txt&amp;lt;/code&amp;gt; conține pe prima linie numărul natural &amp;lt;code&amp;gt;N&amp;lt;/code&amp;gt;, reprezentând numărul de noduri ale arborelui. Pe următoarele &amp;lt;code&amp;gt;N-1&amp;lt;/code&amp;gt; linii se vor afla câte două numere &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;Y&amp;lt;/code&amp;gt;, cu semnificația că există o muchie între nodurile &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;Y&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;arbore1OUT.txt&amp;lt;/code&amp;gt; va conține pe prima linie două numere naturale reprezentând numărul maxim de componente conexe pe care îl putem obține, respectiv numărul de moduri în care putem obține acest număr de componente conexe modulo &amp;lt;code&amp;gt;10^9+7&amp;lt;/code&amp;gt; (adică restul împărțirii acestui număr la &amp;lt;code&amp;gt;1000000007&amp;lt;/code&amp;gt;).În cazul în care restricțiile nu sunt îndeplinite, se va afișa mesajul &amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;.&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 000&amp;lt;/code&amp;gt;&lt;br /&gt;
* Se acordă 40% din punctajul unui test dacă numărul maxim de componente conexe este corect.&lt;br /&gt;
* Se acordă 60% din punctajul unui test dacă numărul de moduri este corect.&lt;br /&gt;
* Pentru 20% din teste &amp;lt;code&amp;gt;N ≤ 20&amp;lt;/code&amp;gt;&lt;br /&gt;
* Pentru alte 30% din teste &amp;lt;code&amp;gt;N ≤ 1000&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;arbore1IN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 6&lt;br /&gt;
 1 2&lt;br /&gt;
 1 3&lt;br /&gt;
 1 4&lt;br /&gt;
 4 5&lt;br /&gt;
 4 6&lt;br /&gt;
&amp;lt;code&amp;gt;arbore1OUT.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 4 1&lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
Se șterg nodurile &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt;. Nicio altă submulțime de noduri șterse nu produce &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt; sau mai multe componente conexe.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
&amp;lt;code&amp;gt;arbore1IN.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
 100001&lt;br /&gt;
 1 2&lt;br /&gt;
 1 3&lt;br /&gt;
 1 4&lt;br /&gt;
 4 5&lt;br /&gt;
 4 6&lt;br /&gt;
&amp;lt;code&amp;gt;arbore1OUT.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;
from collections import defaultdict&lt;br /&gt;
&lt;br /&gt;
N = 100100&lt;br /&gt;
mod = 1000000007&lt;br /&gt;
&lt;br /&gt;
v = defaultdict(list)&lt;br /&gt;
D = [[0] * N for _ in range(3)]&lt;br /&gt;
W = [[1] * N for _ in range(3)]&lt;br /&gt;
&lt;br /&gt;
def check_constraints(n):&lt;br /&gt;
    if not (1 &amp;lt;= n &amp;lt;= 100000):&lt;br /&gt;
        with open(&amp;quot;arbore1OUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as output_file:&lt;br /&gt;
            output_file.write(&amp;quot;Datele nu corespund restrictiilor impuse&amp;quot;)&lt;br /&gt;
        exit()&lt;br /&gt;
&lt;br /&gt;
def dfs(x, p):&lt;br /&gt;
    D[1][x] = 1&lt;br /&gt;
    D[0][x] = 0&lt;br /&gt;
    D[2][x] = 1&lt;br /&gt;
    W[1][x] = 1&lt;br /&gt;
    W[0][x] = 1&lt;br /&gt;
    W[2][x] = 1&lt;br /&gt;
    &lt;br /&gt;
    for it in v[x]:&lt;br /&gt;
        if it != p:&lt;br /&gt;
            dfs(it, x)&lt;br /&gt;
    &lt;br /&gt;
    # Cazul 1. Stergem nodul&lt;br /&gt;
    for it in v[x]:&lt;br /&gt;
        if it != p:&lt;br /&gt;
            D[0][x] += D[2][it]&lt;br /&gt;
            W[0][x] *= W[2][it]&lt;br /&gt;
            W[0][x] %= mod&lt;br /&gt;
    &lt;br /&gt;
    # Cazul 2. Nu stergem nodul&lt;br /&gt;
    for it in v[x]:&lt;br /&gt;
        if it != p:&lt;br /&gt;
            if D[0][it] + 1 == D[1][it]:&lt;br /&gt;
                W[1][x] *= (W[0][it] + W[1][it]) % mod&lt;br /&gt;
                W[1][x] %= mod&lt;br /&gt;
                D[1][x] += D[0][it]&lt;br /&gt;
            elif D[0][it] + 1 &amp;lt; D[1][it]:&lt;br /&gt;
                W[1][x] *= W[1][it]&lt;br /&gt;
                W[1][x] %= mod&lt;br /&gt;
                D[1][x] += D[1][it] - 1&lt;br /&gt;
            else:&lt;br /&gt;
                W[1][x] *= W[0][it]&lt;br /&gt;
                W[1][x] %= mod&lt;br /&gt;
                D[1][x] += D[0][it]&lt;br /&gt;
    &lt;br /&gt;
    # Calculam solutia pentru acest subarbore stiind cele 2 cazuri&lt;br /&gt;
    D[2][x] = max(D[1][x], D[0][x])&lt;br /&gt;
    if D[1][x] == D[0][x]:&lt;br /&gt;
        W[2][x] = (W[1][x] + W[0][x]) % mod&lt;br /&gt;
    elif D[1][x] &amp;gt; D[0][x]:&lt;br /&gt;
        W[2][x] = W[1][x]&lt;br /&gt;
    else:&lt;br /&gt;
        W[2][x] = W[0][x]&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;arbore1IN.txt&amp;quot;, &amp;quot;r&amp;quot;) as input_file:&lt;br /&gt;
        n = int(input_file.readline().strip())&lt;br /&gt;
        check_constraints(n)&lt;br /&gt;
        &lt;br /&gt;
        for _ in range(n-1):&lt;br /&gt;
            x, y = map(int, input_file.readline().strip().split())&lt;br /&gt;
            v[x].append(y)&lt;br /&gt;
            v[y].append(x)&lt;br /&gt;
&lt;br /&gt;
    dfs(1, 0)&lt;br /&gt;
&lt;br /&gt;
    with open(&amp;quot;arbore1OUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as output_file:&lt;br /&gt;
        output_file.write(f&amp;quot;{D[2][1]} {W[2][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>Rus Marius</name></author>
	</entry>
</feed>