<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>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/agraphdrawio.png" style="width: 500px; height: 190px;" />
<pre>
<strong>Input:</strong> n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]
<strong>Output:</strong> true
<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.
</pre>

<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aagraphdrawio.png" style="width: 400px; height: 120px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[1,2],[3,4]]
<strong>Output:</strong> true
<strong>Explanation:</strong> The above diagram shows a valid way of adding two edges.</pre>

<p><strong>Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aaagraphdrawio.png" style="width: 150px; height: 158px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[1,2],[1,3],[1,4]]
<strong>Output:</strong> false
<strong>Explanation:</strong> It is not possible to obtain a valid graph with adding at most 2 edges.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>2 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>1 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>There are no repeated edges.</li>
</ul>

class Solution:
    def isPossible(self, n: int, edges: List[List[int]]) -> bool:
        find_isolated_node = [0] * n
        node_nei = defaultdict(set)
        degree = defaultdict(int)
        for edge in edges:
            node_nei[edge[0]].add(edge[1])
            degree[edge[0]] += 1
            node_nei[edge[1]].add(edge[0])
            degree[edge[1]] += 1
            find_isolated_node[edge[0]-1] = 1
            find_isolated_node[edge[1]-1] = 1
        isolated_node = []
        for i, val in enumerate(find_isolated_node):
            if val == 0:
                isolated_node.append(i+1)
        
        for node in isolated_node:
            degree[node] = 0
            node_nei[node] = set([])


        odd_node = []
        for node, deg in degree.items():
            if deg%2 == 1:
                odd_node.append(node)

        ### No need to add edge
        if len(odd_node) == 0:
            return True

        ### We need one edge, two nodes are having odd degree and there is no edge between them
        if len(odd_node) == 2:
            if odd_node[1] not in node_nei[odd_node[0]]:
                return True

        ### We need two additional edges, two edges connect four different nodes, four nodes are having odd degree, these two edges are not existing.
        if len(odd_node) == 4:
            first_n, second_n, third_n, fourth_n = odd_node[0], odd_node[1], odd_node[2], odd_node[3]
            if second_n not in node_nei[first_n] and fourth_n not in node_nei[third_n]:
                return True
            if third_n not in node_nei[first_n] and fourth_n not in node_nei[second_n]:
                return True
            if fourth_n not in node_nei[first_n] and third_n not in node_nei[second_n]:
                return True

        ### We need two additional edges, connect three nodes, one node has even degree, two nodes have odd degree, these two edges are not existing.
        if len(odd_node) == 2:
            first_n, second_n = odd_node[0], odd_node[1]
            for node, nei in node_nei.items():
                if first_n not in nei and second_n not in nei:
                    return True
        
        return False

77084