並查集

/***************************************
*@time              2016/08/14 09:09
*@palce             DHU.5005
*description        並查集操作
**************************************/
#include<cstdio>
#include<map>
using namespace std;
/*********************************************************************
*@函數名                     find_Root
*@parameter                  map<int,int>* Union    儲存並查集,如果節點是非根節點則Union[node]表示node的前驅節點,
*                                                   如果節點是根節點則Union[node]表示以node爲跟節點的樹的節點數
*@parameter                  map<int,bool>* Root    判斷節點是否爲根節點
*@parameter                  int x                  要找到根節點的點
*@description                                       根據Union,Root找到一個節點的根節點
**************************************************************************************************************/
int find_Root(map<int,int>* Union,map<int,bool>* Root,int x);
/*********************************************************************
*@函數名                     creat_Tree
*@parameter                  map<int,int>* Union    儲存並查集,如果節點是非根節點則Union[node]表示node的前驅節點,
*                                                   如果節點是根節點則Union[node]表示以node爲跟節點的樹的節點數
*@parameter                  map<int,bool>* Root    判斷節點是否爲根節點
*@parameter                  int x ,int y           路徑的兩端節點
*@description                                       將節點加入並查集
**************************************************************************************************************/
void creat_Tree(map<int,int>* Union,map<int,bool>* Root,int x,int y);
/*********************************************************************
*@函數名                     count_Trees_Num
*@parameter                  map<int,bool>* Root    判斷節點是否爲根節點
*@return value               int                    樹的個數,也就是根節點的個數
*@description                                       統計樹的個數,也就是根節點的個數
**************************************************************************************************************/
int count_Trees_Num(map<int,bool>* Root);
int find_Root(map<int,int>* Union,map<int,bool>* Root,int x)
{
    while(!(*Root)[x]) x=(*Union)[x];
    return x;
}
void creat_Tree(map<int,int>* Union,map<int,bool>* Root,int x,int y)
{
    int rootX=find_Root(Union,Root,x);
    int rootY=find_Root(Union,Root,y);
    //x,y不在同一顆樹中,將x所在的樹與y所在的是合併
    if(rootX!=rootY)
    {
        if((*Union)[rootX]>(*Union)[rootY])//x樹較大,則將y數加入x數
        {
            (*Union)[rootX]+=(*Union)[rootY];//統計合併後樹中節點的個數
            (*Union)[rootY]=rootX;
            (*Root)[rootY]=false;
        }
        else
        {
            (*Union)[rootY]+=(*Union)[rootX];
            (*Union)[rootX]=rootY;
            (*Root)[rootX]=false;
        }
    }
}
int count_Trees_Num(map<int,bool>* Root)
{
    map<int,bool>::iterator it;
    int sum=0;
    for(it=(*Root).begin();it!=(*Root).end();it++)
    {
        if(it->second==true) sum++;
    }
    return sum;
}
int main()
{
    int num_Nodes;
    int num_Routes;
    map<int,int> Union;
    map<int,bool> Root;
   while(scanf("%d",&num_Nodes)&&num_Nodes)
   {
       Union.clear();
       Root.clear();
       for(int i=0;i<num_Nodes;i++)
        {
            int node;
            scanf("%d",&node);
            Root[node]=true;
            Union[node]=node;
        }
        scanf("%d",&num_Routes);
        for(int i=0;i<num_Routes;i++)
        {
            int start_Node;
            int end_Node;
            scanf("%d %d",&start_Node,&end_Node);
            creat_Tree(&Union,&Root,start_Node,end_Node);
        }
        int sum=count_Trees_Num(&Root);
        printf("sum=%d\n",sum-1);
   }

}

測試樣例:

http://acm.hdu.edu.cn/showproblem.php?pid=1232

發佈了35 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章