<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates that person <code>x<sub>i</sub></code> and person <code>y<sub>i</sub></code> have a meeting at <code>time<sub>i</sub></code>. A person may attend <strong>multiple meetings</strong> at the same time. Finally, you are given an integer <code>firstPerson</code>.</p>

<p>Person <code>0</code> has a <strong>secret</strong> and initially shares the secret with a person <code>firstPerson</code> at time <code>0</code>. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person <code>x<sub>i</sub></code> has the secret at <code>time<sub>i</sub></code>, then they will share the secret with person <code>y<sub>i</sub></code>, and vice versa.</p>

<p>The secrets are shared <strong>instantaneously</strong>. That is, a person may receive the secret and share it with people in other meetings within the same time frame.</p>

<p>Return <em>a list of all the people that have the secret after all the meetings have taken place. </em>You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
<strong>Output:</strong> [0,1,2,3,5]
<strong>Explanation:
</strong>At time 0, person 0 shares the secret with person 1.
At time 5, person 1 shares the secret with person 2.
At time 8, person 2 shares the secret with person 3.
At time 10, person 1 shares the secret with person 5.​​​​
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
<strong>Output:</strong> [0,1,3]
<strong>Explanation:</strong>
At time 0, person 0 shares the secret with person 3.
At time 2, neither person 1 nor person 2 know the secret.
At time 3, person 3 shares the secret with person 0 and person 1.
Thus, people 0, 1, and 3 know the secret after all the meetings.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
<strong>Output:</strong> [0,1,2,3,4]
<strong>Explanation:</strong>
At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
Note that person 2 can share the secret at the same time as receiving it.
At time 2, person 3 shares the secret with person 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
</pre>

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

<ul>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li>
	<li><code>meetings[i].length == 3</code></li>
	<li><code>0 &lt;= x<sub>i</sub>, y<sub>i </sub>&lt;= n - 1</code></li>
	<li><code>x<sub>i</sub> != y<sub>i</sub></code></li>
	<li><code>1 &lt;= time<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= firstPerson &lt;= n - 1</code></li>
</ul>

class Solution:
    def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
        # solution redone here, original solution further below
        m = len(meetings)
        
        def find(x, parents):
            if x == parents[x]:
                return x
            anc_x = find(parents[x], parents)
            parents[x] = anc_x
            return anc_x

        def union(x, y, parents, ranks):
            anc_x, anc_y = find(x, parents), find(y, parents)
            if anc_x == anc_y:
                return
            if ranks[anc_x] > ranks[anc_y]:
                parents[anc_y] = anc_x
            elif ranks[anc_x] < ranks[anc_y]:
                parents[anc_x] = anc_y
            else:
                parents[anc_y] = anc_x
                ranks[anc_x] += 1
        
        time_d = collections.defaultdict(list)
        for x, y, t in meetings:
            time_d[t].append((x, y))
        sorted_times = sorted(time_d.keys())
        
        parents_global = [i for i in range(n)]
        ranks_global = [0] * n
        union(0, firstPerson, parents_global, ranks_global)

        for t in sorted_times:
            parents_local = {}
            ranks_local = collections.defaultdict(int)
            has_secret = False
            for x, y in time_d[t]:
                if x not in parents_local:
                    parents_local[x] = x
                if y not in parents_local:
                    parents_local[y] = y
                union(x, y, parents_local, ranks_local)
                if find(x, parents_global) == find(0, parents_global) or find(y, parents_global) == find(0, parents_global):
                    has_secret = True

            if not has_secret:
                continue
            
            get_secret_local_ancs = set()
            for x in parents_local:
                if find(x, parents_global) == find(0, parents_global):
                    get_secret_local_ancs.add(find(x, parents_local))
            for x in parents_local:
                if find(x, parents_local) in get_secret_local_ancs:
                    union(x, 0, parents_global, ranks_global)
        
        res = []
        for x in range(n):
            if find(x, parents_global) == find(0, parents_global):
                res.append(x)
        return res


































        # def find(x, parents):
        #     if x == parents[x]:
        #         return x
        #     res = find(parents[x], parents)
        #     parents[x] = res
        #     return res
        
        # def union(x, y, parents, ranks):
        #     anc_x = find(x, parents)
        #     anc_y = find(y, parents)
        #     if anc_x == anc_y:
        #         return
        #     if ranks[x] < ranks[y]:
        #         parents[anc_x] = anc_y
        #     elif ranks[x] > ranks[y]:
        #         parents[anc_y] = anc_x
        #     else:
        #         parents[anc_y] = anc_x
        #         ranks[anc_x] += 1
        
        # time_d = collections.defaultdict(list)
        # for x, y, t in meetings:
        #     time_d[t].append((x, y))
        
        # res = set([0, firstPerson])
        # for t in sorted(time_d.keys()):
        #     arr = time_d[t]
        #     has_secret = False
        #     for x, y in arr:
        #         if x in res or y in res:
        #             has_secret = True
        #             break
        #     if not has_secret:
        #         continue
        #     # there is someone who knows the secret
        #     # we just need to identify the connected component of people in time_d[t]
        #     # who will get the secret
        #     local_parents, local_ranks = {}, {}
        #     for x, y in arr:
        #         local_parents[x] = x
        #         local_parents[y] = y
        #         local_ranks[x] = 1
        #         local_ranks[y] = 1
        #     for x, y in arr:
        #         union(x, y, local_parents, local_ranks)
            
        #     local_ancs_to_children = collections.defaultdict(list)
        #     for x, y in arr:
        #         local_ancs_to_children[find(x, local_parents)].append(x)
        #         local_ancs_to_children[find(y, local_parents)].append(y)
            
        #     for local_anc in local_ancs_to_children:
        #         has_secret = False
        #         for x in local_ancs_to_children[local_anc]:
        #             if x in res:
        #                 has_secret = True
        #                 break
        #         if has_secret:
        #             for x in local_ancs_to_children[local_anc]:
        #                 res.add(x)
            
        # return list(res)
60063