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


            May the force be with you!
            posts - 52,  comments - 33,  trackbacks - 0
                很久沒(méi)有在blog上寫解題報(bào)告了。。。前一陣子都在看書,看算導(dǎo),看中大的書。。。差點(diǎn)被認(rèn)為是沒(méi)有在做ACM了。。。怪我自己。
                在中大的書上看到一段寫的不錯(cuò)的toplogical sort的代碼,用它來(lái)過(guò)了一直沒(méi)過(guò)的1094,后來(lái)看到kid的代碼,發(fā)現(xiàn)他寫麻煩了。其實(shí)判環(huán)不用那么麻煩,在拓?fù)渑判蚶锞涂梢耘协h(huán)了。值得注意的是,每次刪點(diǎn)時(shí)都要判斷是否存在兩個(gè)或兩個(gè)以上的入度為0的點(diǎn),因?yàn)檫@樣是不能determin的。如果沒(méi)有這一步就會(huì)wa,比如如下數(shù)據(jù):
            4 5
            A<B
            C<D
            B<D
            A<C
            B<D
            若沒(méi)有那么做的話,會(huì)在輸出
            Sorted sequence determined after 4 relations:ACBD.
            但是實(shí)際上在第四步還沒(méi)有確定。

            代碼比kid的短,呵呵。
                                                    ——Simbaforrest

            代碼如下:
            #include<iostream>
            #include
            <cstring>
            using namespace std;
            int v,e;
            int deg[26];
            int sortAns[26],anstop;
            bool adj[
            26][26];

            int TopSort()
            {
              
            int ret=1;
              bool sure
            =true;
              
            //init
              memset(deg,0,sizeof(deg));
              
            //count degree
              for(int i=0; i<v; i++)
              {
                
            for(int j=0; j<v; j++)
                {
                  
            if(adj[i][j]==true)
                    
            ++deg[j];
                }
              }
              
            //make stack of 0 degree vertex
              int top=-1;
              
            int cnt=0;
              
            for(int i=0; i<v; i++)
              {
                
            if(deg[i]==0)
                {
                  
            ++cnt;
                  deg[i]
            =top;
                  top
            =i;
                }
              }
              
            if(cnt==0)
              {
                sure
            =true;
                
            return ret=-1;//cycle exist
              }
              
            if(cnt>1)
              {
                sure
            =false;
                ret
            =0;//unsure
              }//go on to see if cycle exist
              
              
            //sort
              anstop=-1;
              
            for(int i=0; i<v; i++)
              {
                
            if(top==-1)
                {
                  
            //cycle exist
                  sure=true;
                  
            return ret = -1;
                }
                
            else
                {
                  
            //find j as the 0 degree vertex
                  int j = top;
                  top 
            = deg[top];
                  sortAns[
            ++anstop]=j;//record the ans
                  
                  
            //delete the vertex j
                  cnt=0;
                  
            for(int ni=0; ni<v; ni++)
                  {
                    
            if(adj[j][ni]==true)
                    {
                      
            --deg[ni];
                      
            //a new 0 degree vertex
                      if(deg[ni]==0)
                      {
                        
            ++cnt;
                        deg[ni]
            =top;
                        top 
            = ni;
                      }
                    }
                  }
                  
            if(cnt>1)
                  {
                    sure
            =false;
                    ret
            =0;//unsure
                  }//go on to see if cycle exist
                }
              }
              
            if(sure==false)
                
            return ret=0;
              
            return ret;
            }

            int main()
            {
              
            while(cin>>v>>e,!(v==0&&e==0))
              {
                
            //init
                memset(adj,0,sizeof(adj));
                bool sure
            =false;
                
                
            //read and solve
                for(int i=0; i<e; i++)
                {
                  
            char left,op,right;
                  cin
            >>left>>op>>right;
                  adj[left
            -'A'][right-'A']=1;
                  
                  
            if(sure)
                    
            continue;
                    
                  
            int ret=TopSort();
                  
            if(ret!=0)
                  {
                    sure
            =true;
                    
            if(ret==1)
                    {
                      cout
            <<"Sorted sequence determined after "<<i+1<<" relations: ";
                      
            for(int j=0; j<=anstop; j++)
                        cout
            <<(char)(sortAns[j]+'A');
                      cout
            <<"."<<endl;
                    }
                    
            else if(ret==-1)
                    {
                      cout
            <<"Inconsistency found after "<<i+1<<" relations."<<endl;
                    }
                  }
                }
                
            if(sure==false)
                {
                  cout
            <<"Sorted sequence cannot be determined."<<endl;
                }
              }
              
            return 0;
            }



            posted on 2008-05-15 12:13 R2 閱讀(1450) 評(píng)論(3)  編輯 收藏 引用 所屬分類: Problem Solving

            FeedBack:
            # re: 再談Toplogical sort(pku 1094)
            2009-01-25 01:23 | 風(fēng)之傷
            配上注釋后,相當(dāng)清晰。謝謝大大~~  回復(fù)  更多評(píng)論
              
            # re: 再談Toplogical sort(pku 1094)
            2010-03-06 11:04 | Cinderella
            請(qǐng)問(wèn)一下 中大的書是哪本書  回復(fù)  更多評(píng)論
              
            # re: 再談Toplogical sort(pku 1094)
            2010-03-06 13:09 | R2
            @Cinderella
            中山大學(xué)郭嵩山老師的《國(guó)際大學(xué)生程序設(shè)計(jì)競(jìng)賽例題解》  回復(fù)  更多評(píng)論
              
            你是第 free hit counter 位訪客




            <2007年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            常用鏈接

            留言簿(4)

            隨筆分類(54)

            隨筆檔案(52)

            文章檔案(1)

            ACM/ICPC

            技術(shù)綜合

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 63689
            • 排名 - 358

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            伊人久久大香线蕉亚洲五月天| 久久精品国产精品亚洲艾草网美妙 | 91精品国产91热久久久久福利 | 久久乐国产精品亚洲综合| 久久久久国产| 亚洲国产精品成人久久| 久久综合丁香激情久久| 亚洲精品无码久久久| 97久久久久人妻精品专区| 欧美激情精品久久久久久久| 99久久综合国产精品免费| 久久99精品久久只有精品| 久久久这里有精品中文字幕| 国产精品女同久久久久电影院| 日批日出水久久亚洲精品tv| 久久久久AV综合网成人| 久久精品国产WWW456C0M| 国产亚洲精品自在久久| 久久成人小视频| 99久久伊人精品综合观看| 青草国产精品久久久久久| 内射无码专区久久亚洲| 国产精品一久久香蕉国产线看| 亚洲欧美精品一区久久中文字幕| 国产午夜精品久久久久免费视| 久久中文字幕人妻丝袜| 亚洲午夜久久影院| 91精品国产乱码久久久久久| 久久精品国产久精国产果冻传媒| 美女久久久久久| 久久久久国产| 麻豆久久| 要久久爱在线免费观看| 亚洲精品tv久久久久| 久久毛片免费看一区二区三区| 国产精品99久久久久久董美香| 日韩亚洲欧美久久久www综合网 | 精品久久人人爽天天玩人人妻| 亚洲国产香蕉人人爽成AV片久久| 久久国产香蕉一区精品| 午夜精品久久影院蜜桃|