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
4204 - Este Arbore
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!
== Cerinţa == Verificați dacă un graf este arbore sau nu. == Date de intrare == Fișierul de intrare '''estearborein.txt''' conține pe prima linie numărul de noduri '''n''', iar pe următoarele linii perechi de numere '''x y''', separate printr-un spațiu, cu semnificația că există muchie de la nodul '''x''' la nodul '''y'''. == Date de ieșire == Fișierul de ieșire '''estearboreout.txt''' va conține pe prima linie cuvântul '''DA''' dacă graful poate fi arbore, sau cuvântul '''NU''' dacă graful nu este arbore. == Restricţii şi precizări == * '''1 ⩽ n ⩽ 100''' * '''1 ⩽ x, y ⩽ n''' * muchiile se pot repeta == Exemplul 1 == ;'''estearborein.txt''' 5 1 3 2 4 3 1 3 5 4 2 4 5 ;'''estearboreout.txt''' Datele de intrare corespund restrictiilor impuse DA == Exemplul 1 == ;'''estearborein.txt''' 5 1 2 2 3 3 4 4 105 5 1 ;'''estearboreout.txt''' Datele de intrare nu corespund restrictiilor impuse == Rezolvare == <syntaxhighlight lang="python" line> from collections import defaultdict def este_arbore(numar_noduri, lista_muchii): vizitat = [False] * (numar_noduri + 1) graf = defaultdict(list) for nod_x, nod_y in lista_muchii: graf[nod_x].append(nod_y) graf[nod_y].append(nod_x) if este_ciclu(1, -1, graf, vizitat): return False for i in range(1, numar_noduri + 1): if not vizitat[i]: return False return True def este_ciclu(nod, parinte, graf, vizitat): vizitat[nod] = True for i in graf[nod]: if not vizitat[i]: if este_ciclu(i, nod, graf, vizitat): return True elif i != parinte: return True return False def main(): with open('estearborein.txt', 'r') as f: n = int(f.readline()) muchii = [] for _ in range(n): x, y = map(int, f.readline().split()) muchii.append((x, y)) if 1 <= n <= 100 and all(1 <= x <= n and 1 <= y <= n for x, y in muchii): print("Datele de intrare corespund restrictiilor impuse") with open('estearboreout.txt', 'w') as f: if este_arbore(n, muchii): f.write("NU\n") else: f.write("DA\n") else: print("Datele de intrare nu corespund restrictiilor impuse") if __name__ == "__main__": main() </syntaxhighlight> == Explicatie == Graful poate fi arbore.
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