java.lang.Object
g2501_2600.s2508_add_edges_to_make_degrees_of_all_nodes_even.Solution

public class Solution extends Object
2508 - Add Edges to Make Degrees of All Nodes Even.<p>Hard</p> <p>There is an <strong>undirected</strong> graph consisting of <code>n</code> nodes numbered from <code>1</code> to <code>n</code>. You are given the integer <code>n</code> and a <strong>2D</strong> array <code>edges</code> where <code>edges[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>. The graph can be disconnected.</p> <p>You can add <strong>at most</strong> two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops.</p> <p>Return <code>true</code> <em>if it is possible to make the degree of each node in the graph even, otherwise return</em> <code>false</code><em>.</em></p> <p>The degree of a node is the number of edges connected to it.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/10/26/agraphdrawio.png" alt="" /></p> <p><strong>Input:</strong> n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The above diagram shows a valid way of adding an edge. Every node in the resulting graph is connected to an even number of edges.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/10/26/aagraphdrawio.png" alt="" /></p> <p><strong>Input:</strong> n = 4, edges = [[1,2],[3,4]]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The above diagram shows a valid way of adding two edges.</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/10/26/aaagraphdrawio.png" alt="" /></p> <p><strong>Input:</strong> n = 4, edges = [[1,2],[1,3],[1,4]]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> It is not possible to obtain a valid graph with adding at most 2 edges.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 <= n <= 10<sup>5</sup></code></li> <li><code>2 <= edges.length <= 10<sup>5</sup></code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li>There are no repeated edges.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isPossible

      public boolean isPossible(int n, List<List<Integer>> edges)