Advertisement
Art_Uspen

Untitled

Oct 10th, 2021
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <tuple>
  2. #include <utility>
  3. #include <iostream>
  4. #include <string>
  5. #include <list>
  6. #include <vector>
  7. #include <set>
  8. #include <stdexcept>
  9. #include <experimental/functional>
  10.  
  11. using namespace std;
  12.  
  13. vector<vector<bool>> m;
  14.  
  15. bool check(vector<int> &candidates, vector<int> &wrong, int &necessary, bool &is_reached) {
  16.     for (auto i: wrong) {
  17.         bool q = true;
  18.         for (auto j: candidates) {
  19.             if (m[i][j]) {
  20.                 q = false;
  21.                 break;
  22.             }
  23.         }
  24.         if (q) {
  25.             return false;
  26.         }
  27.     }
  28.     return true;
  29. }
  30.  
  31. bool extend(vector<int> &compsub, vector<int> &candidates, vector<int> &wrong, int &necessary, bool &is_reached) {
  32.     if(is_reached){
  33.         return true;
  34.     }
  35.     while (!candidates.empty() && check(candidates, wrong, necessary, is_reached)) {
  36.         auto v = candidates[0];
  37.         compsub.push_back(v);
  38.         vector<int> new_candidates;
  39.         for (auto i: candidates) {
  40.             if (!m[i][v] && i != v) {
  41.                 new_candidates.push_back(i);
  42.             }
  43.         }
  44.         vector<int> new_wrong;
  45.         for (auto i: wrong) {
  46.             if (!m[i][v] && i != v) {
  47.                 new_wrong.push_back(i);
  48.             }
  49.         }
  50.         if (new_candidates.empty() && new_wrong.empty()) {
  51.             if (compsub.size() >= necessary) {
  52.                 is_reached = true;
  53.                 return true;
  54.             }
  55.         } else {
  56.             extend(compsub, new_candidates, new_wrong, necessary, is_reached);
  57.         }
  58.         auto it1 = find(candidates.begin(), candidates.end(), v);
  59.         candidates.erase(it1);
  60.         auto it2 = find(compsub.begin(), compsub.end(), v);
  61.         compsub.erase(it2);
  62.         wrong.push_back(v);
  63.     }
  64.     return is_reached;
  65. }
  66.  
  67. bool bron_kerbosch_max_by_inclusion(vector<vector<bool>> &m, int& necessary, bool &is_reached) {
  68.     vector<int> compsub;
  69.     vector<int> candidates;
  70.     for (int i = 0; i < m.size(); ++i) {
  71.         candidates.push_back(i);
  72.     }
  73.     vector<int> wrong;
  74.  
  75.     return extend(compsub, candidates, wrong, necessary, is_reached);
  76. }
  77.  
  78. int main() {
  79.     int v, s;
  80.     cin >> v >> s;
  81.     int e;
  82.     cin >> e;
  83.     m.resize(v, vector<bool>(v, false));
  84.     for (int i = 0; i < e; ++i) {
  85.         int first, second;
  86.         cin >> first >> second;
  87.         --first, --second;
  88.         m[first][second] = true;
  89.         m[second][first] = true;
  90.     }
  91.     bool is_reached = false;
  92.     if (bron_kerbosch_max_by_inclusion(m, s, is_reached)) {
  93.         cout << "YES";
  94.     } else {
  95.         cout << "NO";
  96.     }
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement