#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

#if 0
static const int MAX_NUM = 10001;

int FindRoot(int elem[], int target)
{
    int parent;

    parent = target;
    for (;;) {
        if (parent == elem[parent]) {
            break;
        }
        else {
            parent = elem[parent];
        }
    }

    return parent;
}

void Unite(int elem[], int t1, int t2)
{
    int p1, p2;

    p1 = FindRoot(elem, t1);
    p2 = FindRoot(elem, t2);

    elem[p2] = p1;

    return;
}

bool Same(int elem[], int t1, int t2)
{
    bool ret;
    int p1, p2;

    p1 = FindRoot(elem, t1);
    p2 = FindRoot(elem, t2);

    if (p1 == p2) {
        ret = true;
    }
    else {
        ret = false;
    }

    return ret;
}

int main(void)
{
    int i;
    int num, qnum;
    int q;
    int t1, t2;
    int elem[MAX_NUM];

    scanf("%d %d", &num, &qnum);
    for (i = 1; i <= num; i++) {
        elem[i] = i;
    }

    for (i = 0; i < qnum; i++) {
        scanf("%d %d %d", &q, &t1, &t2);
        switch (q) {
        case 0 :
            Unite(elem, t1, t2);
            break;
        case 1 :
            if (Same(elem, t1, t2) == true) {
                printf("1\n");
            }
            else {
                printf("0\n");
            }
            break;
        default :
            break;
        }
    }
    return 0;
}
#else

class UnionFindTree
{
public :
    UnionFindTree(int num) {
        m_iNum = num;
        m_vElem.resize(num);
        m_vRank.resize(num, 0);
        for (int i = 0; i < num; i++) {
            m_vElem[i] = i;
        }
        return;
    };

    int  FindParent(int target);
    bool IsSameParent(int t1, int t2);
    void UniteElem(int t1, int t2);

    void Print() {
        for (int i = 0; i < m_iNum; i++) {
            printf("%d: %d\n", i, FindParent(i));
        }
    };

private :
    int m_iNum;
    vector<int> m_vElem;
    vector<int> m_vRank;
};

int UnionFindTree::FindParent(int target)
{
    int ret;

    if (target == m_vElem[target]) {
        ret = target;
    }
    else {
        ret = FindParent(m_vElem[target]);
        m_vElem[target] = ret;
    }

    return ret;
}

bool UnionFindTree::IsSameParent(int t1, int t2)
{
    bool ret;
    int p1, p2;

    p1 = FindParent(t1);
    p2 = FindParent(t2);
    ret = (p1 == p2) ? true : false;

    return ret;
}

void UnionFindTree::UniteElem(int t1, int t2)
{
    int p1, p2;

    p1 = FindParent(t1);
    p2 = FindParent(t2);
    if (m_vRank[t1] > m_vRank[t2]) {
        m_vElem[p2] = p1;
    }
    else if (m_vRank[t1] < m_vRank[t2]) {
        m_vElem[p1] = p2;
    }
    else {
        m_vElem[p2] = p1;
        ++m_vRank[p1];
    }

    return;
}


int main(void)
{
    int i;
    int num, qnum;
    int q;
    int t1, t2;

    scanf("%d %d", &num, &qnum);
    UnionFindTree cUFT(num);

    for (i = 0; i < qnum; i++) {
        scanf("%d %d %d", &q, &t1, &t2);
        switch (q) {
        case 0 :
            cUFT.UniteElem(t1, t2);
            break;
        case 1 :
            if (cUFT.IsSameParent(t1, t2) == true) {
                printf("1\n");
            }
            else {
                printf("0\n");
            }
            break;
        default :
            break;
        }
    }
//    cUFT.Print();
    return 0;
}
#endif

Measured memory_bytes: 1244