并查集 - 模板

class DisjointSet 
{
private:
    int *father, *rank;
public:
    DisjointSet(int size) 
	{
        father = new int[size];
        rank = new int[size];
        for (int i = 0; i < size; ++i) 
		{
            father[i] = i;
            rank[i] = 0;
        }
    }
    ~DisjointSet() 
	{
         delete[] father;
         delete[] rank;
    }
    int find_set(int node) 
	{
        if (father[node] != node) 
		{
            father[node] = find_set(father[node]);
        }
        return father[node];
    }
    bool merge(int node1, int node2) 
	{
        int ancestor1 = find_set(node1);
        int ancestor2 = find_set(node2);
        if (ancestor1 != ancestor2) 
		{
            if (rank[ancestor1] > rank[ancestor2]) 
			{
                swap(ancestor1, ancestor2);
            }
            father[ancestor1] = ancestor2;
            rank[ancestor2] = max(rank[ancestor2], rank[ancestor1] + 1);
            return true;
        }
        return false;
    }
   int find_father(int node)
    {
    	return father[node];
	}
};



2017 - 3 - 23 


NOIP Day 232

你可能感兴趣的:(NOIP,-,数据结构,数据结构)