-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode2092.cpp
80 lines (74 loc) · 2.66 KB
/
leetcode2092.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 思路:并查集
// 遍历每个时间,对在这个时间开会的人中进行一次并查集合并,模拟信息交流,并查集合并完将没有听到秘密的人重置,再遍历下个时间
class Solution {
public:
struct cmp
{
bool operator()(vector<int>& a, vector<int>& b){
return a[2] < b[2];
}
};
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
meetings.push_back({0, firstPerson, 0});
vector<int> father(n);
iota(father.begin(), father.end(), 0);
sort(meetings.begin(), meetings.end(), cmp());
for(int i = 0; i < meetings.size(); )
{
for(int j = i + 1; j <= meetings.size(); j++)
{
if(j == meetings.size() || meetings[i][2] != meetings[j][2])
{
make_union(meetings, i, j, father, firstPerson);
i = j;
break;
}
}
}
vector<int> result;
int first_father = find_union(father, firstPerson);
for(int i = 0; i < n; i++)
if(find_union(father, i) == first_father)
result.push_back(i);
return result;
}
void make_union(const vector<vector<int>>& meetings, int begin, int end, vector<int>& father, int first_person)
{
// unordered_set<int> candidate;
for(int i = begin; i < end; i++)
{
int a = meetings[i][0];
int b = meetings[i][1];
// candidate.insert(a);
// candidate.insert(b);
if(a > b)
swap(a, b);
combine_union(father, a, b);
}
init_union(meetings, father, begin, end, first_person);
}
int find_union(vector<int>& father, int x)
{
if(father[x] != x)
father[x] = find_union(father, father[x]);
return father[x];
}
void combine_union(vector<int>& father, int i, int j)
{
int i_father = find_union(father, i);
int j_father = find_union(father, j);
if(i_father != j_father)
father[i_father] = j_father;
}
void init_union(const vector<vector<int>>& meetings, vector<int>& father, int begin, int end, int first_person)
{
int first_person_father = find_union(father, first_person);
for(int i = begin; i < end; i++)
{
if(find_union(father, meetings[i][0]) != first_person_father)
father[meetings[i][0]] = meetings[i][0];
if(find_union(father, meetings[i][1]) != first_person_father)
father[meetings[i][1]] = meetings[i][1];
}
}
};