Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Graph
- {
- private:
- bool orient;
- int n, **arr, **vse_rebra, shet_reber, comp = 0, *dlina_comp;
- public:
- Graph (int **matrix, int x, int shet)
- {
- n = x;
- arr = matrix;
- shet_reber = shet;
- }
- void deep(int x, int *R);
- void deep_second(int *R, int x);
- void get_component();
- void poisk_comp();
- int get_comp();
- };
- void Graph::deep(int x, int *R)
- {
- R[x] = comp;
- for (int i = 0; i < n; i++)
- if (arr[x][i] && !R[i])
- deep(i, R);
- }
- void Graph::get_component()
- {
- int *R = new int[n];
- for (int i = 0; i < n; i++)
- R[i] = 0;
- for (int i = 0; i < n; i++)
- if (!R[i])
- {
- comp++;
- deep(i, R);
- }
- dlina_comp = new int[n];
- for(int i = 0; i < n; i++)
- dlina_comp[i] = 0;
- for(int i = 0; i < n; i++)
- dlina_comp[R[i]-1]++;
- cout << comp << endl;
- }
- void Graph::deep_second(int *R, int x)
- {
- R[x] = x+1;
- cout << R[x] << " ";
- for (int i = 0; i < n; i++)
- if (arr[x][i] != 0 && R[i] == 0)
- deep_second (R, i);
- }
- void Graph::poisk_comp()
- {
- int i, comp = 0, *R = new int[n], k = 0;
- for (i = 0; i < n; i++)
- R[i] = 0;
- for (int i = 0; i < n; i++)
- if (!R[i])
- {
- cout << dlina_comp[k] << " ";
- deep_second(R, i);
- k++;
- cout << endl;
- }
- }
- int main()
- {
- int n, shet_reber = 0, x, shisl_comp = 1, shet = 0;
- cin >> n;
- int **arr = new int*[n];
- for (int i =0; i < n; i++)
- arr[i] = new int[n];
- for (int i =0; i < n; i++)
- for (int j =0; j < n; j++)
- {
- cin >> arr[i][j];
- if (arr[i][j] == 1)
- shet_reber++;
- }
- shet_reber /= 2;
- Graph object(arr, n, shet_reber);
- object.get_component();
- object.poisk_comp();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement