class ConnectingGraph {
public:
    ConnectingGraph(int n) {
        father.resize(n + 1);
        for (int i = 1; i <= n; i++) {
            father[i] = i;
        }
    }

    void connect(int a, int b) {
        int father_a = find(a);
        int father_b = find(b);
        if (father_a != )
    }

    void  connect(int a, int b) {
        // Write your code here
        int father_a = find(a);
        int father_b = find(b);
        if (father_a != father_b) {
            father[father_a] = father_b;
        }
        return;
    }

    bool query(int a, int b) {
        // Write your code here
        int father_a = find(a);
        int father_b = find(b);
        return father_a == father_b;
    }

private:
    vector<int> father;

    int find(int x) {
        if (x == father[x]) {
            return x;
        }
        father[x] = find(father[x]);
        return father[x];
    }
};

results matching ""

    No results matching ""