Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. struct vertex
  9. {
  10.     vector<int> neighbours;
  11.     vector<int> results;
  12. };
  13.  
  14. void input(vector<vertex>& g)
  15. {
  16.     int n, m;
  17.     cin >> n >> m;
  18.  
  19.     g.assign(n + 1, vertex());
  20.  
  21.     for (int i = 0; i < m; i++)
  22.     {
  23.         int a, b;
  24.         cin >> a >> b;
  25.  
  26.         g[a].neighbours.push_back(b);
  27.         g[b].neighbours.push_back(a);
  28.     }
  29. }
  30.  
  31. bool solve(vector<vertex>& g, stack<int>& st, unordered_map<int, unordered_map<int, bool>>& r, int s, int v)
  32. {
  33.     for (int i = 0; i < g[v].neighbours.size(); i++)
  34.     {
  35.         if (st.size() == g.size() && g[v].neighbours[i] == 0)
  36.             return true;
  37.  
  38.         int x = s | (1 << g[v].neighbours[i]);
  39.  
  40.         if (s != x && r[g[v].neighbours[i]].find(x) == r[g[v].neighbours[i]].end())
  41.         {
  42.             st.push(g[v].neighbours[i]);
  43.  
  44.             if (solve(g, st, r, x, g[v].neighbours[i]))
  45.                 return true;
  46.             else
  47.             {
  48.                 st.pop();
  49.                 r[g[v].neighbours[i]][x] = false;
  50.             }
  51.         }
  52.     }
  53.  
  54.     return false;
  55. }
  56.  
  57. void output(stack<int>& s)
  58. {
  59.     while (!s.empty())
  60.     {
  61.         if (s.top() != 0)
  62.             cout << s.top() << " ";
  63.  
  64.         s.pop();
  65.     }
  66.  
  67.     cout << endl;
  68. }
  69.  
  70. int main()
  71. {
  72.     int z;
  73.     cin >> z;
  74.  
  75.     while (z--)
  76.     {
  77.         vector<vertex> graph;
  78.         unordered_map<int, unordered_map<int, bool>> visited;
  79.         stack<int> stackOut;
  80.  
  81.         input(graph);
  82.  
  83.         bool res = solve(graph, stackOut, visited, 1, 0);
  84.  
  85.         if (res)
  86.         {
  87.             cout << "TAK" << endl;
  88.             output(stackOut);
  89.         }
  90.         else
  91.             cout << "NIE" << endl;
  92. }
  93.    
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement