|
| 1 | +/** |
| 2 | + * 3373. Maximize the Number of Target Nodes After Connecting Trees II |
| 3 | + * https://leetcode.com/problems/maximize-the-number-of-target-nodes-after-connecting-trees-ii/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * There exist two undirected trees with n and m nodes, labeled from [0, n - 1] and [0, m - 1], |
| 7 | + * respectively. |
| 8 | + * |
| 9 | + * You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, |
| 10 | + * where edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first |
| 11 | + * tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the |
| 12 | + * second tree. |
| 13 | + * |
| 14 | + * Node u is target to node v if the number of edges on the path from u to v is even. Note that |
| 15 | + * a node is always target to itself. |
| 16 | + * |
| 17 | + * Return an array of n integers answer, where answer[i] is the maximum possible number of nodes |
| 18 | + * that are target to node i of the first tree if you had to connect one node from the first tree |
| 19 | + * to another node in the second tree. |
| 20 | + * |
| 21 | + * Note that queries are independent from each other. That is, for every query you will remove the |
| 22 | + * added edge before proceeding to the next query. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {number[][]} edges1 |
| 27 | + * @param {number[][]} edges2 |
| 28 | + * @return {number[]} |
| 29 | + */ |
| 30 | +var maxTargetNodes = function(edges1, edges2) { |
| 31 | + const graph1 = buildGraph(edges1); |
| 32 | + const graph2 = buildGraph(edges2); |
| 33 | + const n = graph1.length; |
| 34 | + const { color: color1 } = getBipartiteGroups(graph1); |
| 35 | + const { maxGroup: maxGroup2 } = getBipartiteGroups(graph2); |
| 36 | + const group0Count = color1.filter(c => c === 0).length; |
| 37 | + const group1Count = n - group0Count; |
| 38 | + const result = new Array(n); |
| 39 | + |
| 40 | + for (let i = 0; i < n; i++) { |
| 41 | + result[i] = (color1[i] === 0 ? group0Count : group1Count) + maxGroup2; |
| 42 | + } |
| 43 | + |
| 44 | + return result; |
| 45 | + |
| 46 | + function buildGraph(edges) { |
| 47 | + const n = edges.length + 1; |
| 48 | + const graph = Array.from({ length: n }, () => []); |
| 49 | + for (const [u, v] of edges) { |
| 50 | + graph[u].push(v); |
| 51 | + graph[v].push(u); |
| 52 | + } |
| 53 | + return graph; |
| 54 | + } |
| 55 | + |
| 56 | + function getBipartiteGroups(graph) { |
| 57 | + const n = graph.length; |
| 58 | + const color = new Array(n).fill(-1); |
| 59 | + const groups = [0, 0]; |
| 60 | + |
| 61 | + for (let i = 0; i < n; i++) { |
| 62 | + if (color[i] === -1) { |
| 63 | + dfs(i, 0); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return { color, maxGroup: Math.max(groups[0], groups[1]) }; |
| 68 | + |
| 69 | + function dfs(node, c) { |
| 70 | + color[node] = c; |
| 71 | + groups[c]++; |
| 72 | + for (const neighbor of graph[node]) { |
| 73 | + if (color[neighbor] === -1) { |
| 74 | + dfs(neighbor, 1 - c); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +}; |
0 commit comments