<?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=3127_%E2%80%93_Arbori_xor</id>
	<title>3127 – Arbori xor - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=3127_%E2%80%93_Arbori_xor"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=3127_%E2%80%93_Arbori_xor&amp;action=history"/>
	<updated>2026-05-02T06:07:55Z</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=3127_%E2%80%93_Arbori_xor&amp;diff=8913&amp;oldid=prev</id>
		<title>Corjuc Eunice: Pagină nouă: = Cerința = Se dă un arbore cu &lt;code&gt;n&lt;/code&gt; noduri, în care fiecare muchie are asociat un număr natural. Se cere răspunsul la &lt;code&gt;Q&lt;/code&gt; întrebări de forma: dacă &lt;code&gt;u&lt;/code&gt; şi &lt;code&gt;v&lt;/code&gt; sunt două noduri din arbore, care este valoarea &lt;code&gt;xor&lt;/code&gt; a tuturor numerelor asociate muchiilor situate pe lanţul ce uneşte &lt;code&gt;u&lt;/code&gt; şi &lt;code&gt;v&lt;/code&gt;?  = Date de intrare = Fișierul de intrare &lt;code&gt;arbori_xorIN.txt&lt;/code&gt; conține pe prima linie nume...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=3127_%E2%80%93_Arbori_xor&amp;diff=8913&amp;oldid=prev"/>
		<updated>2024-01-03T20:29:56Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: = Cerința = Se dă un arbore cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri, în care fiecare muchie are asociat un număr natural. Se cere răspunsul la &amp;lt;code&amp;gt;Q&amp;lt;/code&amp;gt; întrebări de forma: dacă &amp;lt;code&amp;gt;u&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;v&amp;lt;/code&amp;gt; sunt două noduri din arbore, care este valoarea &amp;lt;code&amp;gt;xor&amp;lt;/code&amp;gt; a tuturor numerelor asociate muchiilor situate pe lanţul ce uneşte &amp;lt;code&amp;gt;u&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;v&amp;lt;/code&amp;gt;?  = Date de intrare = Fișierul de intrare &amp;lt;code&amp;gt;arbori_xorIN.txt&amp;lt;/code&amp;gt; conține pe prima linie nume...&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 cu &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; noduri, în care fiecare muchie are asociat un număr natural. Se cere răspunsul la &amp;lt;code&amp;gt;Q&amp;lt;/code&amp;gt; întrebări de forma: dacă &amp;lt;code&amp;gt;u&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;v&amp;lt;/code&amp;gt; sunt două noduri din arbore, care este valoarea &amp;lt;code&amp;gt;xor&amp;lt;/code&amp;gt; a tuturor numerelor asociate muchiilor situate pe lanţul ce uneşte &amp;lt;code&amp;gt;u&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;v&amp;lt;/code&amp;gt;?&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;arbori_xorIN.txt&amp;lt;/code&amp;gt; conține pe prima linie numerele &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;Q&amp;lt;/code&amp;gt;, pe următoarele &amp;lt;code&amp;gt;n-1&amp;lt;/code&amp;gt; linii câte o pereche de numere naturale ce reprezintă extremităţile unei muchii din arbore urmată de un număr natural ce reprezintă valoarea muchiei, iar pe următoarele &amp;lt;code&amp;gt;Q&amp;lt;/code&amp;gt; linii câte o pereche de numere naturale ce reprezintă nodurile pentru care se cere &amp;lt;code&amp;gt;xor&amp;lt;/code&amp;gt;-ul valorilor de pe lanţul ce le uneşte .&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Fișierul de ieșire &amp;lt;code&amp;gt;arbori_xorOUT.txt&amp;lt;/code&amp;gt; va conține pe primele &amp;lt;code&amp;gt;Q&amp;lt;/code&amp;gt; linii răspunsurile la cele &amp;lt;code&amp;gt;Q&amp;lt;/code&amp;gt; cerinţe.&lt;br /&gt;
&lt;br /&gt;
= Restricții și precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;2 ≤ n ≤ 10.000&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ Q ≤ 500.000&amp;lt;/code&amp;gt;&lt;br /&gt;
* valorile asociate muchiilor sunt numere naturale cel mult egale cu &amp;lt;code&amp;gt;10.000&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exemplul 1 ==&lt;br /&gt;
arbori_xorIN.txt:&lt;br /&gt;
&lt;br /&gt;
5 2&lt;br /&gt;
&lt;br /&gt;
1 2 7&lt;br /&gt;
&lt;br /&gt;
1 3 4&lt;br /&gt;
&lt;br /&gt;
3 4 1&lt;br /&gt;
&lt;br /&gt;
4 5 13&lt;br /&gt;
&lt;br /&gt;
1 5&lt;br /&gt;
&lt;br /&gt;
3 2&lt;br /&gt;
&lt;br /&gt;
arbori_xorOUT.txt:&lt;br /&gt;
&lt;br /&gt;
8&lt;br /&gt;
&lt;br /&gt;
3&lt;br /&gt;
&lt;br /&gt;
Explicație: Pentru prima cerinţă avem &amp;lt;code&amp;gt;4 ^ 1 ^ 13 = 8&amp;lt;/code&amp;gt;, iar pentru a doua &amp;lt;code&amp;gt;4 ^ 7 = 3&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
arbori_xorIN.txt:&lt;br /&gt;
&lt;br /&gt;
99999999 2&lt;br /&gt;
&lt;br /&gt;
1 2 7&lt;br /&gt;
&lt;br /&gt;
1 3 4&lt;br /&gt;
&lt;br /&gt;
3 4 1&lt;br /&gt;
&lt;br /&gt;
4 5 13&lt;br /&gt;
&lt;br /&gt;
1 5&lt;br /&gt;
&lt;br /&gt;
3 2&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
Input conditions 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;
from collections import deque&lt;br /&gt;
&lt;br /&gt;
def bfs(nod):&lt;br /&gt;
    global coada, s, t&lt;br /&gt;
    coada.append(nod)&lt;br /&gt;
    while coada:&lt;br /&gt;
        nod = coada.popleft()&lt;br /&gt;
        for i in range(len(L[nod])):&lt;br /&gt;
            vecin, cost = L[nod][i]&lt;br /&gt;
            if t[nod] == vecin:&lt;br /&gt;
                continue&lt;br /&gt;
            coada.append(vecin)&lt;br /&gt;
            t[vecin] = nod&lt;br /&gt;
            s[vecin] = s[nod] ^ cost&lt;br /&gt;
&lt;br /&gt;
def query(a, b):&lt;br /&gt;
    return s[a] ^ s[b]&lt;br /&gt;
&lt;br /&gt;
def verify_conditions(n, q):&lt;br /&gt;
    if 2 &amp;lt;= n &amp;lt;= 10000 and 1 &amp;lt;= q &amp;lt;= 500000:&lt;br /&gt;
        return True&lt;br /&gt;
    else:&lt;br /&gt;
        return False&lt;br /&gt;
&lt;br /&gt;
fin = open(&amp;quot;arbori_xorIN.txt&amp;quot;, &amp;quot;r&amp;quot;)&lt;br /&gt;
fout = open(&amp;quot;arbori_xorOUT.txt&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
coada = deque()&lt;br /&gt;
L = [[] for _ in range(10009)]&lt;br /&gt;
s = [0] * 10009&lt;br /&gt;
t = [0] * 10009&lt;br /&gt;
&lt;br /&gt;
n, q = map(int, fin.readline().split())&lt;br /&gt;
&lt;br /&gt;
if not verify_conditions(n, q):&lt;br /&gt;
    print(&amp;quot;Input conditions not satisfied.&amp;quot;)&lt;br /&gt;
    fin.close()&lt;br /&gt;
    fout.close()&lt;br /&gt;
    exit()&lt;br /&gt;
&lt;br /&gt;
for _ in range(n-1):&lt;br /&gt;
    x, y, z = map(int, fin.readline().split())&lt;br /&gt;
    L[x].append((y, z))&lt;br /&gt;
    L[y].append((x, z))&lt;br /&gt;
&lt;br /&gt;
bfs(1)&lt;br /&gt;
&lt;br /&gt;
for _ in range(q):&lt;br /&gt;
    a, b = map(int, fin.readline().split())&lt;br /&gt;
    fout.write(str(query(a, b)) + &amp;#039;\n&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
fin.close()&lt;br /&gt;
fout.close()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Corjuc Eunice</name></author>
	</entry>
</feed>