Advertisement
Guest User

Untitled

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