Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. void search(vector <vector <int> > &graph, vector <int> &extra, int &i, int &count){
  7. extra[i] = count;
  8. for (int j = 0; j < graph[i].size(); j++) {
  9. if (extra[graph[i][j]] == 0) {
  10. search(graph, extra, graph[i][j], count);
  11. }
  12. }
  13. return;
  14. }
  15.  
  16. int main() {
  17. ifstream fin("components.in");
  18. ios_base::sync_with_stdio(false); cin.tie(NULL);
  19. ofstream fout("components.out");
  20. int n, nr;
  21. fin >> n >> nr;
  22. vector <vector <int> > graph(n, vector<int>());
  23. vector <int> extra(n, 0);
  24. int a, b;
  25. for (int i = 0; i < nr; i++){
  26. fin >> a >> b;
  27. graph[a-1].push_back(b-1);
  28. graph[b-1].push_back(a-1);
  29. }
  30. int count = 1;
  31. for (int i = 0; i < n; i++){
  32. if (extra[i] == 0){
  33. search(graph, extra, i, count);
  34. count++;
  35. }
  36. }
  37. fout << count-1 << "\n";
  38. for (int i = 0; i < n; i++){
  39. fout << extra[i] << ' ';
  40. }
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement