<p>You are given the <code>head</code> of a linked list containing unique integer values and an integer array <code>nums</code> that is a subset of the linked list values.</p>

<p>Return <em>the number of connected components in </em><code>nums</code><em> where two values are connected if they appear <strong>consecutively</strong> in the linked list</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom1.jpg" style="width: 424px; height: 65px;" />
<pre>
<strong>Input:</strong> head = [0,1,2,3], nums = [0,1,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 0 and 1 are connected, so [0, 1] and [3] are the two connected components.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom2.jpg" style="width: 544px; height: 65px;" />
<pre>
<strong>Input:</strong> head = [0,1,2,3,4], nums = [0,3,1,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
</pre>

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

<ul>
	<li>The number of nodes in the linked list is <code>n</code>.</li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= Node.val &lt; n</code></li>
	<li>All the values <code>Node.val</code> are <strong>unique</strong>.</li>
	<li><code>1 &lt;= nums.length &lt;= n</code></li>
	<li><code>0 &lt;= nums[i] &lt; n</code></li>
	<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
</ul>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void make_set(vector<int>& p, int n) {
        for (int i = 0; i < n; ++i) p[i] = i;
    }
    int find(vector<int>& p, int x) {
        return (x == p[x] ? x : p[x] = find(p, p[x]));
    }
    void _union(vector<int>& p, vector<int>& rank, int x, int y) {
        if ((x = find(p, x)) == (y = find(p, y))) return;
        if (rank[x] < rank[y]) p[x] = y;
        else {
            p[y] = x;
            if (rank[x] == rank[y]) ++rank[x];
        }
    }

    int numComponents(ListNode* head, vector<int>& nums) {
        int n = 0;
        ListNode* node = head;
        
        while (node) {
            ++n;
            node = node->next;
        }

        vector<int> p(n), rank(n);
        vector<bool> in_nums(n);

        for (auto const& num: nums) in_nums[num] = true;
        make_set(p, n);
        
        node = head;
        while (node->next) {
            if (in_nums[node->val] && in_nums[node->next->val]) _union(p, rank, node->val, node->next->val);
            node = node->next;
        }
        
        unordered_map<int, bool> checked;
        int answer = 0;

        for (int i = 0; i < n; ++i) {
            if (!in_nums[i]) continue;
            if (!checked[find(p, i)]) ++answer;
            checked[find(p, i)] = true;
        }

        return answer;
    }
};
24900