모음 집 - 템 플 릿

1101 단어 NOIP-데이터 구조
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

좋은 웹페이지 즐겨찾기