diff --git a/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README.md b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README.md index 9de3232cc24a9..c9ae974cfe878 100644 --- a/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README.md +++ b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README.md @@ -216,17 +216,13 @@ public: g[u].push_back(v); g[v].push_back(u); } - auto max_node = [&](const vector& dist) { - int mx = ranges::max(dist); - return ranges::find(dist, mx) - dist.begin(); - }; vector dist1(n); dfs(0, -1, dist1); - int a = max_node(dist1); + int a = max_element(dist1.begin(), dist1.end()) - dist1.begin(); vector dist2(n); dfs(a, -1, dist2); - int b = max_node(dist2); + int b = max_element(dist2.begin(), dist2.end()) - dist2.begin(); vector dist3(n); dfs(b, -1, dist3); @@ -342,6 +338,48 @@ function lastMarkedNodes(edges: number[][]): number[] { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} edges + * @return {number[]} + */ +var lastMarkedNodes = function (edges) { + const n = edges.length + 1; + const g = Array.from({ length: n }, () => []); + for (const [u, v] of edges) { + g[u].push(v); + g[v].push(u); + } + const dfs = (i, fa, dist) => { + for (const j of g[i]) { + if (j !== fa) { + dist[j] = dist[i] + 1; + dfs(j, i, dist); + } + } + }; + + const dist1 = Array(n).fill(0); + dfs(0, -1, dist1); + const a = dist1.indexOf(Math.max(...dist1)); + + const dist2 = Array(n).fill(0); + dfs(a, -1, dist2); + const b = dist2.indexOf(Math.max(...dist2)); + + const dist3 = Array(n).fill(0); + dfs(b, -1, dist3); + + const ans = []; + for (let i = 0; i < n; ++i) { + ans.push(dist2[i] > dist3[i] ? a : b); + } + return ans; +}; +``` + diff --git a/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README_EN.md b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README_EN.md index 501e35597eb6b..56f91d6d149a4 100644 --- a/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README_EN.md +++ b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/README_EN.md @@ -216,17 +216,13 @@ public: g[u].push_back(v); g[v].push_back(u); } - auto max_node = [&](const vector& dist) { - int mx = ranges::max(dist); - return ranges::find(dist, mx) - dist.begin(); - }; vector dist1(n); dfs(0, -1, dist1); - int a = max_node(dist1); + int a = max_element(dist1.begin(), dist1.end()) - dist1.begin(); vector dist2(n); dfs(a, -1, dist2); - int b = max_node(dist2); + int b = max_element(dist2.begin(), dist2.end()) - dist2.begin(); vector dist3(n); dfs(b, -1, dist3); @@ -342,6 +338,48 @@ function lastMarkedNodes(edges: number[][]): number[] { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} edges + * @return {number[]} + */ +var lastMarkedNodes = function (edges) { + const n = edges.length + 1; + const g = Array.from({ length: n }, () => []); + for (const [u, v] of edges) { + g[u].push(v); + g[v].push(u); + } + const dfs = (i, fa, dist) => { + for (const j of g[i]) { + if (j !== fa) { + dist[j] = dist[i] + 1; + dfs(j, i, dist); + } + } + }; + + const dist1 = Array(n).fill(0); + dfs(0, -1, dist1); + const a = dist1.indexOf(Math.max(...dist1)); + + const dist2 = Array(n).fill(0); + dfs(a, -1, dist2); + const b = dist2.indexOf(Math.max(...dist2)); + + const dist3 = Array(n).fill(0); + dfs(b, -1, dist3); + + const ans = []; + for (let i = 0; i < n; ++i) { + ans.push(dist2[i] > dist3[i] ? a : b); + } + return ans; +}; +``` + diff --git a/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/Solution.cpp b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/Solution.cpp index e379295e8c4a9..4daf6f9e7f6be 100644 --- a/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/Solution.cpp +++ b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/Solution.cpp @@ -8,17 +8,13 @@ class Solution { g[u].push_back(v); g[v].push_back(u); } - auto max_node = [&](const vector& dist) { - int mx = ranges::max(dist); - return ranges::find(dist, mx) - dist.begin(); - }; vector dist1(n); dfs(0, -1, dist1); - int a = max_node(dist1); + int a = max_element(dist1.begin(), dist1.end()) - dist1.begin(); vector dist2(n); dfs(a, -1, dist2); - int b = max_node(dist2); + int b = max_element(dist2.begin(), dist2.end()) - dist2.begin(); vector dist3(n); dfs(b, -1, dist3); diff --git a/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/Solution.js b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/Solution.js new file mode 100644 index 0000000000000..1597956411cc5 --- /dev/null +++ b/solution/3300-3399/3313.Find the Last Marked Nodes in Tree/Solution.js @@ -0,0 +1,37 @@ +/** + * @param {number[][]} edges + * @return {number[]} + */ +var lastMarkedNodes = function (edges) { + const n = edges.length + 1; + const g = Array.from({ length: n }, () => []); + for (const [u, v] of edges) { + g[u].push(v); + g[v].push(u); + } + const dfs = (i, fa, dist) => { + for (const j of g[i]) { + if (j !== fa) { + dist[j] = dist[i] + 1; + dfs(j, i, dist); + } + } + }; + + const dist1 = Array(n).fill(0); + dfs(0, -1, dist1); + const a = dist1.indexOf(Math.max(...dist1)); + + const dist2 = Array(n).fill(0); + dfs(a, -1, dist2); + const b = dist2.indexOf(Math.max(...dist2)); + + const dist3 = Array(n).fill(0); + dfs(b, -1, dist3); + + const ans = []; + for (let i = 0; i < n; ++i) { + ans.push(dist2[i] > dist3[i] ? a : b); + } + return ans; +};