-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode2092.cpp
More file actions
57 lines (47 loc) · 1.35 KB
/
leetcode2092.cpp
File metadata and controls
57 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
vector<int> parent;
int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a != b) parent[b] = a;
}
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
sort(meetings.begin(), meetings.end(),
[](auto &a, auto &b) { return a[2] < b[2]; });
parent.resize(n);
for (int i = 0; i < n; i++) parent[i] = i;
unite(0, firstPerson);
int m = meetings.size();
int i = 0;
while (i < m) {
int time = meetings[i][2];
vector<int> people;
int j = i;
while (j < m && meetings[j][2] == time) {
int x = meetings[j][0];
int y = meetings[j][1];
unite(x, y);
people.push_back(x);
people.push_back(y);
j++;
}
for (int p : people) {
if (find(p) != find(0)) {
parent[p] = p;
}
}
i = j;
}
vector<int> result;
for (int i = 0; i < n; i++) {
if (find(i) == find(0))
result.push_back(i);
}
return result;
}
};