国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 1094 Sorting It All Out (拓撲排序)

2019-11-10 20:09:59
字體:
來源:轉載
供稿:網友

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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0

Sample Output

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

題意

依序給出一些字母之間的大小關系,判斷并輸出它能否唯一確定一個序列滿足這一關系。

思路

依舊是一道拓撲排序的題目。

無法唯一確定一個序列當且僅當在所有關系輸入完畢之后,進行拓撲排序的時候存在一個以上入度為0的點。

序列不存在會在圖中存在環的情況下出現。

于是,每輸入一組關系,進行一次拓撲排序,對此次拓撲排序的結果判斷并做出相應的選擇。

AC 代碼

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>#include<queue>#include<set>using namespace std;#define M 30vector<int>G[M];int in[M],n,m;char ans[M];int solve(){ int res=1,h[M],top=0; memcpy(h,in,sizeof(h)); //copy 入度數組 queue<int>sk; for(int i=0; i<n; i++) //入度為0的點壓入隊列 if(h[i]==0) sk.push(i); while(!sk.empty()) { int p=sk.front(); sk.pop(); ans[top++]=p+'A'; if(sk.size()>0) //如果入度為0的點同時存在一個以上,說明無法唯一確定序列 res=0; for(int i=0; i<(int)G[p].size(); i++) //消除當前點,臨界點入度-1 { int j=G[p][i]; if(--h[j]==0) sk.push(j); } } if(top<n)res=-1; //圖中存在環 ans[top]=0; return res;}int main(){ while(~scanf("%d%d%*c",&n,&m)&&(n||m)) { char a,b; int flag=0; memset(in,0,sizeof(in)); for(int i=0; i<M; i++) G[i].clear(); for(int i=0; i<m; i++) { scanf("%c%*c%c%*c",&a,&b); if(flag)continue; a-='A'; b-='A'; G[(int)a].push_back((int)b); ++in[(int)b]; //入度 flag=solve(); //拓撲排序 if(flag==1) printf("Sorted sequence determined after %d relations: %s./n",i+1,ans); if(flag==-1) printf("Inconsistency found after %d relations./n",i+1); } if(!flag) printf("Sorted sequence cannot be determined./n"); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会昌县| 江门市| 海安县| 五寨县| 台前县| 陆良县| 西吉县| 西昌市| 永吉县| 剑阁县| 买车| 宜兴市| 红河县| 泰安市| 绥德县| 前郭尔| 行唐县| 东丰县| 岳普湖县| 灵石县| 无棣县| 社旗县| 无棣县| 怀来县| 游戏| 正安县| 留坝县| 襄樊市| 临夏市| 醴陵市| 兰坪| 临颍县| 华蓥市| 阜阳市| 永善县| 涿鹿县| 新乐市| 墨竹工卡县| 增城市| 蒙山县| 密山市|