mr_dot_convict

10503-The dominoes solitaire-UVa-mr.convict

Jun 6th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include         <bits/stdc++.h>
  2. using namespace std;
  3. #pragma GCC      optimize ("Ofast")
  4. #pragma GCC      optimize ("unroll-loops")
  5. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  6.  
  7. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  8. #define PREC     cout.precision (10); cout << fixed
  9. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  10. #define x        first
  11. #define y        second
  12.  
  13. #define debug(args...) { \
  14.   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  15.   stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  16. }
  17. void err(istream_iterator<string> it) { it->empty();
  18.   cerr << " (Line : " << __LINE__ << ")" << '\n';
  19. }
  20. template<typename T, typename... Args>
  21. void err(istream_iterator<string> it, T a, Args... args) {
  22.   cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  23.   err(++it, args...);
  24. }
  25.  
  26. const int M = 35;
  27. struct Edge {
  28.   int u, v, id;
  29.   vector <Edge*> adj;
  30.   inline bool operator == (const Edge &o) const {
  31.     return u == o.u && v == o.v && id == o.id;
  32.   }
  33. };
  34. Edge edges[M];
  35.  
  36. int vis[M];
  37. Edge start, last;
  38.  
  39. int n, m, idx = 0;
  40. vector <Edge> stck;
  41.  
  42. bool dfs (Edge cur, int level) {
  43.   // cerr << "STACK NOW : -> "; for (Edge e : stck) { cerr << "[ " << e.u << ',' << e.v << " ]->"; } cerr << '\n';
  44.   if (level == n + 1) {
  45.     if (cur == last) {
  46.       return true;
  47.     }
  48.     else return false;
  49.   }
  50.  
  51.   bool ok = false;
  52.   for (Edge *e : cur.adj) {
  53.     if (!vis[e->id]) {
  54.       vis[e->id] = true;
  55.       stck.push_back(*e);
  56.       ok = dfs (*e, level + 1);
  57.       stck.pop_back();
  58.       vis[e->id] = false;
  59.     }
  60.     if (ok) return true;
  61.   }
  62.   return false;
  63. }
  64.  
  65. void init() {
  66.   idx = 0;
  67.   for (int i = 0; i < m + 2; ++i) vis[i] = false;
  68. }
  69.  
  70. void read() {
  71.   cin >> m;
  72.   init();
  73.   cin >> edges[2 * m].u >> edges[2 * m].v;
  74.   edges[2 * m].id = idx++;
  75.   edges[2 * m].adj.clear();
  76.  
  77.   cin >> edges[2 * m + 1].u >> edges[2 * m + 1].v;
  78.   edges[2 * m + 1].id = idx++;
  79.   edges[2 * m + 1].adj.clear();
  80.  
  81.   for (int i = 0; i < m; ++i) {
  82.     cin >> edges[2 *i].u >> edges[2 * i].v;
  83.     edges[2 * i + 1].v = edges[2 * i].u, edges[2 * i + 1].u = edges[2 * i].v;
  84.     edges[2 * i].id = edges[2 * i + 1].id = idx;
  85.     edges[2 * i].adj.clear(), edges[2 * i + 1].adj.clear();
  86.     idx++;
  87.   }
  88.  
  89.   for (int i = 0; i <= 2 * m + 1; ++i) {
  90.     for (int j = 0; j <= 2 * m + 1; ++j) {
  91.       if (i == j || edges[i].id == edges[j].id) continue;
  92.       if (edges[i].v == edges[j].u) {
  93.         edges[i].adj.push_back(&edges[j]);
  94.       }
  95.     }
  96.   }
  97.  
  98.   start = edges[2 * m], last = edges[2 * m + 1];
  99.   vis[start.id] = true;
  100.   stck.push_back(start);
  101.   bool ok = dfs (start, 0);
  102.   stck.pop_back();
  103.   cout << (ok ? "YES\n" : "NO\n");
  104. }
  105.  
  106. signed main() {
  107.   IOS; PREC;
  108.  
  109.   while (cin >> n, n) {
  110.     read();
  111.   }
  112.   return EXIT_SUCCESS;
  113. }
  114.  
  115.  
  116. //2 3  1
  117.  
  118. //3 2  1
Add Comment
Please, Sign In to add comment