Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- void search(vector <vector <int> > &graph, vector <int> &extra, int &i, int &count){
- extra[i] = count;
- for (int j = 0; j < graph[i].size(); j++) {
- if (extra[graph[i][j]] == 0) {
- search(graph, extra, graph[i][j], count);
- }
- }
- return;
- }
- int main() {
- ifstream fin("components.in");
- ios_base::sync_with_stdio(false); cin.tie(NULL);
- ofstream fout("components.out");
- int n, nr;
- fin >> n >> nr;
- vector <vector <int> > graph(n, vector<int>());
- vector <int> extra(n, 0);
- int a, b;
- for (int i = 0; i < nr; i++){
- fin >> a >> b;
- graph[a-1].push_back(b-1);
- graph[b-1].push_back(a-1);
- }
- int count = 1;
- for (int i = 0; i < n; i++){
- if (extra[i] == 0){
- search(graph, extra, i, count);
- count++;
- }
- }
- fout << count-1 << "\n";
- for (int i = 0; i < n; i++){
- fout << extra[i] << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement