cpp: solve «2092. Find All People With Secret»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
2cd90d0db7
commit
5a9d218bcb
1 changed files with 62 additions and 25 deletions
|
@ -1,51 +1,88 @@
|
|||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
void handle_previous_meeting(std::set<int> &overall,
|
||||
std::set<int> ¤t) {
|
||||
std::vector<int> intersection;
|
||||
std::set_intersection(current.begin(), current.end(), overall.begin(),
|
||||
overall.end(), std::back_inserter(intersection));
|
||||
|
||||
if (!intersection.empty()) {
|
||||
overall.insert(current.begin(), current.end());
|
||||
void process_timeslot(std::vector<bool> &knows,
|
||||
const std::vector<std::pair<int, int>> &meetings) {
|
||||
// Reconstruct the components for the same timeslot
|
||||
// and also construct the source of secret
|
||||
std::unordered_set<int> sources;
|
||||
std::unordered_map<int, std::vector<int>> neighbors;
|
||||
for (const auto &[a, b] : meetings) {
|
||||
if (knows[a]) {
|
||||
sources.insert(a);
|
||||
}
|
||||
if (knows[b]) {
|
||||
sources.insert(b);
|
||||
}
|
||||
|
||||
current = {};
|
||||
neighbors[a].push_back(b);
|
||||
neighbors[b].push_back(a);
|
||||
}
|
||||
|
||||
// Constructing queue from iterators is allowed since C++23…
|
||||
std::queue<int> q;
|
||||
for (const auto &p : sources) {
|
||||
q.push(p);
|
||||
}
|
||||
|
||||
// Classic BFS
|
||||
while (!q.empty()) {
|
||||
int u = q.front();
|
||||
q.pop();
|
||||
|
||||
for (const auto &neighbor : neighbors[u]) {
|
||||
if (knows[neighbor]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
knows[neighbor] = true;
|
||||
q.push(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<int> findAllPeople(int n,
|
||||
std::vector<std::vector<int>> meetings,
|
||||
int firstPerson) {
|
||||
// Sort the meetings by time
|
||||
std::sort(meetings.begin(), meetings.end(),
|
||||
[](const auto &l, const auto &r) { return l[2] < r[2]; });
|
||||
|
||||
std::set<int> knowers{0, firstPerson};
|
||||
|
||||
std::set<int> current_meeting{};
|
||||
int timestamp = -1;
|
||||
// Group meetings first
|
||||
std::map<int, std::vector<std::pair<int, int>>> groups;
|
||||
for (const auto &meeting : meetings) {
|
||||
auto a = meeting[0];
|
||||
auto b = meeting[1];
|
||||
auto t = meeting[2];
|
||||
|
||||
if (t != timestamp) {
|
||||
// handle the previous meeting
|
||||
handle_previous_meeting(knowers, current_meeting);
|
||||
timestamp = t;
|
||||
groups[t].emplace_back(a, b);
|
||||
}
|
||||
|
||||
// just add the members and carry on
|
||||
current_meeting.insert(a);
|
||||
current_meeting.insert(b);
|
||||
}
|
||||
handle_previous_meeting(knowers, current_meeting);
|
||||
// Keep “knowers” in the bitset
|
||||
std::vector<bool> knows(n, false);
|
||||
knows[0] = true;
|
||||
knows[firstPerson] = true;
|
||||
|
||||
return std::vector<int>(knowers.begin(), knowers.end());
|
||||
// Go through the groups
|
||||
for (const auto &[time, meetings] : groups) {
|
||||
process_timeslot(knows, meetings);
|
||||
}
|
||||
|
||||
// Reconstruct the list of people that know
|
||||
std::vector<int> all_people;
|
||||
for (auto i = 0; i < n; ++i) {
|
||||
if (knows[i]) {
|
||||
all_people.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
return all_people;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue