Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<set>
- using std::cin;
- using std::cout;
- using std::vector;
- struct Staff {
- int time;
- vector<int> friends;
- void Print() const {
- cout << " Time: " << time << " ";
- cout << "Friends: ";
- for (auto i : friends) cout << i << " ";
- cout << "\n";
- }
- bool operator < (Staff a) const {
- return this->time < a.time;
- }
- bool operator >(Staff a) const {
- return this->time > a.time;
- }
- };
- struct HeapStaff {
- int name;
- int time;
- HeapStaff(int n, int t): name(n), time(t) {}
- HeapStaff() : name(0), time(0) {}
- void Print() {
- cout << "name: " << name << " time: " << time;
- }
- };
- class Heap {
- private:
- vector<HeapStaff> values;
- int GetLeftChild(int index) const {
- return (2 * index + 1) <= values.size() - 1 ? (2 * index + 1) : -1;
- }
- int GetRightChild(int index) const {
- return (2 * index + 2) <= values.size() - 1 ? (2 * index + 2) : -1;
- }
- int GetParent(int index) const {
- return index != 0 ? (index + 1) / 2 - 1 : -1;
- }
- void swap(int index1, int index2) {
- HeapStaff a = values[index1];
- values[index1] = values[index2];
- values[index2] = a;
- }
- void Heapify(int index) {
- if (index >= values.size()) return;
- int min_index = index;
- if (GetLeftChild(index) != -1)
- if (values[min_index].time > values[GetLeftChild(index)].time) {
- min_index = GetLeftChild(index);
- }
- if (GetRightChild(index) != -1) {
- if (values[min_index].time > values[GetRightChild(index)].time) {
- min_index = GetRightChild(index);
- }
- }
- if (min_index == index) return;
- else {
- swap(index, min_index);
- Heapify(min_index);
- }
- }
- void HeapUp(int index) {
- if (index == 0) return;
- int parent = GetParent(index);
- while (index > 0 && values[index].time < values[parent].time) {
- swap(index, parent);
- index = parent;
- parent = GetParent(index);
- }
- return;
- }
- public:
- void BuildHeap(const vector<HeapStaff>& val) {
- this->values.resize(val.size());
- std::copy(val.begin(), val.end(), this->values.begin());
- for (int i = (val.size() / 2); i >= 0; --i) {
- Heapify(i);
- }
- }
- HeapStaff GetMin() const {
- return values[0];
- }
- void ExtractMin() {
- swap(0, values.size() - 1);
- values.erase(--values.end());
- Heapify(0);
- }
- void Add(HeapStaff elem) {
- values.push_back(elem);
- HeapUp(values.size() - 1);
- }
- bool Empty() {
- return values.size() == 0;
- }
- void PrintHeap() {
- for (int i = 0; i < values.size(); ++i) {
- cout << "#elem = " << i << "\n";
- values[i].Print();
- }
- }
- };
- vector<Staff> Input() {
- int num_staff;
- cin >> num_staff;
- vector<Staff> personal(num_staff);
- for (int i = 0; i < num_staff; ++i) {
- cin >> personal[i].time;
- int num_friends;
- cin >> num_friends;
- personal[i].friends.resize(num_friends);
- for (int j = 0; j < num_friends; ++j) {
- cin >> personal[i].friends[j];
- personal[i].friends[j]--;
- }
- }
- return personal;
- }
- int MinTime(const vector<Staff>& personal) {
- Heap current_staff;
- vector<bool> staff_in_heap(personal.size(), false);
- for (int i = 0; i < personal.size() && personal[i].time == 0; ++i) {
- HeapStaff staff(i, personal[i].time);
- current_staff.Add(staff);
- staff_in_heap[i] = true;
- }
- int min_time = 0;
- HeapStaff staff(-1, -1);
- int num_staff_know = 0;
- while (!current_staff.Empty() && num_staff_know < (personal.size() + 1) / 2) {
- staff = current_staff.GetMin();
- current_staff.ExtractMin();
- ++num_staff_know;
- if (min_time < staff.time) min_time = staff.time;
- for (int i = 0; i < personal[staff.name].friends.size(); ++i) {
- if (!staff_in_heap[personal[staff.name].friends[i]]) {
- HeapStaff s(personal[staff.name].friends[i], personal[personal[staff.name].friends[i]].time);
- current_staff.Add(s);
- staff_in_heap[personal[staff.name].friends[i]] = true;
- }
- }
- }
- if (num_staff_know >= (personal.size() + 1) / 2) return min_time;
- else return -1;
- }
- int main() {
- vector<Staff> personal = Input();
- cout << MinTime(personal);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement