CCCPaste

Untitled

#include 

using namespace std;
const int maxn = 105;
int idx[maxn];
int size[maxn];
int find_root(int x) {
    while(idx[x] != x) {
        idx[x] = idx[idx[x]];
        x = idx[x];
    }
    return x;
}
bool find(int A, int B) {
    if(find_root(A) == find_root(B)) {
        return true;
    }
    return false;
}
void union_two_elements(int A, int B) {
    int idx_A = find_root(A);
    int idx_B = find_root(B);
    if(size[idx_A] < size[idx_B]) {
        idx[idx_A] = idx[idx_B];
        size[idx_B] += size[idx_A];
    }
    else {
        idx[idx_B] = idx[idx_A];
        size[idx_A] += size[idx_B];
    }
}
int main() {
    for(int i = 0; i < maxn; i++) {
        idx[i] = i;
        size[i] = 1;
    }
    while(true) {
        string s;
        cin >> s;
        if(s == "u") {
            int a, b;
            cin >> a >> b;
            union_two_elements(a, b);
        }
        else {
            int a, b;
            cin >> a >> b;
            if(find(a, b)) {
                cout << "YES\n";
            }
            else {
                cout << "NO\n";
            }
        }
    }
    return 0;
}