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
4073 - Componente Conexe 5
(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!
== Rezolvare == <syntaxhighlight lang="python3" line="1"> def verificare_restrictii(n, m, q, noduri_interes): if not (1 <= n <= 1000 and 1 <= m <= n * (n - 1) // 2 and 1 <= q <= 1000): return False for nod in noduri_interes: if not (1 <= nod <= n): return False return True def dfs(nod, graf, vizitat, componenta): vizitat[nod] = True componenta.append(nod) for vecin in graf[nod]: if not vizitat[vecin]: dfs(vecin, graf, vizitat, componenta) def gaseste_componente_conexe(graf, noduri_interes): componente = [] vizitat = {nod: False for nod in graf} for nod in noduri_interes: if not vizitat[nod]: componenta = [] dfs(nod, graf, vizitat, componenta) componente.append(componenta) return componente def main(): # Citirea datelor de intrare with open("componenteconexe5IN.txt", "r") as fisier: try: n, m = map(int, fisier.readline().split()) graf = {i: [] for i in range(1, n + 1)} for _ in range(m): muchie = list(map(int, fisier.readline().split())) if len(muchie) != 2 or not (1 <= muchie[0] <= n and 1 <= muchie[1] <= n): raise ValueError("Datele nu corespund formatului asteptat.") graf[muchie[0]].append(muchie[1]) graf[muchie[1]].append(muchie[0]) q = int(fisier.readline()) noduri_interes = list(map(int, fisier.readline().split())) except ValueError as e: with open("componenteconexe5OUT.txt", "w") as fisier_out: fisier_out.write(str(e)) return # Verificarea restricțiilor if not verificare_restrictii(n, m, q, noduri_interes): with open("componenteconexe5OUT.txt", "w") as fisier_out: fisier_out.write("Datele nu corespund restrictiilor impuse") return # Identificarea componentelor conexe componente = gaseste_componente_conexe(graf, set(noduri_interes)) # Scrierea rezultatelor în fișierul de ieșire with open("componenteconexe5OUT.txt", "w") as fisier_out: for nod in noduri_interes: for componenta in componente: if nod in componenta: fisier_out.write(str(len(componenta)) + "\n") break 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