Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- // @author: Danielto1404
- #define c_boost std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr)
- using namespace std;
- inline void file_open(const string& name) {
- freopen((name + ".in").c_str(), "r", stdin);
- freopen((name + ".out").c_str(), "w", stdout);
- }
- using graph = vector <vector <int>>;
- void fillNextAndPrev(int start, int end, vector <int>& next, vector <int>& prev) {
- next[start] = end;
- prev[end] = start;
- }
- void find3lengthCycle(int n, graph& g, vector <int>& next, vector <int>& prev) {
- int fst = 0;
- for (int snd = 0; snd < n; ++snd)
- for (int thd = 0; thd < n; ++thd)
- if (g[fst][snd] == 1 && g[snd][thd] == 1 && g[thd][fst] == 1 && snd != thd) {
- fillNextAndPrev(fst, snd, next, prev);
- fillNextAndPrev(snd, thd, next, prev);
- fillNextAndPrev(thd, fst, next, prev);
- return;
- }
- }
- bool transition(int n, graph& g, vector <int>& next, vector <int>& prev) {
- for (int i = 0; i < n; ++i) {
- if (next[i] != -1) continue;
- for (int v = 0; v < n; ++v) {
- if (next[v] == -1) continue;
- // Обход по циклу пока не встретим первую вершину
- for (int u = next[v]; u != v; u = next[u]) {
- if (g[v][i] == 1 && g[i][u] == 1) {
- fillNextAndPrev(prev[u], i, next, prev);
- fillNextAndPrev(i, u, next, prev);
- return true;
- }
- }
- }
- }
- return false;
- }
- void outsideTransition(int n, graph& g, vector <int>& next, vector <int>& prev) {
- for (int fst = 0; fst < n; ++fst) {
- if (next[fst] != -1) continue;
- for (int i = 0; i < n; ++i) {
- if (next[i] == -1 || g[fst][i] == 0) continue;
- for (int snd = 0; snd < n; ++snd) {
- if (fst == snd || next[snd] != -1 || g[prev[i]][snd] == 0) continue;
- fillNextAndPrev(prev[i], snd, next, prev);
- fillNextAndPrev(snd, fst, next, prev);
- fillNextAndPrev(fst, i, next, prev);
- return;
- }
- }
- }
- }
- int main() {
- c_boost;
- file_open("guyaury");
- int n;
- string edge;
- cin >> n;
- graph g = graph(n, vector <int>(n, 0));
- for (int i = 1; i < n; ++i) {
- cin >> edge;
- for (int j = 0; j < int(edge.length()); ++j) {
- if (edge[j] == '1') {
- g[i][j] = 1;
- } else {
- g[j][i] = 1;
- }
- }
- }
- vector <int> next(n, -1);
- vector <int> prev(n, -1);
- find3lengthCycle(n, g, next, prev);
- int cycleLength = 3;
- while (cycleLength != n) {
- bool isChanged = transition(n, g, next, prev);
- if (isChanged)
- continue;
- outsideTransition(n, g, next, prev);
- cycleLength++;
- }
- int start = next.front();
- while (true) {
- cout << start + 1 << ' ';
- start = next[start];
- if (start == next.front()) break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement