Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <array>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. static int n;
  9. static vector<vector<bool> > binds;
  10. static array<int, 5> complex;
  11. static vector<array<int, 5> > results;
  12.  
  13. void load()
  14. {
  15.     cin >> n;
  16.     for(int i = 0; i < n; i++)
  17.     {
  18.         binds.push_back(vector<bool>(n));
  19.     }
  20.  
  21.     int m, a, b;
  22.     cin >> m;
  23.     for(int i = 0; i < m; i++)
  24.     {
  25.         cin >> a >> b;
  26.         binds[a][b] = true;
  27.         binds[b][a] = true;
  28.     }
  29. }
  30.  
  31. void search(int phase, int node, int increments)
  32. {
  33.     if(increments == 3) return;
  34.  
  35.     if(phase == 5)
  36.     {
  37.         results.push_back(complex);
  38.         sort(results.back().begin(), results.back().end());
  39.         return;
  40.     }
  41.  
  42.    
  43.     for(int i = 0; i < n; i++)
  44.     {
  45.         bool isIn = false;
  46.         for(int j = 0; j < phase; j++)
  47.         {
  48.             if(complex[j] == i)
  49.             {
  50.                 isIn = true;
  51.                 break;
  52.             }
  53.         }
  54.         if(isIn)
  55.         {
  56.             continue;
  57.         }
  58.  
  59.         if(binds[node][i]
  60.             && (i > complex[0])
  61.             && (phase < 2 || !binds[i][complex[phase - 2]])
  62.             && (phase < 3 || !binds[i][complex[phase - 3]])
  63.             && (phase < 4 || binds[i][complex[0]])
  64.         )
  65.         {
  66.             int nextInc = increments;
  67.             if(i > node)
  68.             {
  69.                 nextInc++;
  70.             }
  71.             complex[phase] = i;
  72.             search(phase + 1, i, nextInc);
  73.         }
  74.     }
  75. }
  76. int main()
  77. {
  78.     ios::sync_with_stdio(false);
  79.     load();
  80.  
  81.     for(int i = 0; i < n; i++)
  82.     {
  83.         complex[0] = i;
  84.         search(1, i, 0);
  85.  
  86.         sort(results.begin(), results.end());
  87.         for(auto res : results)
  88.         {
  89.             for(int j = 0; j < 5; j++)
  90.             {
  91.                 cout << res[j] << " ";
  92.             }
  93.             cout << endl;
  94.         }
  95.         results.clear();
  96.     }
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement