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

            Ural 1080 Map Colouring

            1080. Map Colouring

            Time Limit: 1.0 second
            Memory Limit: 16 MB
            We consider a geographical map with N countries numbered from 1 to N (0 < N < 99). For every country we know the numbers of other countries which are connected with its border. From every country we can reach to any other one, eventually crossing some borders. Write a program which determines whether it is possible to colour the map only in two colours — red and blue in such a way that if two countries are connected their colours are different. The colour of the first country is red. Your program must output one possible colouring for the other countries, or show, that such colouring is impossible.

            Input

            On the first line is written the number N. On the following N lines, the i-th line contains the countries to which the i-th country is connected. Every integer on this line is bigger than i, except the last one which is 0 and marks that no more countries are listed for country i. If a line contains 0, that means that the i-th country is not connected to any other country, which number is larger than i.

            Output

            The output contains exactly one line. If the colouring is possible, this line must contain a list of zeros and ones, without any separators between them. The i-th digit in this sequence is the colour of the i-th country. 0 corresponds to red colour, and one — to blue colour. If a colouring is not possible, output the integer −1.

            Sample

            input output
            3
                                    2 0
                                    3 0
                                    0
                                    
            010
                                    

            DFS:或BFS,或者并查集(不會(huì)用)
            這里用的DFS,用一個(gè)標(biāo)記數(shù)組,沒進(jìn)入一個(gè)聯(lián)通分圖后,標(biāo)記為0(表示一種顏色)與它相連的標(biāo)記為1(另一種顏色),
            然后與1相連的在標(biāo)記為0。 這里的標(biāo)記都是對(duì)沒有標(biāo)記過的進(jìn)行的,如果是標(biāo)記過的,就要檢查他們的標(biāo)記是否相同
            如果相同則說明,他們同色。
            //ural 1080
            #include<iostream>
            using namespace std;

            const int MAX=100;
            bool adj[MAX][MAX];
            int flg[MAX];
            int n;
            bool isPossible=true;

            void input()
            {
                 cin
            >>n;
                 
            int temp;
                 
            for(int i=1; i<=n; i++)
                 {
                         
            while(cin>>temp,temp!=0)
                         {
                                                 adj[i][temp]
            =adj[temp][i]=true;
                         }
                 }
            }

            void dfs(int i)
            {
                 
            if(isPossible==false)return ;
                 
            if(flg[i]==-1)flg[i]=0;
                 
            for(int j=1; j<=n; j++)
                 {
                         
            if(adj[i][j])
                         {
                                      
            if(flg[j]==-1){ flg[j]=flg[i]==0? 1:0;  dfs(j); } 
                                      
            else if(flg[j]==flg[i])isPossible=false;
                                      
                         }
                 }
                 
            }

            int main()
            {
                memset(adj,
            0,sizeof adj);
                
                input();
                
            for(int i=1; i<=n; i++
                       flg[i]
            =-1;  
                
                flg[
            1]=0;
                
            for(int i=1; i<=n; i++)
                        dfs(i);
                
                
            if(isPossible==false)cout<<-1<<endl;
                
            else { 
                     
            for(int k=1; k<=n; k++)
                         cout
            <<flg[k];
                     cout
            <<endl;
                       }
                         
                
                system(
            "pause");
                
            return 0;
            }

            posted on 2010-07-31 17:17 田兵 閱讀(301) 評(píng)論(0)  編輯 收藏 引用 所屬分類: URAL

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            久久久久久精品久久久久| 久久久亚洲欧洲日产国码是AV| 精品国产91久久久久久久| 久久精品九九亚洲精品天堂 | 久久精品国产亚洲AV忘忧草18| 久久久无码精品亚洲日韩蜜臀浪潮| 69久久夜色精品国产69 | 囯产精品久久久久久久久蜜桃 | 欧美性猛交xxxx免费看久久久| 亚洲va久久久噜噜噜久久男同| 精品国产91久久久久久久| 久久99这里只有精品国产| 亚洲精品高清久久| 久久夜色精品国产亚洲| 久久精品国产亚洲av瑜伽| 久久精品a亚洲国产v高清不卡| 久久亚洲日韩看片无码| 久久激情五月丁香伊人| 69久久夜色精品国产69| 国产精品久久午夜夜伦鲁鲁| 亚洲AV无码久久精品成人 | 性高湖久久久久久久久AAAAA| 久久99精品国产| 欧洲人妻丰满av无码久久不卡| 国产精品久久久久久五月尺| 久久五月精品中文字幕| 国产999精品久久久久久| 97久久精品无码一区二区天美| 国内精品久久久久久久久电影网 | 伊色综合久久之综合久久| 精品久久综合1区2区3区激情| 欧美日韩中文字幕久久伊人| 久久精品成人免费网站| 99久久综合狠狠综合久久止| 人人狠狠综合久久亚洲婷婷| 久久精品国产网红主播| 久久天天躁狠狠躁夜夜avapp| 国内精品久久久久影院一蜜桃| 国产精品久久久久影院色| 久久精品国产亚洲麻豆| 久久本道综合久久伊人|