• <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>

            ACM___________________________

            ______________白白の屋
            posts - 182, comments - 102, trackbacks - 0, articles - 0
            <2010年9月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            自己的 并查集 模板

            Posted on 2010-08-10 11:36 MiYu 閱讀(527) 評論(0)  編輯 收藏 引用 所屬分類: ACM ( 并查集 )ACM ( MST 最小生成樹 )
            MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋

            剛剛學習完并查集的基礎知識.. 自己寫了3個模板類 . 發上和大家分享下:

            MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋

            #include 
            <iostream>
            using namespace std;
            typedef 
            class arrUFS{
                   
            public:
                          arrUFS(
            int n = 0):N(n){ set = new int[n]; for ( int i = 0; i != N; ++ i) set[i] = i; };
                          
            ~arrUFS(){ delete [] set; };
                          
            int find ( int x ){ return set[x]; }
                          
            void Merge1( int a,int b){   int i = min(set[a],set[b]);
                                                       
            int j = max(set[a],set[b]);
                                                       
            for ( int k=1; k<=N; k++) {
                                                             
            if (set[k] == j)
                                                             
            set[k] = i;
                                                       }
                                                   }
                   
            private:
                          
            int *set;
                          
            int N;         
            }arrUFS; 
            // 數組形式

            //樹形并查集, 路徑壓縮 
            typedef struct {
                 
            int parent;
                 
            int cnt;   
            }Tset;  

            typedef 
            class treeUFS{
                   
            public:
                          treeUFS(
            int n = 0):N(n+2) { set = new Tset[N]; 
                                                      
            for ( int i = 0; i != N; ++ i) 
                                                      
            set[i].parent = i,set[i].cnt = 1
                                                    }
                          
            ~treeUFS(){ delete [] set; };
                          
            int find ( int x ){ int r = x; while ( set[r].parent != r ) //循環結束,則找到根節點
                                                                r = set[r],parent;       
                                                         
            int i = x;
                                                         
            while ( i != r) //本循環修改查找路徑中所有節點
                                                         {   
                                                             
            int j = set[i].parent;
                                                             
            set[i].parent = r;
                                                             i 
            = j;
                                                         } 
                                               
            return r;
                                            }
                          
            void Merge1( int x,int y ){  x = find ( x );  y = find ( y );  
                                                       
            if ( x == y ) return;
                                                       
            if ( set[x].cnt > set[y].cnt ){
                                                            
            set[y].parent = x;
                                                            
            set[x].cnt += set[y].cnt;
                                                       }
                                                       
            else{
                                                               
            set[x].parent = y;
                                                               
            set[y].cnt += set[x].cnt;        
                                                           }
                                                    }
                   
            private:
                          
            int *set;
                          
            int N;         
            }treeUFS; 
            // 樹形式  路徑壓縮 

            //屬性并查集, 帶樹深 
            typedef struct {
                 
            int parent;
                 
            int height;   
            }Tset;  

            typedef 
            class treeUFS{
                   
            public:
                          treeUFS(
            int n = 0):N(n+2) { set = new Tset[N];
                                                      visited 
            = new bool[N]; 
                                                      
            for ( int i = 0; i != N; ++ i) 
                                                      
            set[i].parent = i,set[i].height = 1,visited[i] = false
                                                    }
                          
            ~treeUFS(){ delete [] set; };
                          
            int find ( int x ){  int r = x;  while ( r != set[r].parent ) r = ser[r].parent;
                                               
            return r;
                                            }
                          
            void Merge1( int x,int y ){  x = find ( x );  y = find ( y );  
                                                       
            if ( x == y ) return;
                                                       
            if ( set[x].height == set[y].height ){
                                                            
            set[y].parent = x;
                                                            
            set[x].height ++;
                                                       }
                                                       
            else if ( set[x].height < set[y].height ) {
                                                                 
            set[x].parent = y;       
                                                               }
                                                       
            else{
                                                                 
            set[y].parent = x;
                                                           }
                                                    }
                   
            private:
                          
            int *set;
                          
            bool *visited;
                          
            int N;         
            }treeUFS; 
            // 樹形式 帶樹深 

            int main ()
            {
                
            return 0
            }
            91麻豆精品国产91久久久久久| 一本久久久久久久| 久久久久久久久波多野高潮| 婷婷伊人久久大香线蕉AV| 精品国际久久久久999波多野| 欧美精品一本久久男人的天堂| 久久精品国产第一区二区| 狠狠色婷婷久久一区二区| 久久精品国产一区| 久久久久久久久波多野高潮| 国产成人综合久久精品尤物| 久久久久精品国产亚洲AV无码| 日韩精品国产自在久久现线拍| 2021久久精品免费观看| 久久精品国产秦先生| 久久精品国产日本波多野结衣| 国产精品无码久久综合网| 午夜不卡久久精品无码免费| 亚洲乱码日产精品a级毛片久久 | 久久av高潮av无码av喷吹| 伊人久久大香线蕉av不变影院| 国产成人综合久久精品尤物| 97精品久久天干天天天按摩| 人人妻久久人人澡人人爽人人精品| 欧美伊香蕉久久综合类网站| 无遮挡粉嫩小泬久久久久久久| 久久亚洲精品无码VA大香大香| 久久精品国产精品亜洲毛片 | 2021国产精品午夜久久| 久久久精品视频免费观看| 人人狠狠综合久久亚洲88| 国产精品美女久久久久| 久久久久亚洲AV片无码下载蜜桃 | 久久99国产亚洲高清观看首页| 亚洲国产精品无码久久SM| 国产精品久久久香蕉| 久久精品视频一| 伊人久久大香线蕉综合影院首页| 久久久久亚洲AV无码观看| 99久久精品免费看国产一区二区三区 | 无码任你躁久久久久久|