Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
0649 – Subarbori
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Explicație = Singura pereche de noduri neterminale pentru care subarborii au același număr de noduri este <code>2 1</code>. Subarborii cu rădăcina în <code>2</code> și <code>1</code> au câte <code>3</code> noduri fiecare. == Exemplul 2: == <code>subarboriIN.txt</code> 101 4 3 0 3 2 1 2 1 <code>subarboriOUT.txt</code> Datele nu corespund restrictiilor impuse == Rezolvare == <syntaxhighlight lang="python3"> class NodArbore: def __init__(self, valoare): self.valoare = valoare self.copii = [] def construieste_arbore(tati): noduri = [NodArbore(i) for i in range(len(tati))] for i, tata in enumerate(tati): if tata != 0: noduri[tata - 1].copii.append(noduri[i]) return noduri[0] # returnează rădăcina arborelui def numara_subarbori_cu_acelasi_numar_de_noduri(nod): if not nod.copii: return 1, {1: 1} # tuplu: (număr de noduri, dicționar cu numărul de subarbori de acea dimensiune) total_noduri = 1 dimensiuni_subarbori = {} for copil in nod.copii: dimensiune_copil, dimensiuni_subarbori_copil = numara_subarbori_cu_acelasi_numar_de_noduri(copil) total_noduri += dimensiune_copil for dimensiune, numar in dimensiuni_subarbori_copil.items(): dimensiuni_subarbori[dimensiune] = dimensiuni_subarbori.get(dimensiune, 0) + numar dimensiuni_subarbori[total_noduri] = dimensiuni_subarbori.get(total_noduri, 0) + 1 return total_noduri, dimensiuni_subarbori def verificare_restricții(n): if not (1 <= n <= 100): with open("subarboriOUT.txt", "w") as fisier_iesire: fisier_iesire.write("Datele nu corespund restrictiilor impuse") exit() def main(): with open("subarboriIN.txt", "r") as fisier_intrare: n = int(fisier_intrare.readline().strip()) verificare_restricții(n) tati = list(map(int, fisier_intrare.readline().strip().split())) radacina = construieste_arbore(tati) _, dimensiuni_subarbori = numara_subarbori_cu_acelasi_numar_de_noduri(radacina) rezultat = sum(numar * (numar - 1) // 2 for numar in dimensiuni_subarbori.values()) with open("subarboriOUT.txt", "w") as fisier_iesire: fisier_iesire.write(str(rezultat)) if __name__ == "__main__": main() </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width