Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxn = 1e5 + 10;
- int idx[maxn];
- void init() {
- for(int i = 0; i < maxn; i++) {
- idx[i] = i;
- }
- }
- void unite(int A, int B) {
- int root_A = idx[A];
- for(int i = 0; i < maxn; i++) {
- if(idx[i] == root_A) {
- idx[i] = idx[B];
- }
- }
- }
- bool check_if_they_belong_to_same_set(int A, int B) {
- return idx[A] == idx[B];
- }
- int main()
- {
- init();
- while(true) {
- string s;
- cin >> s;
- if(s == "end") {
- break;
- }
- if(s == "union") {
- int a, b;
- cin >> a >> b;
- unite(a, b);
- }
- else {
- int a, b;
- cin >> a >> b;
- cout << check_if_they_belong_to_same_set(a, b) << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement