<?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=0642_%E2%80%93_Det_Drum</id>
	<title>0642 – Det Drum - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=0642_%E2%80%93_Det_Drum"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0642_%E2%80%93_Det_Drum&amp;action=history"/>
	<updated>2026-06-17T10:03:45Z</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=0642_%E2%80%93_Det_Drum&amp;diff=8803&amp;oldid=prev</id>
		<title>Corjuc Eunice: Pagină nouă: = Cerința = Se dă vectorul de tați al unui arbore cu rădăcină cu &lt;code&gt;n&lt;/code&gt; noduri și un nod &lt;code&gt;k&lt;/code&gt;. Determinați drumul de la nodul &lt;code&gt;k&lt;/code&gt; la rădăcina arborelui.  = Date de intrare = Fișierul de intrare &lt;code&gt;detdrumIN.txt&lt;/code&gt; conține pe prima linie numărul de noduri &lt;code&gt;n&lt;/code&gt; și nodul &lt;code&gt;k&lt;/code&gt;. Pe linia următoare se află vectorul de tați al arborelui, valorile fiind separate prin spații.  = Date de ieșire = Fișierul de ie...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=0642_%E2%80%93_Det_Drum&amp;diff=8803&amp;oldid=prev"/>
		<updated>2024-01-02T21:28:54Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerința = Se dă vectorul de tați al unui arbore cu rădăcină cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri și un nod &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt;. Determinați drumul de la nodul &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; la rădăcina arborelui.  = Date de intrare = Fișierul de intrare &amp;lt;code&amp;gt;detdrumIN.txt&amp;lt;/code&amp;gt; conține pe prima linie numărul de noduri &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; și nodul &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt;. Pe linia următoare se află vectorul de tați al arborelui, valorile fiind separate prin spații.  = Date de ieșire = Fișierul de ie...&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ă vectorul de tați al unui arbore cu rădăcină cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri și un nod &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt;. Determinați drumul de la nodul &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; la rădăcina arborelui.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;detdrumIN.txt&amp;lt;/code&amp;gt; conține pe prima linie numărul de noduri &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; și nodul &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt;. Pe linia următoare se află vectorul de tați al arborelui, valorile fiind separate prin spații.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Fișierul de ieșire &amp;lt;code&amp;gt;detdrumOUT.txt&amp;lt;/code&amp;gt; va conține pe prima linie nodurile care alcătuiesc drumul determinat, separate printr-un spațiu.&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&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ k ≤ n&amp;lt;/code&amp;gt;&lt;br /&gt;
* în vectorul de tați rădăcina este marcată cu &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exemplul 1 ==&lt;br /&gt;
detdrumIN.txt:&lt;br /&gt;
&lt;br /&gt;
7 2&lt;br /&gt;
&lt;br /&gt;
4 1 7 0 7 7 1&lt;br /&gt;
&lt;br /&gt;
detdrumOUT.txt:&lt;br /&gt;
&lt;br /&gt;
2 1 4&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
detdrumIN.txt:&lt;br /&gt;
&lt;br /&gt;
101 2&lt;br /&gt;
&lt;br /&gt;
4 1 7 0 7 7 1&lt;br /&gt;
&lt;br /&gt;
detdrumOUT.txt:&lt;br /&gt;
&lt;br /&gt;
Invalid input. Constraints not satisfied.&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 verify_constraints(n, k):&lt;br /&gt;
    return 1 &amp;lt;= n &amp;lt;= 100 and 1 &amp;lt;= k &amp;lt;= n&lt;br /&gt;
&lt;br /&gt;
def solve_detdrum(n, k, t):&lt;br /&gt;
    a = [0] * 101&lt;br /&gt;
    rad = 0&lt;br /&gt;
&lt;br /&gt;
    for i in range(1, n + 1):&lt;br /&gt;
        if t[i - 1] == 0:&lt;br /&gt;
            rad = i&lt;br /&gt;
        a[i] = t[i - 1]&lt;br /&gt;
&lt;br /&gt;
    result = []&lt;br /&gt;
&lt;br /&gt;
    def afis(x, a):&lt;br /&gt;
        nonlocal result&lt;br /&gt;
        if x != 0 and t[x - 1]:&lt;br /&gt;
            result.append(x)&lt;br /&gt;
            afis(t[x - 1], a)&lt;br /&gt;
&lt;br /&gt;
    afis(k, a)&lt;br /&gt;
    result.append(rad)&lt;br /&gt;
    return result&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    with open(&amp;quot;detdrumIN.txt&amp;quot;, &amp;quot;r&amp;quot;) as infile, open(&amp;quot;detdrumOUT.txt&amp;quot;, &amp;quot;w&amp;quot;) as outfile:&lt;br /&gt;
        n, k = map(int, infile.readline().split())&lt;br /&gt;
&lt;br /&gt;
        if not verify_constraints(n, k):&lt;br /&gt;
            outfile.write(&amp;quot;Invalid input. Constraints not satisfied.&amp;quot;)&lt;br /&gt;
        else:&lt;br /&gt;
            t = list(map(int, infile.readline().split()))&lt;br /&gt;
&lt;br /&gt;
            result = solve_detdrum(n, k, t)&lt;br /&gt;
&lt;br /&gt;
            outfile.write(&amp;quot; &amp;quot;.join(map(str, result)))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Corjuc Eunice</name></author>
	</entry>
</feed>