• <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 閱讀(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
            }
            国产精品一久久香蕉国产线看 | 伊人色综合久久天天| 久久久久久久久久久久中文字幕| 亚洲精品美女久久久久99| 国产精品久久久久天天影视 | 久久久久国产日韩精品网站 | 久久99精品久久久久久动态图| 国产精品久久久久aaaa| 一级A毛片免费观看久久精品| 欧美黑人又粗又大久久久| 亚洲一区二区三区日本久久九| 一本久久免费视频| 色偷偷888欧美精品久久久| 久久亚洲精品无码aⅴ大香| 久久天堂AV综合合色蜜桃网| 久久97久久97精品免视看| 婷婷久久久亚洲欧洲日产国码AV | 久久精品中文字幕一区| 国产成人久久精品麻豆一区| 精品无码久久久久国产动漫3d| 久久精品国产影库免费看| 久久99精品国产麻豆宅宅| 久久九色综合九色99伊人| 久久91亚洲人成电影网站| 综合网日日天干夜夜久久| 人妻无码久久精品| 久久精品国产精品亚洲精品| 五月丁香综合激情六月久久| 亚洲国产成人精品91久久久| 国产激情久久久久影院老熟女| 久久久久久久人妻无码中文字幕爆| 久久这里的只有是精品23| 久久久久国产一区二区| 国产AV影片久久久久久| 久久综合狠狠色综合伊人| 国产精品久久久久jk制服| 中文字幕无码精品亚洲资源网久久| 久久久久亚洲AV无码去区首| 国产真实乱对白精彩久久| 久久人人爽人人精品视频| 国产农村妇女毛片精品久久|