• <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久久久精品| 久久久91人妻无码精品蜜桃HD | 国产香蕉久久精品综合网| 偷窥少妇久久久久久久久| 亚洲中文久久精品无码ww16| 久久精品国产亚洲av水果派| 青青青国产精品国产精品久久久久| 久久er国产精品免费观看8| 久久婷婷人人澡人人爽人人爱 | 久久高清一级毛片| 狠狠色噜噜色狠狠狠综合久久| 精品少妇人妻av无码久久| 品成人欧美大片久久国产欧美...| 亚洲精品国产综合久久一线| 久久国产欧美日韩精品| 久久精品国产一区二区三区| 久久久久久夜精品精品免费啦| 色天使久久综合网天天| 欧美牲交A欧牲交aⅴ久久| 人人狠狠综合88综合久久| 777久久精品一区二区三区无码| 东方aⅴ免费观看久久av| 国产免费久久久久久无码| 国产精品美女久久久久网| 97精品依人久久久大香线蕉97| 久久精品成人一区二区三区| www.久久精品| 久久精品一区二区三区不卡| 久久成人国产精品| 欧美一区二区三区久久综| 亚洲中文字幕无码久久2020| 久久综合伊人77777麻豆| 精品国产乱码久久久久久浪潮| 免费观看久久精彩视频 | 久久99精品久久久久久野外 | 久久天天躁狠狠躁夜夜2020 | 少妇被又大又粗又爽毛片久久黑人 | 久久丝袜精品中文字幕| 久久久久亚洲AV无码专区网站| 中文字幕一区二区三区久久网站|