<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>

<p>You are given a 2D integer array <code>edges</code> of length <code>n - 1</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> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>

<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>

<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>

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

<ul>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>edges.length == n - 1</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>0 &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><code>edges</code> represents a valid tree.</li>
	<li><code>1 &lt;= restricted.length &lt; n</code></li>
	<li><code>1 &lt;= restricted[i] &lt; n</code></li>
	<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    int find(int x,vector<int>& parent){
    if(x==parent[x]){
        return x;
    }
    return find(parent[x],parent);
}
void union1(int x,int y,vector<int>& parent){
    int px = find(x,parent);
    int py = find(y,parent);
    if(px<py){
        parent[py] = px;
    }
    else if(py<px){
        parent[px] = py;
    }
    else{
        parent[px] = py;
    }
}
int compressor_find(int x,vector<int>& parent){
    if(x==parent[x]){
        return x;
    }
    return parent[x] = compressor_find(parent[x],parent);
}
void show(vector<int> parent,int l=-5,int i=0){
    if(l==-5){
        l= parent.size();
    }
    if(parent[i]!=-55){
        cout<<i<<": "<<parent[i]<<endl;
    }
    if(i+1<=l-1){
        show(parent,l,i+1);
    }
}
void show1(vector<vector<int>> a,int l=-5,int i=0){
    if(l==-5){
        l=a.size();
    }
    cout<<a[i][0]<<" & "<<a[i][1]<<endl;
    if(i+1<=l-1){
        show1(a,l,i+1);
    }
}
int barking_pork(vector<vector<int>> edges,vector<int> restricted,int n){
    int edges_size = edges.size();
    int res_size = restricted.size();
    vector<int> parent(n,0);
    for(int i=0;i<n;i++){
        parent[i]=i;
    }
    for(int i=0;i<res_size;i++){
        parent[restricted[i]] = -55;
    }
    for(int i=0;i<edges_size;i++){
        int x = edges[i][0];int y = edges[i][1];
        if(parent[x]==-55 || parent[y]==-55){
            continue;
        }
        union1(x,y,parent);
    }
    for(int i=0;i<n;i++){
        if(parent[i]!=-55){
            compressor_find(i,parent);
        }
    }
    int count=0;
    for(int i=0;i<n;i++){
        if(parent[i]==0){
            count++;
        }
    }
    return count;
}

    int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {
        
        return barking_pork(edges,restricted,n);
    }
};
136140