Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxn = 105;
- int idx[maxn];
- int find_root(int x) {
- while(idx[x] != x) {
- idx[x] = idx[idx[x]];
- x = idx[x];
- }
- return x;
- }
- bool check(int A, int B) {
- if(find_root(A) == find_root(B)) {
- return true;
- }
- return false;
- }
- void union_two_elements(int A, int B) {
- int root_a = find_root(A);
- int root_b = find_root(B);
- idx[root_a] = root_b;
- }
- int main() {
- for(int i = 0; i < maxn; i++) {
- idx[i] = i;
- }
- while(true) {
- string s;
- cin >> s;
- if(s == "union") {
- int a,b;
- cin >> a >> b;
- union_two_elements(a, b);
- }
- else if(s == "check") {
- int a, b;
- cin >> a >> b;
- if(check(a, b)) {
- cout << "YES" << endl;
- }
- else {
- cout << "NO" << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement