Advertisement
Tevronis

104

May 15th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.         int n, m;
  9.         cin >> n >> m;
  10.         vector <vector <int>> mas(n + 1);
  11.         vector <bool> used(n + 1);
  12.         vector <int> answer(n + 1);
  13.         for (int i = 0, x, y; i < m; i++)
  14.         {
  15.                 cin >> x >> y;
  16.                 mas[x].push_back(y);
  17.                 mas[y].push_back(x);
  18.         }
  19.         int count = 1;
  20.         queue <int> q;
  21.         for (int i = 1; i < n + 1; i++)
  22.         {
  23.                 if (used[i] == false)
  24.                 {
  25.                         q.push(i);
  26.                         while(!q.empty())
  27.                         {
  28.                                 used[q.front()] = true;
  29.                                 answer[q.front()] = count;
  30.                                 for (int j = 0; j < mas[q.front()].size(); j++)
  31.                                 {
  32.                                         if (used[mas[q.front()][j]] == false)
  33.                                                 q.push(mas[q.front()][j]);
  34.                                 }
  35.                                 q.pop();
  36.                         }
  37.                         count++;
  38.                 }
  39.         }
  40.         cout << count - 1 << endl;
  41.         for (int i = 1; i < n + 1; i++)
  42.                 cout << answer[i] << " ";
  43.         return 0;
  44. }
  45. **********************************************************************
  46. **********************************************************************
  47.  
  48. #include <iostream>
  49. #include <vector>
  50. #include <algorithm>
  51.  
  52. using namespace std;
  53.  
  54. int main()
  55. {
  56.     int n, m;
  57.     cin >> n >> m;
  58.     vector<int> Coomp(n);
  59.     vector<int> ans;
  60.     for (int i(0); i < n; i++)
  61.         Coomp[i] = i;
  62.     for (int i(0); i < m; i++)
  63.     {
  64.         int a, b, maxB(0);
  65.         cin >> a >> b;
  66.         a--; b--;
  67.         if (b < a)
  68.             swap(a, b);
  69.         for (int j(0); j < n;j++)
  70.             if (Coomp[j] == Coomp[b] && b != j)
  71.                 Coomp[j] = Coomp[a];
  72.         maxB = Coomp[b];
  73.         Coomp[b] = Coomp[a];
  74.         for (int j(0); j < n;j++)
  75.             if (Coomp[j] > Coomp[a] && Coomp[j] > maxB)
  76.                 Coomp[j]--;
  77.     }
  78.     sort(Coomp.begin(), Coomp.end());
  79.     cout << Coomp[n - 1]+1 << endl;
  80.     for (int i(0); i < n; i++)
  81.         cout << Coomp[i]+1 << " ";
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement