deushiro

цэшка

Feb 24th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <stdlib.h>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <iterator>
  8. #include <vector>
  9. #include <set>
  10. #include <string>
  11. #include <fstream>
  12.  
  13. using namespace std;
  14.  
  15. ifstream in("input.txt");
  16. ofstream out("output.txt");
  17.  
  18. struct node {
  19.     string person;
  20.     set<node*> friends;
  21.     node(string p_) : person(p_) {};
  22.     node(string p_, set<node*> f_) : person(p_), friends(f_) {};
  23.     void print() {
  24.         cout << person << " (" << this << ") knows: { ";
  25.         for (auto f : friends)
  26.             cout << f->person << " ";
  27.         cout << "}\n";
  28.     }
  29. };
  30.  
  31. class graph {
  32. private:
  33.     set<node*> g;
  34. public:
  35.     void addNode(string p_) {
  36.         node* n = new node(p_);
  37.         g.insert(n);
  38.     }
  39.     void addEdge(string p1_, string p2_) {
  40.         node* n1 = NULL, * n2 = NULL;
  41.         for (auto p : g) {
  42.             if (p->person == p1_) { n1 = p; }
  43.             else if (p->person == p2_) { n2 = p; }
  44.         }
  45.         if (n1 && n2)
  46.             n1->friends.insert(n2), n2->friends.insert(n1);
  47.     }
  48.     set<node*> getUniverse() {
  49.         return g;
  50.     }
  51.     void print() {
  52.         for (auto p : g)
  53.             (*p).print();
  54.     }
  55. };
  56.  
  57.  
  58. vector<int> sm;
  59. vector<string> ans;
  60. int maxi = 0;
  61.  
  62.  
  63. void set_print(set<node*> s) {
  64.     int sum = 0;
  65.     for (auto p : s) {
  66.         sum += sm[stoi(p->person) - 1];
  67.     }
  68.     if (sum > maxi) {
  69.         ans.clear();
  70.         for (auto p : s)
  71.             ans.push_back(p->person);
  72.         out << ans.size() << endl;
  73.         for (auto t : ans) {
  74.             out << t << " ";
  75.         }
  76.         out << endl;
  77.         maxi = sum;
  78.     }
  79. }
  80.  
  81.  
  82. set<node*> set_union(set<node*> a, set<node*> b) {
  83.     set<node*> c;
  84.     set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.end()));
  85.     return c;
  86. }
  87.  
  88. set<node*> set_intersection(set<node*> a, set<node*> b) {
  89.     set<node*> c;
  90.     set_intersection(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.end()));
  91.     return c;
  92. }
  93.  
  94. //note, set_difference is the relative compliment on input set a//
  95. set<node*> set_difference(set<node*> a, set<node*> b) {
  96.     set<node*> c;
  97.     set_difference(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.end()));
  98.     return c;
  99. }
  100.  
  101. void bronKerbosch(set<node*> R, set<node*> P, set<node*> X) {
  102.     if (P.empty() && X.empty()) {
  103.         set_print(R);
  104.     }
  105.     set<node*>::iterator v = P.begin();
  106.     while (!P.empty() && v != P.end()) {
  107.         set<node*> singleton = { (*v) };
  108.         bronKerbosch(set_union(R, singleton), set_intersection(P, (*v)->friends), set_intersection(X, (*v)->friends));
  109.         P = set_difference(P, singleton);
  110.         X = set_union(X, singleton);
  111.         if (!P.empty())
  112.             v = P.begin();
  113.     }
  114. }
  115.  
  116. int main() {
  117.  
  118.     int tt;
  119.     in >> tt;
  120.     while (tt--) {
  121.         graph g;
  122.         int n, m;
  123.         in >> n >> m;
  124.         sm.clear();
  125.         sm.resize(m);
  126.         for (int i = 0; i < m; ++i) {
  127.             g.addNode(to_string(i + 1));
  128.         }
  129.         vector<vector<int>> z(m);
  130.         for (int i = 0; i < m; ++i) {
  131.             int k;
  132.             in >> k;
  133.             sm[i] = k;
  134.             for (int j = 0; j < k; ++j) {
  135.                 int x;
  136.                 in >> x;
  137.                 z[i].push_back(x);
  138.             }
  139.         }
  140.         for (int i = 0; i < m; ++i) {
  141.             for (int j = 0; j < m; ++j) {
  142.                 bool flag = true;
  143.                 if (i != j) {
  144.                     vector<int> c(n + 1, 0);
  145.                     for (int k : z[i]) {
  146.                         c[k]++;
  147.                     }
  148.                     for (int k : z[j]) {
  149.                         c[k]++;
  150.                     }
  151.                     for (int k = 0; k < n + 1; ++k) {
  152.                         if (c[k] > 1)
  153.                             flag = false;
  154.                     }
  155.                     if (flag) {
  156.                         g.addEdge(to_string(i + 1), to_string(j + 1));
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.         set<node*> R, P, X;
  162.         P = g.getUniverse();
  163.         maxi = 0;
  164.         bronKerbosch(R, P, X);
  165.         out << ans.size() << endl;
  166.         for (auto t : ans) {
  167.             out << t << " ";
  168.         }
  169.         out << endl;
  170.  
  171.     }
  172.  
  173.     return 0;
  174. }
Add Comment
Please, Sign In to add comment