Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- int main()
- {
- int n, m;
- cin >> n >> m;
- vector <vector <int>> mas(n + 1);
- vector <bool> used(n + 1);
- vector <int> answer(n + 1);
- for (int i = 0, x, y; i < m; i++)
- {
- cin >> x >> y;
- mas[x].push_back(y);
- mas[y].push_back(x);
- }
- int count = 1;
- queue <int> q;
- for (int i = 1; i < n + 1; i++)
- {
- if (used[i] == false)
- {
- q.push(i);
- while(!q.empty())
- {
- used[q.front()] = true;
- answer[q.front()] = count;
- for (int j = 0; j < mas[q.front()].size(); j++)
- {
- if (used[mas[q.front()][j]] == false)
- q.push(mas[q.front()][j]);
- }
- q.pop();
- }
- count++;
- }
- }
- cout << count - 1 << endl;
- for (int i = 1; i < n + 1; i++)
- cout << answer[i] << " ";
- return 0;
- }
- **********************************************************************
- **********************************************************************
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main()
- {
- int n, m;
- cin >> n >> m;
- vector<int> Coomp(n);
- vector<int> ans;
- for (int i(0); i < n; i++)
- Coomp[i] = i;
- for (int i(0); i < m; i++)
- {
- int a, b, maxB(0);
- cin >> a >> b;
- a--; b--;
- if (b < a)
- swap(a, b);
- for (int j(0); j < n;j++)
- if (Coomp[j] == Coomp[b] && b != j)
- Coomp[j] = Coomp[a];
- maxB = Coomp[b];
- Coomp[b] = Coomp[a];
- for (int j(0); j < n;j++)
- if (Coomp[j] > Coomp[a] && Coomp[j] > maxB)
- Coomp[j]--;
- }
- sort(Coomp.begin(), Coomp.end());
- cout << Coomp[n - 1]+1 << endl;
- for (int i(0); i < n; i++)
- cout << Coomp[i]+1 << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement