Advertisement
pasholnahuy

Untitled

Sep 24th, 2023
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <algorithm>
  2. #include <chrono>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <random>
  6. #include <vector>
  7. #include <unordered_set>
  8.  
  9. using namespace std;
  10. using namespace std::literals::chrono_literals;
  11.  
  12. void check(bool &flag, int needed, vector<int> &taken, unordered_set<int>& permissed, vector<int> &order, int now, vector<vector<int>>& graph) {
  13.     if (needed == 0) {
  14.         flag = true;
  15.     }
  16.     if (flag || now == order.size() || needed > order.size() - taken.size() || needed > taken.size() + permissed.size()) {
  17.         return;
  18.     }
  19.     if (!permissed.contains(now)){
  20.         check(flag, needed, taken, permissed, order, now + 1, graph);
  21.     } else {
  22.         vector<int> last_del;
  23.         for (auto el: graph[now]){
  24.             if (permissed.contains(el) && el > now){
  25.                 last_del.emplace_back(el);
  26.             }
  27.         }
  28.         if (needed > taken.size() + permissed.size() - last_del.size()){
  29.             return;
  30.         }
  31.         for (auto el: last_del){
  32.             permissed.erase(el);
  33.         }
  34.         check(flag, needed - 1, taken, permissed, order, now + 1, graph);
  35.         if (flag){
  36.             return;
  37.         } else {
  38.             for (auto el: last_del){
  39.                 permissed.insert(el);
  40.             }
  41.         }
  42.     }
  43. }
  44.  
  45. int main() {
  46.     int v, k;
  47.     cin >> v >> k;
  48.     vector<vector<int>> graph(v);
  49.     vector<int> deg(v, 0);
  50.     int e;
  51.     cin >> e;
  52.     for (int i = 0; i < e; ++i) {
  53.         int v1, v2;
  54.         cin >> v1 >> v2, --v1, --v2;
  55.         ++deg[v1];
  56.         ++deg[v2];
  57.         graph[v1].emplace_back(v2);
  58.         graph[v2].emplace_back(v1);
  59.     }
  60.     vector<int> order;
  61.     unordered_set<int> permissed;
  62.     for (int i = 0; i < v; ++i) {
  63.         order.emplace_back(i);
  64.         permissed.insert(i);
  65.     }
  66.     sort(order.begin(), order.end(), [&](auto v1, auto v2) { return deg[v1] < deg[v2]; });
  67.     bool flag = false;
  68.     vector<int> taken;
  69.     check(flag, k, taken, permissed, order, 0, graph);
  70.     if (flag){
  71.         cout << "YES";
  72.     } else {
  73.         cout << "NO";
  74.     }
  75.     return 0;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement