• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            /*
              Name: 并查集UFSet類
              Copyright: 始發于goal00001111的專欄;允許自由轉載,但必須注明作者和出處
              Author: goal00001111
              Date: 23-12-08 15:21
              Description: 實現了普通的查找和合并的算法,也實現了壓縮路徑和按大小求并高效算法,并對兩者進行了測試比較。
            有關算法的分析討論詳見拙作《一種簡單而有趣的數據結構--并查集》:
            http://blog.csdn.net/goal00001111/archive/2008/12/24/3595844.aspx
            */
            #include <iostream>
            #include <time.h>

            using namespace std;

            const int MAX = 50000; //集合的最大成員數量

            class UFSet{ //并查集類
            public:
                UFSet(int s = MAX); //構造函數
               
                UFSet(const UFSet & obj); //拷貝構造函數
               
                ~UFSet() //析構函數
                {
                    delete []father;
                }
               
                UFSet & operator = (const UFSet & obj); //賦值函數
                
                int FindFather(int pos); //尋找序號pos的族長大人
               
                bool Unite(int posI, int posJ);//將成員posI和posJ合并到同一個家族
               
                int FindFatherAndReducePath(int pos); //查找族長并壓縮路徑
               
                bool UnionBySize (int posI, int posJ); //按大小求并
               
                bool SameFamily(int posI, int posJ); //判斷成員posI和posJ是否屬于同一家族
               
                void PrintUFSet();

            private:
                int *father; //并查集成員數組,存放各元素的父親結點
                int size;   //并查集成員數量
            };

            UFSet::UFSet(int s) : size(s) //構造函數
            {
                father = new int[size + 1];
                for (int i=0; i<=size; i++) //所有的數組元素值均初始化為-1
                    father[i] = -1;
            }

            UFSet::UFSet(const UFSet & obj) //拷貝構造函數
            {
                size = obj.size;
                father = new int[size + 1];
                for (int i=0; i<=size; i++)
                    father[i] = obj.father[i];
            }


            UFSet & UFSet::operator = (const UFSet & obj) //賦值函數
            {
                if (this == &obj) //檢查自賦值
                    return *this;
               
                delete []father; //釋放原有的內存資源
               
                size = obj.size;  //分配新的內存資源,并復制內容
                father = new int[size + 1];
                for (int i=0; i<=size; i++)
                    father[i] = obj.father[i];
                   
                return *this; //返回本對象的引用
            }

            int UFSet::FindFather(int pos)//尋找序號pos的族長大人。若pos本身是族長,則返回自身
            {
                if (father[pos] < 0)
                    return pos;
                   
                return FindFather(father[pos]);
            }

            bool UFSet::Unite(int posI, int posJ)//將成員posI和posJ合并到同一個家族
            {
                //首先各自去尋找自己的族長
                int fI = FindFather(posI);
                int fJ = FindFather(posJ);
               
                if (fI == fJ) //如果是同一個族長門下,不必合并,即合并失敗
                    return false;
                else
                    father[fJ] = fI; //否則fI當族長:誰讓posI站在posJ的前面呢!
                   
                return true;
            }

            int UFSet::FindFatherAndReducePath(int pos)//查找族長并壓縮路徑
            {
                if (father[pos] < 0)
                    return pos;
                //若自己不是族長,則找到族長后,將所途經的結點均指向族長   
                return father[pos] = FindFatherAndReducePath(father[pos]);
            }

            bool UFSet::UnionBySize(int posI, int posJ)//按大小求并
            {
                //首先各自去尋找自己的族長
                int fI = FindFatherAndReducePath(posI);
                int fJ = FindFatherAndReducePath(posJ);
               
                if (fI == fJ) //如果是同一個族長門下,不必合并,即合并失敗
                    return false;
                else if (father[fI] < father[fJ])
                {//如果族長fI的實力比fJ強,即|fI|>|fJ|,則fI當族長,并修改father[fI]和father[fJ]
                    father[fI] += father[fJ];
                    father[fJ] = fI;
                }
                else              //否則fJ當族長
                {
                    father[fJ] += father[fI];
                    father[fI] = fJ;
                }
               
                return true;
            }

            bool UFSet::SameFamily(int posI, int posJ)//判斷成員posI和posJ是否屬于同一家族
            {
                return FindFatherAndReducePath(posI) == FindFatherAndReducePath(posJ);
            }

            void UFSet::PrintUFSet()//輸出集合的所有元素
            {
                for (int i=0; i<=size; i++)
                    cout << father[i] << ' ';
                cout << endl;
            }

            int main()
            {
                time_t startTime, endTime;
                UFSet obj;
                int p, q;
               
                time(&startTime);
               
                for (int i=0; i<MAX; i++)
                {
                    p = rand() % MAX + 1;
                    q = rand() % MAX + 1;
                    if (p == q)
                        continue;
                       
                    //cout << p << "-" << q << "   ";
            //        if (i % 10 == 0)
            //            cout << endl;
                   
                    obj.Unite(p, q);
                }
               
                while (1)
                {
                    p = rand() % MAX + 1;
                    q = rand() % MAX + 1;
                    if (p == q)
                        continue;
                   
                    if (obj.SameFamily(p, q))
                        cout << endl << p << "≡" << q << endl;
                    else
                        cout << endl << p << "!≡" << q << endl;
                    break;
                }
               
                time(&endTime);
                cout << difftime(endTime, startTime) << endl;
               
                //////////////////////////////////////////////////////////////////////////
                time(&startTime);
                for (int i=0; i<MAX; i++)
                {
                    p = rand() % MAX + 1;
                    q = rand() % MAX + 1;
                    if (p == q)
                        continue;
                       
                    //cout << p << "-" << q << "   ";
            //        if (i % 10 == 0)
            //            cout << endl;
                   
                    obj.UnionBySize(p, q);
                }
               
                while (1)
                {
                    p = rand() % MAX + 1;
                    q = rand() % MAX + 1;
                    if (p == q)
                        continue;
                   
                    if (obj.SameFamily(p, q))
                        cout << endl << p << "≡" << q << endl;
                    else
                        cout << endl << p << "!≡" << q << endl;
                    break;
                }
               
                time(&endTime);
                cout << difftime(endTime, startTime) << endl;
               
                system("pause");
                return 0;
            }
            Posted on 2008-12-24 13:41 夢想飛揚 閱讀(1273) 評論(0)  編輯 收藏 引用
            人妻少妇久久中文字幕| 久久久久亚洲精品男人的天堂| 久久精品一本到99热免费| 久久久国产打桩机| 东京热TOKYO综合久久精品| 中文字幕亚洲综合久久| 青青热久久国产久精品| 漂亮人妻被中出中文字幕久久| 日本欧美久久久久免费播放网| 精品蜜臀久久久久99网站| yellow中文字幕久久网| 94久久国产乱子伦精品免费| 日韩中文久久| 99久久综合狠狠综合久久止| 久久电影网| 精品999久久久久久中文字幕| 激情久久久久久久久久| 亚洲午夜久久久久久久久久| 久久夜色tv网站| 日日噜噜夜夜狠狠久久丁香五月| 激情五月综合综合久久69| 久久亚洲国产成人精品性色| 久久久久久青草大香综合精品| 久久66热人妻偷产精品9| 久久青青草视频| 久久亚洲2019中文字幕| 亚洲嫩草影院久久精品| 久久国产色AV免费观看| 久久婷婷国产剧情内射白浆 | 狠狠色丁香久久婷婷综| 综合久久一区二区三区 | 色婷婷综合久久久久中文字幕 | 亚洲第一永久AV网站久久精品男人的天堂AV | 国产亚洲精午夜久久久久久| 伊人久久大香线蕉av不变影院| 久久精品国产国产精品四凭| 97久久香蕉国产线看观看| 香蕉久久夜色精品升级完成| 久久国内免费视频| 麻豆精品久久久久久久99蜜桃| 亚洲国产综合久久天堂|