Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // решала совместно с Максимом Ткачевым
- #include <iostream>
- using namespace std;
- int m = 0;
- struct Graph
- {
- int n;
- bool** arr;
- Graph(int x)
- {
- n = x;
- arr = new bool* [n] {0};
- for (int i = 0; i < n; i++)
- arr[i] = new bool[n] {0};
- }
- void new_rebro(int a, int b)
- {
- arr[a][b] = 1;
- }
- void delete_rebro(int a, int b)
- {
- arr[a][b] = 0;
- arr[b][a] = 0;
- }
- void DFS(int start, bool* visited)
- {
- m++;
- visited[start] = true;
- for (int i = 0; i < n; i++)
- if (arr[start][i] == true and visited[i] == false)
- DFS(i, visited);
- }
- int izolation()
- {
- int num = 0;
- bool temp = false;
- for (int i = 0; i < n; i++)
- {
- temp = false;
- for (int j = 0; j < n; j++)
- if (arr[i][j] == true)
- {
- temp = true;
- break;
- }
- if (temp == false)
- num++;
- }
- return num;
- }
- void poisk_cycle(int current = 0)
- {
- int invite = 0, out = 0, chislo_reber = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (arr[i][j] == true)
- out++;
- if (arr[j][i] == true)
- invite++;
- }
- if (out != invite)
- {
- cout << "-1";
- return;
- }
- else
- {
- chislo_reber += out;
- out = 0;
- invite = 0;
- }
- }
- int to_delete = 0;
- cout << current + 1 << " ";
- while (to_delete < chislo_reber)
- for (int i = n-1; i >= 0; i--)
- if (arr[current][i] == true)
- {
- delete_rebro(current, i);
- bool* visited = new bool[n]{0};
- m = 0;
- DFS(i, visited);
- delete[] visited;
- int cant_access = izolation();
- if (m + cant_access == n || cant_access == n)
- {
- to_delete += 2;
- cout << i + 1 << " ";
- current = i;
- break;
- }
- else
- {
- new_rebro(i, current);
- new_rebro(current, i);
- }
- }
- }
- };
- int main()
- {
- int n, elem;
- cin >> n;
- Graph object(n);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- {
- cin >> elem;
- if (elem == 1)
- object.new_rebro(i, j);
- }
- object.poisk_cycle(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement