• <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年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            自己的 并查集 模板

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

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

            MiYu原創(chuàng), 轉帖請注明 : 轉載自 ______________白白の屋

            #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; 
            // 數(shù)組形式

            //樹形并查集, 路徑壓縮 
            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 ) //循環(huán)結束,則找到根節(jié)點
                                                                r = set[r],parent;       
                                                         
            int i = x;
                                                         
            while ( i != r) //本循環(huán)修改查找路徑中所有節(jié)點
                                                         {   
                                                             
            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
            }
            国产99久久久国产精免费| 理论片午午伦夜理片久久| 国产精品一区二区久久精品涩爱| 久久久久国产精品| 国产精品伊人久久伊人电影| 久久青青草视频| 爱做久久久久久| 97久久超碰国产精品旧版| 免费精品久久天干天干| 男女久久久国产一区二区三区 | 久久亚洲AV成人出白浆无码国产| 久久久久亚洲AV无码麻豆| 青青青青久久精品国产| 2021国产精品久久精品| 亚洲精品97久久中文字幕无码 | 久久精品国产亚洲综合色| 午夜欧美精品久久久久久久| 久久精品免费全国观看国产| 久久久青草青青国产亚洲免观| 国产精品久久波多野结衣| 久久97精品久久久久久久不卡| 99久久国产宗和精品1上映| 久久婷婷五月综合色高清| 亚洲综合精品香蕉久久网| 久久精品国产精品亚洲毛片| 久久久久亚洲AV无码网站| 久久久久久久人妻无码中文字幕爆| 久久综合丁香激情久久| 嫩草影院久久国产精品| 欧美国产精品久久高清| 亚洲国产精品无码久久98| 久久香蕉国产线看观看99| 亚洲欧美精品一区久久中文字幕| 久久综合狠狠综合久久| 精品久久久无码中文字幕天天| 一本色道久久99一综合| 久久人人爽人人爽人人片AV东京热| 久久久久国产一级毛片高清板| 亚洲国产精品无码久久久秋霞2 | 93精91精品国产综合久久香蕉| 亚洲中文字幕伊人久久无码 |