{"id":8859,"date":"2021-11-28T09:44:35","date_gmt":"2021-11-28T17:44:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8859"},"modified":"2021-11-28T09:45:33","modified_gmt":"2021-11-28T17:45:33","slug":"leetcode-2092-find-all-people-with-secret","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2092-find-all-people-with-secret\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2092. Find All People With Secret"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>n<\/code>&nbsp;indicating there are&nbsp;<code>n<\/code>&nbsp;people numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>. You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>meetings<\/code>&nbsp;where&nbsp;<code>meetings[i] = [x<sub>i<\/sub>, y<sub>i<\/sub>, time<sub>i<\/sub>]<\/code>&nbsp;indicates that person&nbsp;<code>x<sub>i<\/sub><\/code>&nbsp;and person&nbsp;<code>y<sub>i<\/sub><\/code>&nbsp;have a meeting at&nbsp;<code>time<sub>i<\/sub><\/code>. A person may attend&nbsp;<strong>multiple meetings<\/strong>&nbsp;at the same time. Finally, you are given an integer&nbsp;<code>firstPerson<\/code>.<\/p>\n\n\n\n<p>Person&nbsp;<code>0<\/code>&nbsp;has a&nbsp;<strong>secret<\/strong>&nbsp;and initially shares the secret with a person&nbsp;<code>firstPerson<\/code>&nbsp;at time&nbsp;<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&nbsp;<code>x<sub>i<\/sub><\/code>&nbsp;has the secret at&nbsp;<code>time<sub>i<\/sub><\/code>, then they will share the secret with person&nbsp;<code>y<sub>i<\/sub><\/code>, and vice versa.<\/p>\n\n\n\n<p>The secrets are shared&nbsp;<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>\n\n\n\n<p>Return&nbsp;<em>a list of all the people that have the secret after all the meetings have taken place.&nbsp;<\/em>You may return the answer in&nbsp;<strong>any order<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1\n<strong>Output:<\/strong> [0,1,2,3,5]\n<strong>Explanation:\n<\/strong>At time 0, person 0 shares the secret with person 1.\nAt time 5, person 1 shares the secret with person 2.\nAt time 8, person 2 shares the secret with person 3.\nAt time 10, person 1 shares the secret with person 5.\u200b\u200b\u200b\u200b\nThus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3\n<strong>Output:<\/strong> [0,1,3]\n<strong>Explanation:<\/strong>\nAt time 0, person 0 shares the secret with person 3.\nAt time 2, neither person 1 nor person 2 know the secret.\nAt time 3, person 3 shares the secret with person 0 and person 1.\nThus, people 0, 1, and 3 know the secret after all the meetings.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1\n<strong>Output:<\/strong> [0,1,2,3,4]\n<strong>Explanation:<\/strong>\nAt time 0, person 0 shares the secret with person 1.\nAt time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.\nNote that person 2 can share the secret at the same time as receiving it.\nAt time 2, person 3 shares the secret with person 4.\nThus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, meetings = [[0,2,1],[1,3,1],[4,5,1]], firstPerson = 1\n<strong>Output:<\/strong> [0,1,2,3]\n<strong>Explanation:<\/strong>\nAt time 0, person 0 shares the secret with person 1.\nAt time 1, person 0 shares the secret with person 2, and person 1 shares the secret with person 3.\nThus, people 0, 1, 2, and 3 know the secret after all the meetings.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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&nbsp;<\/sub>&lt;= n - 1<\/code><\/li><li><code>x<sub>i<\/sub>&nbsp;!= y<sub>i<\/sub><\/code><\/li><li><code>1 &lt;= time<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= firstPerson &lt;= n - 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Union Find<\/strong><\/h2>\n\n\n\n<p>Sorting meetings by time.<\/p>\n\n\n\n<p>At each time stamp, union people who meet.<br>Key step: &#8220;<strong><span class=\"has-inline-color has-vivid-red-color\">un-union<\/span><\/strong>&#8221; people if they <strong><span class=\"has-inline-color has-vivid-red-color\">DO NOT<\/span><\/strong> connected to 0 \/ known the secret after each timestamp.<\/p>\n\n\n\n<p>Time complexity: O(nlogn + m + n)<br>Space complexity: O(m + n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {        \n    map<int, vector<pair<int, int>>> events;\n    for (const auto& m : meetings)\n      events[m[2]].emplace_back(m[0], m[1]);\n    vector<int> p(n);\n    iota(begin(p), end(p), 0);\n    function<int(int)> find = [&](int x) {\n      return p[x] == x ? x : (p[x] = find(p[x]));\n    };\n    p[firstPerson] = 0;\n    for (const auto& [t, s] : events) {\n      for (const auto [u, v] : s)\n        p[find(u)] = find(v);\n      for (const auto [u, v] : s) {\n        if (find(u) != find(0)) p[u] = u;\n        if (find(v) != find(0)) p[v] = v;\n      }\n    }    \n    vector<int> ans;\n    for (int i = 0; i < n; ++i)\n      if (find(i) == find(0)) ans.push_back(i);\n    return ans;\n  }\n};\n\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1202-smallest-string-with-swaps\/\">\u82b1\u82b1\u9171 LeetCode 1202. Smallest String With Swaps<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1489-find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree\/\" data-type=\"post\" data-id=\"6962\">\u82b1\u82b1\u9171 LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2076-process-restricted-friend-requests\/\" data-type=\"post\" data-id=\"8719\">\u82b1\u82b1\u9171 LeetCode 2076. Process Restricted Friend Requests<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1722-minimize-hamming-distance-after-swap-operations\/\" data-type=\"post\" data-id=\"7967\">\u82b1\u82b1\u9171 LeetCode 1722. Minimize Hamming Distance After Swap Operations<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/data-structure\/sp1-union-find-set\/\" data-type=\"post\" data-id=\"1039\">\u82b1\u82b1\u9171 LeetCode Disjoint set \/ Union Find Forest SP1<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer&nbsp;n&nbsp;indicating there are&nbsp;n&nbsp;people numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are also given a&nbsp;0-indexed&nbsp;2D integer array&nbsp;meetings&nbsp;where&nbsp;meetings[i] = [xi, yi, timei]&nbsp;indicates that person&nbsp;xi&nbsp;and person&nbsp;yi&nbsp;have&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[77,217,113],"class_list":["post-8859","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-hard","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8859"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8859\/revisions"}],"predecessor-version":[{"id":8861,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8859\/revisions\/8861"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}