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

            POJ 1094 Sorting It All Out 拓?fù)渑判?/a>

            Description

            An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

            Input

            Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

            Output

            For each problem instance, output consists of one line. This line should be one of the following three:

            Sorted sequence determined after xxx relations: yyy...y.
            Sorted sequence cannot be determined.
            Inconsistency found after xxx relations.

            where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.

            Sample Input

            4 6
            A<B
            A<C
            B<C
            C<D
            B<D
            A<B
            3 2
            A<B
            B<A
            26 1
            A<Z
            0 0
            

            Sample Output

            Sorted sequence determined after 4 relations: ABCD.
            Inconsistency found after 2 relations.
            Sorted sequence cannot be determined.

            Source

                拓?fù)渑判蛩惴ǎ?nbsp;         1.將所有入度為0的點(diǎn)加入隊(duì)列;
                                            2.彈出隊(duì)首元素u,輸出u并將所有與u關(guān)聯(lián)的頂點(diǎn)v的入度減1;如果v的入度為0,將u加入隊(duì)列;
                                            3.重復(fù)第2步,如果所有的頂點(diǎn)都被訪問(wèn)到,則輸出序列是一個(gè)拓?fù)渑判颍环駝t該DAG圖中存在環(huán)路。
            #include <iostream>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <queue>
            using namespace std;

            int n,m;
            vector
            <int> top;
            vector
            <int> in;
            vector
            < vector<int> > map;

            int topsort(){
                
            int i,u;
                
            bool flag=false;
                queue
            <int> q;
                vector
            <int> d(in.begin(),in.end());
                
            for(i=0;i<n;i++)
                    
            if(!d[i]) q.push(i);
                top.clear();
                
            while(!q.empty()){
                    
            if(q.size()!=1) flag=true;
                    u
            =q.front();
                    q.pop();
                    top.push_back(u);
                    
            for(i=0;i<map[u].size();i++)
                        
            if(--d[map[u][i]]==0) q.push(map[u][i]);
                }

                
            if(top.size()!=n) return 1;
                
            if(flag) return 0;
                
            return 2;
            }

            int main(){
                
            string str;
                
            int i,j,u,v,ans;
                
            while(cin>>n>>m,n||m){
                    
            in.assign(n,0);
                    map.assign(n,vector
            <int>());
                    
            for(ans=i=0;i<&& !ans;i++){
                        cin
            >>str;
                        u
            =str[0]-'A',v=str[2]-'A';
                        
            if(find(map[u].begin(),map[u].end(),v)==map[u].end())
                            map[u].push_back(v),
            in[v]++;
                        ans
            =topsort();
                    }

                    
            for(j=i;j<m;j++) cin>>str;
                    
            switch(ans){
                        
            case 0:cout<<"Sorted sequence cannot be determined."<<endl;break;
                        
            case 1:cout<<"Inconsistency found after "<<i<<" relations."<<endl;break;
                        
            case 2:{
                            cout
            <<"Sorted sequence determined after "<<i<<" relations: ";
                            
            for(j=0;j<n;j++) cout<<char('A'+top[j]);
                            cout
            <<"."<<endl;
                            
            break;
                               }

                    }

                }

                
            return 0;
            }

            posted on 2009-05-19 20:43 極限定律 閱讀(627) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年5月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国产成人久久精品激情| 日韩欧美亚洲国产精品字幕久久久 | 国产精品久久久久久久人人看| 成人精品一区二区久久| 国内精品九九久久久精品| 久久精品人人做人人爽电影蜜月| 波多野结衣AV无码久久一区| 久久久久青草线蕉综合超碰| 香蕉99久久国产综合精品宅男自| 久久久久久噜噜精品免费直播| 欧美一级久久久久久久大| 亚洲国产日韩欧美综合久久| 亚洲伊人久久综合影院| 亚洲精品无码成人片久久| 久久发布国产伦子伦精品| 精品久久久久久亚洲| 精品国产乱码久久久久久浪潮| 久久AⅤ人妻少妇嫩草影院| 伊人色综合久久天天人守人婷| 久久精品aⅴ无码中文字字幕不卡| 国产精品成人久久久| 99久久精品国产高清一区二区| 99久久精品国产综合一区| 久久亚洲视频| 国产亚洲色婷婷久久99精品| 94久久国产乱子伦精品免费| 国产成人综合久久精品红| 2020久久精品国产免费| 亚洲国产成人久久一区久久| 亚洲愉拍99热成人精品热久久 | 亚洲精品无码久久久久久| 国内精品久久久久影院免费| 久久人妻少妇嫩草AV无码蜜桃| 亚洲色欲久久久综合网东京热| 国产精品久久久久久久久久免费| 久久人人爽人人爽人人av东京热| 久久精品国产精品青草app| 99久久做夜夜爱天天做精品| 国产99久久九九精品无码| 久久久无码精品亚洲日韩按摩| 日本加勒比久久精品|