Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <utility>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- #include <set>
- #include <sstream>
- #include <list>
- using namespace std;
- class DS {
- public:
- DS(int n) : parentOfEach(n), countOfEach(n), minOfEach(n), maxOfEach(n) {
- for (int i = 0; i != n; i++) {
- parentOfEach[i] = i;
- countOfEach[i] = 1;
- minOfEach[i] = i;
- maxOfEach[i] = i;
- }
- }
- vector<int> parentOfEach;
- vector<int> countOfEach;
- vector<int> minOfEach;
- vector<int> maxOfEach;
- int GetCount(int index) {
- return countOfEach[GetElement(index)];
- }
- int GetMax(int index) {
- return maxOfEach[GetElement(index)];
- }
- int GetMin(int index) {
- return minOfEach[GetElement(index)];
- }
- void Union(int lhs, int rhs) {
- if (!Check(lhs, rhs)) {
- countOfEach[GetElement(lhs)] += countOfEach[GetElement(rhs)];
- if (minOfEach[GetElement(lhs)] < minOfEach[GetElement(rhs)])
- minOfEach[GetElement(lhs)] = minOfEach[GetElement(rhs)];
- if (maxOfEach[GetElement(lhs)] > maxOfEach[GetElement(rhs)])
- maxOfEach[GetElement(lhs)] = maxOfEach[GetElement(rhs)];
- parentOfEach[GetElement(rhs)] = GetElement(lhs);
- }
- }
- int GetElement(int index) {
- return parentOfEach[index] == index
- ? index
- : parentOfEach[index] = GetElement(parentOfEach[index]);
- }
- bool Check(int lhs, int rhs) {
- return GetElement(lhs) == GetElement(rhs);
- }
- };
- int main() {
- ifstream reader("dsu.in");
- ofstream writer("dsu.out");
- int n;
- reader >> n;
- DS u(n);
- string s;
- while (true) {
- string command;
- if (reader.eof())
- break;
- reader >> command;
- if (command == "union") {
- int a, b;
- reader >> a >> b;
- u.Union(a - 1, b - 1);
- } else if (command == "get") {
- int a;
- reader >> a;
- int cnt = u.GetCount(a - 1);
- int mx = u.GetMax(a - 1) + 1;
- int mn = u.GetMin(a - 1) + 1;
- writer << mx << " " << mn << " " << cnt << "\n";
- }
- }
- writer.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement