Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tuple>
- #include <utility>
- #include <iostream>
- #include <string>
- #include <list>
- #include <vector>
- #include <set>
- #include <stdexcept>
- #include <experimental/functional>
- using namespace std;
- vector<vector<bool>> m;
- bool check(vector<int> &candidates, vector<int> &wrong, int &necessary, bool &is_reached) {
- for (auto i: wrong) {
- bool q = true;
- for (auto j: candidates) {
- if (m[i][j]) {
- q = false;
- break;
- }
- }
- if (q) {
- return false;
- }
- }
- return true;
- }
- bool extend(vector<int> &compsub, vector<int> &candidates, vector<int> &wrong, int &necessary, bool &is_reached) {
- if(is_reached){
- return true;
- }
- while (!candidates.empty() && check(candidates, wrong, necessary, is_reached)) {
- auto v = candidates[0];
- compsub.push_back(v);
- vector<int> new_candidates;
- for (auto i: candidates) {
- if (!m[i][v] && i != v) {
- new_candidates.push_back(i);
- }
- }
- vector<int> new_wrong;
- for (auto i: wrong) {
- if (!m[i][v] && i != v) {
- new_wrong.push_back(i);
- }
- }
- if (new_candidates.empty() && new_wrong.empty()) {
- if (compsub.size() >= necessary) {
- is_reached = true;
- return true;
- }
- } else {
- extend(compsub, new_candidates, new_wrong, necessary, is_reached);
- }
- auto it1 = find(candidates.begin(), candidates.end(), v);
- candidates.erase(it1);
- auto it2 = find(compsub.begin(), compsub.end(), v);
- compsub.erase(it2);
- wrong.push_back(v);
- }
- return is_reached;
- }
- bool bron_kerbosch_max_by_inclusion(vector<vector<bool>> &m, int& necessary, bool &is_reached) {
- vector<int> compsub;
- vector<int> candidates;
- for (int i = 0; i < m.size(); ++i) {
- candidates.push_back(i);
- }
- vector<int> wrong;
- return extend(compsub, candidates, wrong, necessary, is_reached);
- }
- int main() {
- int v, s;
- cin >> v >> s;
- int e;
- cin >> e;
- m.resize(v, vector<bool>(v, false));
- for (int i = 0; i < e; ++i) {
- int first, second;
- cin >> first >> second;
- --first, --second;
- m[first][second] = true;
- m[second][first] = true;
- }
- bool is_reached = false;
- if (bron_kerbosch_max_by_inclusion(m, s, is_reached)) {
- cout << "YES";
- } else {
- cout << "NO";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement