• <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 閱讀(438) 評論(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久久久国产精免费| 少妇被又大又粗又爽毛片久久黑人| 久久久SS麻豆欧美国产日韩| 97久久超碰国产精品2021| 久久精品国产精品亚洲| 久久99亚洲综合精品首页| 亚洲精品NV久久久久久久久久 | 99久久国产宗和精品1上映| 久久久久亚洲精品天堂| 性做久久久久久久久老女人| 午夜精品久久久久久99热| 国产免费福利体检区久久| 国内精品久久久久影院薰衣草 | 久久久久久久久久免免费精品| 亚洲熟妇无码另类久久久| 久久国产免费直播| 久久99国产精一区二区三区| 亚洲精品乱码久久久久久自慰| 久久99精品久久久久久不卡| 99久久综合狠狠综合久久止| 7777久久久国产精品消防器材| 久久久久亚洲AV无码专区网站 | 一本一道久久综合狠狠老| 久久久精品国产亚洲成人满18免费网站 | 一极黄色视频久久网站| 欧美粉嫩小泬久久久久久久| 国产高清美女一级a毛片久久w| 成人久久综合网| 国产99精品久久| 996久久国产精品线观看| 精品一区二区久久| 久久精品嫩草影院| 久久伊人精品青青草原高清| 国产精品福利一区二区久久| 国内精品久久久久久久涩爱| 久久精品国产国产精品四凭| 最新久久免费视频|