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

            gzwzm06

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              1 隨筆 :: 52 文章 :: 17 評論 :: 0 Trackbacks
            Trie + 并查集
            #include <stdio.h>
            #include 
            <cstring>

            const int MAXN = 600002 ;

            int father[MAXN] ;
            int degree[MAXN] ;
            int N ;

            int FindSet( int x )
            {
                
            int t , y ;
                t 
            = father[x] ;
                y 
            = x ;
                
            while ( t != father[t] ) t = father[t] ;
                
            while ( x != t )
                
            {
                    y 
            = father[x] ;
                    father[x] 
            = t ;
                    x 
            = y ;
                }


                
            return t ;
            }


            void UnionSet( int x, int y )
            {
                
            int u = FindSet(x) ;
                
            int v = FindSet(y) ;

                father[v] 
            = u ;
            }


            const int CAP = 26 ;
            typedef 
            struct NODE
            {    
                NODE()
                
            {
                    cnt 
            = 0;
                    id 
            = 0;
                    memset(next, NULL, 
            sizeof(NODE));
                }
            ;
                NODE 
            *next[CAP];
                
            int cnt;
                
            int id;
            }
            NODE;

            const int MEMORY = 600001 ;//節點數目
            NODE memory[MEMORY] ;
            class BTree
            {
            public:
                BTree()
                
            {
                    index 
            = 1;
                    id_index 
            = 0;
                    head 
            = &memory[0];
                }


                
            //插入單詞(返回單詞ID)
                int insert(char *str)
                
            {
                    
            int len = (int)strlen(str);
                    NODE 
            *pt = head;
                    
            for (int i = 0; i < len; ++i)
                    
            {
                        
            if (pt->next[str[i]-'a'== NULL)
                        
            {
                            pt
            ->next[str[i]-'a'= &memory[index++];
                        }

                        
                        pt 
            = pt->next[str[i]-'a'];
                    }


                    
            if (pt->cnt == 0)
                        pt
            ->id = id_index++;

                    (pt
            ->cnt)++;//單詞累加一
                    
                    
            return pt->id;
                }


            public:
                NODE 
            *head;
                
            int index;//內存索引
                int id_index;//單詞ID索引
            }
            ;

            void Init()
            {
                
            int i ;

                
            for ( i = 0 ; i < MAXN ; i++ )
                
            {
                    father[i] 
            = i ;
                    degree[i] 
            = 0 ;
                }

            }


            int main()
            {
                
            char str1[12], str2[12] ;
                BTree trie ;
                
            int x , y , num , i ;
                Init() ;
            //    freopen("1.txt", "r", stdin) ;

                
            while ( scanf("%s %s"&str1, &str2) != EOF )
                
            {
                    x 
            = trie.insert( str1 ) ;
                    y 
            = trie.insert( str2 ) ;
                    degree[x]
            ++ ;
                    degree[y]
            ++ ;

                    UnionSet( x, y ) ;
                }


                num 
            = trie.id_index ;
                
            bool con = true ;

                
            if ( num == 0 ){
                    printf(
            "Possible\n") ;
                }

                
            else {
                    
                    x 
            = FindSet( 0 ) ;
                    
                    
            for ( i = 1 ; i < num ; i++ )
                    
            {
                        y 
            = FindSet( i ) ;
                        
            if ( x != y )
                        
            {
                            con 
            = false ;
                            
            break ;
                        }

                    }


                    
            if ( !con )
                    
            {
                        printf(
            "Impossible\n") ;
                    }

                    
            else{
                        x 
            = 0 ;
                        
            for ( i = 0 ; i < num ; i++ )
                        
            {
                            
            if ( degree[i] % 2 != 0 )
                                x
            ++ ;
                        }


                        
            if ( x == 2 || x == 0 )
                        
            {
                            printf(
            "Possible\n") ;
                        }

                        
            else{
                            printf(
            "Impossible\n") ;
                        }

                    }

                }



                
            return 0 ;
            }
            posted on 2008-11-08 15:50 閱讀(431) 評論(2)  編輯 收藏 引用 所屬分類: 字符串處理

            評論

            # re: Pku 2513--Colored Sticks(Trie) 2009-02-11 17:20 LC
            請問一下
            pt->next[str[i]-'a'] = &memory[index++];

            memory是用來保存什么啊
            這句什么意思啊?
            本人不太熟練指針有望指教啊!  回復  更多評論
              

            # re: Pku 2513--Colored Sticks(Trie)[未登錄] 2009-08-21 15:42 tom
            memory 就一數組啊  回復  更多評論
              

            久久男人Av资源网站无码软件| 国产99精品久久| 久久久精品久久久久久| 久久久久久免费一区二区三区| 奇米影视7777久久精品| 久久天天躁狠狠躁夜夜躁2O2O| 久久强奷乱码老熟女网站 | 精品一久久香蕉国产线看播放| 狠狠狠色丁香婷婷综合久久五月| 国产精品久久久久久久| 2020最新久久久视精品爱| 久久香蕉一级毛片| 精品久久久无码中文字幕| 四虎影视久久久免费观看| 久久天天躁狠狠躁夜夜躁2014| 久久妇女高潮几次MBA| 嫩草伊人久久精品少妇AV| 精品久久久久久国产| 国产一区二区精品久久凹凸 | 91亚洲国产成人久久精品| 久久夜色撩人精品国产| 中文字幕久久精品| 国产成人精品久久免费动漫| 国产精品无码久久综合网| 国产精品久久婷婷六月丁香| 久久无码人妻一区二区三区| 欧美亚洲国产精品久久蜜芽| 久久国产亚洲精品| 国产午夜福利精品久久2021 | 久久精品免费全国观看国产| 久久影院综合精品| 久久久这里有精品中文字幕| 看久久久久久a级毛片| 久久精品国产亚洲沈樵| 亚洲午夜福利精品久久| 国产精品美女久久久| 久久久久久伊人高潮影院| 国产巨作麻豆欧美亚洲综合久久 | 精品无码久久久久久久动漫| 人人妻久久人人澡人人爽人人精品| 国产亚洲欧美成人久久片 |