From 8b324cb21ac97ee57a61fce54c1dbf4a28ff0305 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:09:02 +0300 Subject: [PATCH 1/3] Added task 3772 --- .../Solution.java | 70 +++++++++++++++++++ .../readme.md | 67 ++++++++++++++++++ .../SolutionTest.java | 34 +++++++++ 3 files changed, 171 insertions(+) create mode 100644 src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java create mode 100644 src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/readme.md create mode 100644 src/test/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java new file mode 100644 index 000000000..644904e43 --- /dev/null +++ b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java @@ -0,0 +1,70 @@ +package g3701_3800.s3772_maximum_subgraph_score_in_a_tree; + +// #Hard #Array #Dynamic_Programming #Tree #Senior_Staff #Weekly_Contest_479 #Depth_First_Search +// #2026_05_08_Time_35_ms_(100.00%)_Space_279.46_MB_(74.36%) + +import java.util.Arrays; + +public class Solution { + public int[] maxSubgraphScore(int n, int[][] edges, int[] good) { + int[] h = new int[n]; + int[] e = new int[2 * (n - 1)]; + int[] ne = new int[2 * (n - 1)]; + int idx = 0; + Arrays.fill(h, -1); + for (int[] ed : edges) { + int a = ed[0], b = ed[1]; + e[idx] = b; + ne[idx] = h[a]; + h[a] = idx++; + e[idx] = a; + ne[idx] = h[b]; + h[b] = idx++; + } + int[] v = new int[n]; + for (int i = 0; i < n; i++) { + v[i] = good[i] == 1 ? 1 : -1; + } + int[] dp = new int[n]; + int[] p = new int[n]; + int[] ord = new int[n]; + int top = 0; + Arrays.fill(p, -1); + int[] st = new int[n]; + int sp = 0; + boolean[] vis = new boolean[n]; + st[sp++] = 0; + vis[0] = true; + while (sp > 0) { + int u = st[--sp]; + ord[top++] = u; + for (int i = h[u]; i != -1; i = ne[i]) { + int w = e[i]; + if (!vis[w]) { + vis[w] = true; + p[w] = u; + st[sp++] = w; + } + } + } + for (int i = n - 1; i >= 0; i--) { + int u = ord[i]; + dp[u] = v[u]; + for (int j = h[u]; j != -1; j = ne[j]) { + int w = e[j]; + if (p[w] == u && dp[w] > 0) { + dp[u] += dp[w]; + } + } + } + int[] ans = new int[n]; + ans[0] = dp[0]; + for (int i = 1; i < n; i++) { + int u = ord[i]; + int par = p[u]; + int pc = ans[par] - Math.max(0, dp[u]); + ans[u] = dp[u] + Math.max(0, pc); + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/readme.md b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/readme.md new file mode 100644 index 000000000..fd52c89c2 --- /dev/null +++ b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/readme.md @@ -0,0 +1,67 @@ +3772\. Maximum Subgraph Score in a Tree + +Hard + +You are given an **undirected tree** with `n` nodes, numbered from 0 to `n - 1`. It is represented by a 2D integer array `edges` of length `n - 1`, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. + +You are also given an integer array `good` of length `n`, where `good[i]` is 1 if the ith node is good, and 0 if it is bad. + +Define the **score** of a **subgraph** as the number of good nodes minus the number of bad nodes in that subgraph. + +For each node `i`, find the **maximum** possible score among all **connected subgraphs** that contain node `i`. + +Return an array of `n` integers where the ith element is the **maximum** score for node `i`. + +A **subgraph** is a graph whose vertices and edges are subsets of the original graph. + +A **connected subgraph** is a subgraph in which every pair of its vertices is reachable from one another using only its edges. + +**Example 1:** + +![Tree Example 1](https://assets.leetcode.com/uploads/2025/11/17/tree1fixed.png) + +**Input:** n = 3, edges = [[0,1],[1,2]], good = [1,0,1] + +**Output:** [1,1,1] + +**Explanation:** + +* Green nodes are good and red nodes are bad. +* For each node, the best connected subgraph containing it is the whole tree, which has 2 good nodes and 1 bad node, resulting in a score of 1. +* Other connected subgraphs containing a node may have the same score. + +**Example 2:** + +![Tree Example 2](https://assets.leetcode.com/uploads/2025/11/17/tree2.png) + +**Input:** n = 5, edges = [[1,0],[1,2],[1,3],[3,4]], good = [0,1,0,1,1] + +**Output:** [2,3,2,3,3] + +**Explanation:** + +* Node 0: The best connected subgraph consists of nodes `0, 1, 3, 4`, which has 3 good nodes and 1 bad node, resulting in a score of `3 - 1 = 2`. +* Nodes 1, 3, and 4: The best connected subgraph consists of nodes `1, 3, 4`, which has 3 good nodes, resulting in a score of 3. +* Node 2: The best connected subgraph consists of nodes `1, 2, 3, 4`, which has 3 good nodes and 1 bad node, resulting in a score of `3 - 1 = 2`. + +**Example 3:** + +![Tree Example 3](https://assets.leetcode.com/uploads/2025/11/17/tree3.png) + +**Input:** n = 2, edges = [[0,1]], good = [0,0] + +**Output:** [-1,-1] + +**Explanation:** + +For each node, including the other node only adds another bad node, so the best score for both nodes is -1. + +**Constraints:** + +* 2 <= n <= 105 +* `edges.length == n - 1` +* edges[i] = [ai, bi] +* 0 <= ai, bi < n +* `good.length == n` +* `0 <= good[i] <= 1` +* The input is generated such that `edges` represents a valid tree. \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/SolutionTest.java b/src/test/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/SolutionTest.java new file mode 100644 index 000000000..048e211ba --- /dev/null +++ b/src/test/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/SolutionTest.java @@ -0,0 +1,34 @@ +package g3701_3800.s3772_maximum_subgraph_score_in_a_tree; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxSubgraphScore() { + assertThat( + new Solution() + .maxSubgraphScore(3, new int[][] {{0, 1}, {1, 2}}, new int[] {1, 0, 1}), + equalTo(new int[] {1, 1, 1})); + } + + @Test + void maxSubgraphScore2() { + assertThat( + new Solution() + .maxSubgraphScore( + 5, + new int[][] {{1, 0}, {1, 2}, {1, 3}, {3, 4}}, + new int[] {0, 1, 0, 1, 1}), + equalTo(new int[] {2, 3, 2, 3, 3})); + } + + @Test + void maxSubgraphScore3() { + assertThat( + new Solution().maxSubgraphScore(2, new int[][] {{0, 1}}, new int[] {0, 0}), + equalTo(new int[] {-1, -1})); + } +} From 87da76d5f00221219114e560e9dbb3ba91359106 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:12:05 +0300 Subject: [PATCH 2/3] Fixed style --- .../s3772_maximum_subgraph_score_in_a_tree/Solution.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java index 644904e43..38212ee4d 100644 --- a/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java +++ b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java @@ -13,7 +13,8 @@ public int[] maxSubgraphScore(int n, int[][] edges, int[] good) { int idx = 0; Arrays.fill(h, -1); for (int[] ed : edges) { - int a = ed[0], b = ed[1]; + int a = ed[0]; + int b = ed[1]; e[idx] = b; ne[idx] = h[a]; h[a] = idx++; From 711f46596d24fce57955736012f65d6fa24d6e1b Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 May 2026 08:17:38 +0300 Subject: [PATCH 3/3] Fixed sonar --- .../s3772_maximum_subgraph_score_in_a_tree/Solution.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java index 38212ee4d..d1ec1d050 100644 --- a/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java +++ b/src/main/java/g3701_3800/s3772_maximum_subgraph_score_in_a_tree/Solution.java @@ -17,10 +17,12 @@ public int[] maxSubgraphScore(int n, int[][] edges, int[] good) { int b = ed[1]; e[idx] = b; ne[idx] = h[a]; - h[a] = idx++; + h[a] = idx; + idx++; e[idx] = a; ne[idx] = h[b]; - h[b] = idx++; + h[b] = idx; + idx++; } int[] v = new int[n]; for (int i = 0; i < n; i++) {