mr_dot_convict

193-Graph-coloring-UVa-mr.convict

Jun 10th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32.  
  33. const int N = 104;
  34. vector <int> stck, sol;
  35. int edge[N][N];
  36. int n, m;
  37. int vis[N];
  38.  
  39. void backtrack (int u) { //find max independent set
  40.    // cerr << "STACK :"; for (int v : stck) { cerr << v + 1 << " ";} cerr << '\n';
  41.    if (stck.size() > sol.size()) {
  42.       sol = stck;
  43.    }
  44.  
  45.    for (int v = u; v < n; ++v) {
  46.       bool ok = true;
  47.       for (int s : stck) {
  48.          if (edge[s][v]) {
  49.             ok = false;
  50.             break;
  51.          }
  52.       }
  53.       if (ok) {
  54.          stck.push_back(v);
  55.          backtrack (v + 1);
  56.          stck.pop_back();
  57.       }
  58.    }
  59. }
  60.  
  61. void init() {
  62.    for (int i = 0; i < n; ++i)
  63.       for (int j = 0; j < n; ++j) edge[i][j] = 0;
  64. }
  65.  
  66. void solve() {
  67.    sol.clear(), stck.clear();
  68.    backtrack(0);
  69.  
  70.    cout << sol.size() << '\n';
  71.    for (int i = 0; i < (int)sol.size(); ++i) {
  72.       if (i) cout << ' ';
  73.       cout << sol[i] + 1;
  74.    } cout << '\n';
  75. }
  76.  
  77. void read() {
  78.    cin >> n >> m;
  79.    init();
  80.    for (int i = 0, u, v; i < m; ++i) {
  81.       cin >> u >> v;
  82.       --u, --v;
  83.       edge[u][v] = edge[v][u] = 1;
  84.    }
  85. }
  86.  
  87. signed main() {
  88.    IOS; PREC;
  89.    int tc; cin >> tc;
  90.    while (tc--) {
  91.       read();
  92.       solve();
  93.    }
  94.  
  95.    return EXIT_SUCCESS;
  96. }
Add Comment
Please, Sign In to add comment