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

            poj2513

            Colored Sticks

            Time Limit: 5000MS Memory Limit: 128000K
            Total Submissions: 24238 Accepted: 6381

            Description

            You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

            Input

            Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

            Output

            If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

            Sample Input

            blue red
            red violet
            cyan blue
            blue magenta
            magenta cyan
            

            Sample Output

            Possible

            Hint

            Huge input,scanf is recommended.

            早起一水
            囧呆了
            給很多木棍,兩端有顏色,問能不能是這些木棍首尾相接,并且保證相連接處顏色相同
            一筆畫問題,判斷歐拉路
            統(tǒng)計節(jié)點的度數(shù),奇點個數(shù)為0或2,存在歐拉路
            但這里是單詞作為點,所以要判斷
            怎么判斷呢,好多方法,hash,trie,map(目前還不會)
            這里我顯然用了trie這段時間學(xué)了這個,練下
            判歐拉圖,還是是連通的才可以,所以得判連通,用并查集即可
            或者麻煩的建圖,dfs一遍

            father數(shù)組忘記初始化了,wa一次
            改后忘記注視freopen了,wa一次
            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #include 
            <iomanip>
            using namespace std;
            int cnt;
            struct node
            {
                
            int next[26];
                
            int count;
                
            int key;
                
            void init()
                
            {
                    memset(next,
            -1,sizeof(next));
                    count
            =0;
                }

            }
             s[6000005];
            int father[250005],count1[250005],sind;
            void cas_init()
            {
                s[
            0].init();
                sind
            =1;
            }

            int ins(char str[])
            {
                
            int len=strlen(str);
                
            int i,j,ind;
                ind
            =0;
                
            for(i=0; i<len; i++)
                
            {
                    j
            =str[i]-'a';
                    
            if(s[ind].next[j]==-1)
                    
            {
                        s[sind].init();
                        s[ind].next[j]
            =sind++;
                    }

                    ind
            =s[ind].next[j];
                }

                
            if(s[ind].count==0)
                
            {
                    s[ind].key
            =cnt++;
                }

                s[ind].count
            ++;
                
            return s[ind].key;
            }

            int find(int x)
            {
                
            if(x==father[x]) return x;
                
            else
                
            {
                    father[x]
            =find(father[x]);
                    
            return father[x];
                }

            }

            void un(int a,int b)
            {
                father[find(a)]
            =find(father[b]);
            }

            int main()
            {
                
            int ca,cb;
                
            char str1[21],str2[21];
                cas_init();
                cnt
            =0;
                memset(count1,
            0,sizeof(count1));
                
            for(int i=0; i<250005; i++) father[i]=i;
                
            //freopen("in1.txt","r+",stdin);
                while(scanf("%s%s",str1,str2)!=EOF)
                
            {
                    
            //puts(str1);puts(str2);
                    ca=ins(str1);
                    cb
            =ins(str2);
                    
            //cout<<ca<<" "<<cb<<endl;
                    count1[ca]++;
                    count1[cb]
            ++;
                    un(ca,cb);
                }

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

                
            else
                
            {
                    
            int tmp;
                    tmp
            =0;
                    
            for(int i=0; i<cnt; i++)
                        
            if(count1[i]%2==1)
                            tmp
            ++;//奇點個數(shù)
                    ca=find(0);
                    
            for(int i=1; i<cnt; i++)
                        
            if(ca!=find(i))
                        
            {
                            tmp
            =-1;
                            
            break;
                        }

                    
            //printf("%d\n",cnt);
                    if(tmp==0||tmp==2)
                        printf(
            "Possible\n");
                    
            else printf("Impossible\n");
                }

                
            //fclose(stdin);
                return 0;
            }



            posted on 2012-07-17 08:13 jh818012 閱讀(212) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內(nèi)容較長,點擊標題查看
            • --王私江
            久久久久一级精品亚洲国产成人综合AV区 | 91视频国产91久久久| 综合网日日天干夜夜久久| 亚洲国产欧洲综合997久久| 久久99热只有频精品8| 99久久精品国产毛片| 怡红院日本一道日本久久 | 午夜精品久久久久久影视777| 综合久久给合久久狠狠狠97色 | 国产精品免费久久| 97久久国产综合精品女不卡| 日本一区精品久久久久影院| 久久综合亚洲色一区二区三区| 99国产欧美精品久久久蜜芽| 亚洲人成网站999久久久综合| 99国产精品久久| 久久国产精品无码HDAV| 久久人人爽人人人人片av| 久久久久久免费一区二区三区| 中文字幕久久久久人妻| 久久精品18| 欧美激情精品久久久久| 欧洲精品久久久av无码电影 | 久久人搡人人玩人妻精品首页| 久久精品蜜芽亚洲国产AV| 欧洲国产伦久久久久久久| 91精品国产91久久久久久蜜臀| 久久精品亚洲日本波多野结衣| 久久精品国产亚洲AV久| 欧洲国产伦久久久久久久| 久久久WWW成人| 色综合久久中文综合网| 国产AV影片久久久久久| 大伊人青草狠狠久久| 国内精品人妻无码久久久影院| 久久综合给久久狠狠97色| 欧美午夜精品久久久久免费视| 亚洲精品乱码久久久久久按摩| 97久久国产综合精品女不卡| 波多野结衣AV无码久久一区| 伊人久久综合精品无码AV专区|