• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              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 ;//節(jié)點數(shù)目
            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 閱讀(425) 評論(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 就一數(shù)組啊  回復  更多評論
              

            久久精品国产亚洲AV不卡| 亚洲精品乱码久久久久久蜜桃图片| 国产精品久久久久久久app| 国产精品久久久久久一区二区三区| 国产精品一区二区久久精品涩爱| 久久精品国产99国产精品| 久久无码av三级| 97久久精品人人做人人爽| 97精品伊人久久大香线蕉app| 精品国产乱码久久久久久1区2区| 亚洲精品乱码久久久久久按摩| 久久婷婷五月综合97色直播| 亚洲性久久久影院| 国内精品人妻无码久久久影院导航 | 伊人色综合久久| 亚洲国产成人久久精品影视| 久久综合综合久久97色| 久久―日本道色综合久久| 久久久久亚洲AV成人网| 伊人久久亚洲综合影院| 精品人妻伦九区久久AAA片69| 久久久久久毛片免费播放| 色综合久久88色综合天天| 久久综合视频网站| 午夜精品久久久久久99热| 国产精品岛国久久久久| 久久精品亚洲精品国产欧美| 一本色道久久88—综合亚洲精品| 99re久久精品国产首页2020| 国产一区二区精品久久岳| 精品久久久久久中文字幕大豆网| 青青草原1769久久免费播放| 狠狠色丁香久久婷婷综合_中| 久久777国产线看观看精品| 久久久综合香蕉尹人综合网| 久久天天躁狠狠躁夜夜avapp| 久久精品免费网站网| 久久精品国产亚洲AV大全| 久久久综合香蕉尹人综合网| 99久久国产热无码精品免费| 国产欧美久久久精品影院|