Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. // @author: Danielto1404
  6.  
  7. #define c_boost std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr)
  8.  
  9. using namespace std;
  10.  
  11. inline void file_open(const string& name) {
  12.     freopen((name + ".in").c_str(), "r", stdin);
  13.     freopen((name + ".out").c_str(), "w", stdout);
  14. }
  15.  
  16. using graph = vector <vector <int>>;
  17.  
  18. void fillNextAndPrev(int start, int end, vector <int>& next, vector <int>& prev) {
  19.     next[start] = end;
  20.     prev[end] = start;
  21. }
  22.  
  23. void find3lengthCycle(int n, graph& g, vector <int>& next, vector <int>& prev) {
  24.     int fst = 0;
  25.     for (int snd = 0; snd < n; ++snd)
  26.         for (int thd = 0; thd < n; ++thd)
  27.             if (g[fst][snd] == 1 && g[snd][thd] == 1 && g[thd][fst] == 1 && snd != thd) {
  28.                 fillNextAndPrev(fst, snd, next, prev);
  29.                 fillNextAndPrev(snd, thd, next, prev);
  30.                 fillNextAndPrev(thd, fst, next, prev);
  31.                 return;
  32.             }
  33. }
  34.  
  35. bool transition(int n, graph& g, vector <int>& next, vector <int>& prev) {
  36.     for (int i = 0; i < n; ++i) {
  37.         if (next[i] != -1) continue;
  38.  
  39.         for (int v = 0; v < n; ++v) {
  40.             if (next[v] == -1) continue;
  41.             // Обход по циклу пока не встретим первую вершину
  42.             for (int u = next[v]; u != v; u = next[u]) {
  43.                 if (g[v][i] == 1 && g[i][u] == 1) {
  44.                     fillNextAndPrev(prev[u], i, next, prev);
  45.                     fillNextAndPrev(i, u, next, prev);
  46.                     return true;
  47.                 }
  48.             }
  49.         }
  50.     }
  51.     return false;
  52. }
  53.  
  54. void outsideTransition(int n, graph& g, vector <int>& next, vector <int>& prev) {
  55.     for (int fst = 0; fst < n; ++fst) {
  56.         if (next[fst] != -1) continue;
  57.  
  58.         for (int i = 0; i < n; ++i) {
  59.             if (next[i] == -1 || g[fst][i] == 0) continue;
  60.  
  61.             for (int snd = 0; snd < n; ++snd) {
  62.                 if (fst == snd || next[snd] != -1 || g[prev[i]][snd] == 0) continue;
  63.  
  64.                 fillNextAndPrev(prev[i], snd, next, prev);
  65.                 fillNextAndPrev(snd, fst, next, prev);
  66.                 fillNextAndPrev(fst, i, next, prev);
  67.                 return;
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. int main() {
  74.     c_boost;
  75.     file_open("guyaury");
  76.     int n;
  77.     string edge;
  78.     cin >> n;
  79.     graph g = graph(n, vector <int>(n, 0));
  80.     for (int i = 1; i < n; ++i) {
  81.         cin >> edge;
  82.         for (int j = 0; j < int(edge.length()); ++j) {
  83.             if (edge[j] == '1') {
  84.                 g[i][j] = 1;
  85.             } else {
  86.                 g[j][i] = 1;
  87.             }
  88.         }
  89.     }
  90.  
  91.     vector <int> next(n, -1);
  92.     vector <int> prev(n, -1);
  93.  
  94.     find3lengthCycle(n, g, next, prev);
  95.     int cycleLength = 3;
  96.  
  97.     while (cycleLength != n) {
  98.         bool isChanged = transition(n, g, next, prev);
  99.         if (isChanged)
  100.             continue;
  101.  
  102.         outsideTransition(n, g, next, prev);
  103.         cycleLength++;
  104.     }
  105.  
  106.     int start = next.front();
  107.     while (true) {
  108.         cout << start + 1 << ' ';
  109.         start = next[start];
  110.         if (start == next.front()) break;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement