mr_dot_convict

10503-The-dominoes-solitaire-UVa-mr.convict

Jun 6th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 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 M = 35;
  34. struct Edge {
  35.   int u, v, id;
  36.   vector <Edge*> adj;
  37.   inline bool operator == (const Edge &o) const {
  38.     return u == o.u && v == o.v && id == o.id;
  39.   }
  40. };
  41. Edge edges[M];
  42.  
  43. int vis[M];
  44. Edge start, last;
  45.  
  46. int n, m, idx = 0;
  47. vector <Edge> stck;
  48.  
  49. bool dfs (Edge cur, int level) {
  50.   // cerr << "STACK NOW : -> "; for (Edge e : stck) { cerr << "[ " << e.u << ',' << e.v << " ]->"; } cerr << '\n';
  51.   if (level == n + 1) {
  52.     if (cur == last) {
  53.       return true;
  54.     }
  55.     else return false;
  56.   }
  57.  
  58.   bool ok = false;
  59.   for (Edge *e : cur.adj) {
  60.     if (!vis[e->id]) {
  61.       vis[e->id] = true;
  62.       // stck.push_back(*e);
  63.       ok = dfs (*e, level + 1);
  64.       // stck.pop_back();
  65.       vis[e->id] = false;
  66.     }
  67.     if (ok) return true;
  68.   }
  69.   return false;
  70. }
  71.  
  72. void init() {
  73.   idx = 0;
  74.   for (int i = 0; i < m + 2; ++i) vis[i] = false;
  75. }
  76.  
  77. void read() {
  78.   cin >> m;
  79.   init();
  80.   cin >> edges[2 * m].u >> edges[2 * m].v;
  81.   edges[2 * m].id = idx++;
  82.   edges[2 * m].adj.clear();
  83.  
  84.   cin >> edges[2 * m + 1].u >> edges[2 * m + 1].v;
  85.   edges[2 * m + 1].id = idx++;
  86.   edges[2 * m + 1].adj.clear();
  87.  
  88.   for (int i = 0; i < m; ++i) {
  89.     cin >> edges[2 *i].u >> edges[2 * i].v;
  90.     edges[2 * i + 1].v = edges[2 * i].u, edges[2 * i + 1].u = edges[2 * i].v;
  91.     edges[2 * i].id = edges[2 * i + 1].id = idx;
  92.     edges[2 * i].adj.clear(), edges[2 * i + 1].adj.clear();
  93.     idx++;
  94.   }
  95.  
  96.   for (int i = 0; i <= 2 * m + 1; ++i) {
  97.     for (int j = 0; j <= 2 * m + 1; ++j) {
  98.       if (i == j || edges[i].id == edges[j].id) continue;
  99.       if (edges[i].v == edges[j].u) {
  100.         edges[i].adj.push_back(&edges[j]);
  101.       }
  102.     }
  103.   }
  104.  
  105.   start = edges[2 * m], last = edges[2 * m + 1];
  106.   vis[start.id] = true;
  107.   // stck.push_back(start);
  108.   bool ok = dfs (start, 0);
  109.   // stck.pop_back();
  110.   cout << (ok ? "YES\n" : "NO\n");
  111. }
  112.  
  113. signed main() {
  114.   IOS; PREC;
  115.  
  116.   while (cin >> n, n) {
  117.     read();
  118.   }
  119.   return EXIT_SUCCESS;
  120. }
Add Comment
Please, Sign In to add comment