Skip to content

Commit 73ef9e2

Browse files
committed
Sync LeetCode submission Runtime - 457 ms (71.88%), Memory - 104.3 MB (49.22%)
1 parent 57374ef commit 73ef9e2

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, labeled from <code>[0, n - 1]</code> and <code>[0, m - 1]</code>, respectively.</p>
2+
3+
<p>You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code> and <code>m - 1</code>, respectively, where <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the first tree and <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the second tree.</p>
4+
5+
<p>Node <code>u</code> is <strong>target</strong> to node <code>v</code> if the number of edges on the path from <code>u</code> to <code>v</code> is even.&nbsp;<strong>Note</strong> that a node is <em>always</em> <strong>target</strong> to itself.</p>
6+
7+
<p>Return an array of <code>n</code> integers <code>answer</code>, where <code>answer[i]</code> is the <strong>maximum</strong> possible number of nodes that are <strong>target</strong> to node <code>i</code> of the first tree if you had to connect one node from the first tree to another node in the second tree.</p>
8+
9+
<p><strong>Note</strong> that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<div class="example-block">
15+
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]]</span></p>
16+
17+
<p><strong>Output:</strong> <span class="example-io">[8,7,7,8,8]</span></p>
18+
19+
<p><strong>Explanation:</strong></p>
20+
21+
<ul>
22+
<li>For <code>i = 0</code>, connect node 0 from the first tree to node 0 from the second tree.</li>
23+
<li>For <code>i = 1</code>, connect node 1 from the first tree to node 4 from the second tree.</li>
24+
<li>For <code>i = 2</code>, connect node 2 from the first tree to node 7 from the second tree.</li>
25+
<li>For <code>i = 3</code>, connect node 3 from the first tree to node 0 from the second tree.</li>
26+
<li>For <code>i = 4</code>, connect node 4 from the first tree to node 4 from the second tree.</li>
27+
</ul>
28+
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/24/3982-1.png" style="width: 600px; height: 169px;" /></div>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]]</span></p>
34+
35+
<p><strong>Output:</strong> <span class="example-io">[3,6,6,6,6]</span></p>
36+
37+
<p><strong>Explanation:</strong></p>
38+
39+
<p>For every <code>i</code>, connect node <code>i</code> of the first tree with any node of the second tree.</p>
40+
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/24/3928-2.png" style="height: 281px; width: 500px;" /></div>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= n, m &lt;= 10<sup>5</sup></code></li>
47+
<li><code>edges1.length == n - 1</code></li>
48+
<li><code>edges2.length == m - 1</code></li>
49+
<li><code>edges1[i].length == edges2[i].length == 2</code></li>
50+
<li><code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li>
51+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
52+
<li><code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code></li>
53+
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; m</code></li>
54+
<li>The input is generated such that <code>edges1</code> and <code>edges2</code> represent valid trees.</li>
55+
</ul>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def maxTargetNodes(
3+
self, edges1: List[List[int]], edges2: List[List[int]]
4+
) -> List[int]:
5+
def dfs(node, parent, depth, children, color):
6+
res = 1 - depth % 2
7+
color[node] = depth % 2
8+
for child in children[node]:
9+
if child == parent:
10+
continue
11+
res += dfs(child, node, depth + 1, children, color)
12+
return res
13+
14+
def build(edges, color):
15+
n = len(edges) + 1
16+
children = [[] for _ in range(n)]
17+
for u, v in edges:
18+
children[u].append(v)
19+
children[v].append(u)
20+
res = dfs(0, -1, 0, children, color)
21+
return [res, n - res]
22+
23+
n = len(edges1) + 1
24+
m = len(edges2) + 1
25+
color1 = [0] * n
26+
color2 = [0] * m
27+
count1 = build(edges1, color1)
28+
count2 = build(edges2, color2)
29+
res = [0] * n
30+
for i in range(n):
31+
res[i] = count1[color1[i]] + max(count2[0], count2[1])
32+
return res

0 commit comments

Comments
 (0)