Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int N;
- vector<vector<int>>g;
- vector <bool>visited(false);
- int ss = 0;
- vector<int>color;
- vector<int>klv;
- vector<vector<int>>nv;
- void DFS(int V)
- {
- visited[V] = true;
- color[V] = ss;
- nv[ss].push_back(V);
- for (int i = 0; i < g[V].size(); i++)
- {
- int to = g[V][i];
- if (!visited[to])
- {
- DFS(to);
- }
- }
- }
- int main()
- {
- int M, a, b,q;
- cin >> N >> M;
- g.resize(N);
- nv.resize(N);
- visited.resize(N);
- for (int i = 0; i < M; i++)
- {
- cin >> a >> b;
- a--;
- b--;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- color.resize(N);
- for (int i = 0; i < N; i++)
- {
- if (!visited[i])
- {
- ss++;
- DFS(i);
- }
- }
- cout << ss;
- for (int i = 1; i <= ss; i++)
- {
- cout << endl << nv[i].size() << endl;
- q = nv[i].size();
- while (q > 0)
- {
- cout << nv[i][q - 1] + 1 << " ";
- q--;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement