麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

POJ 1094 Sorting It All Out (拓?fù)渑判颍?/h1>
2019-11-10 20:09:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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.

題意

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

思路

依舊是一道拓?fù)渑判虻念}目。

無(wú)法唯一確定一個(gè)序列當(dāng)且僅當(dāng)在所有關(guān)系輸入完畢之后,進(jìn)行拓?fù)渑判虻臅r(shí)候存在一個(gè)以上入度為0的點(diǎn)。

序列不存在會(huì)在圖中存在環(huán)的情況下出現(xiàn)。

于是,每輸入一組關(guān)系,進(jìn)行一次拓?fù)渑判颍瑢?duì)此次拓?fù)渑判虻慕Y(jié)果判斷并做出相應(yīng)的選擇。

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 入度數(shù)組 queue<int>sk; for(int i=0; i<n; i++) //入度為0的點(diǎn)壓入隊(duì)列 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的點(diǎn)同時(shí)存在一個(gè)以上,說(shuō)明無(wú)法唯一確定序列 res=0; for(int i=0; i<(int)G[p].size(); i++) //消除當(dāng)前點(diǎn),臨界點(diǎn)入度-1 { int j=G[p][i]; if(--h[j]==0) sk.push(j); } } if(top<n)res=-1; //圖中存在環(huán) 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(); //拓?fù)渑判? 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;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

主站蜘蛛池模板: 日本s级毛片免费观看 | 国内免费视频成人精品 | 特级黄色一级毛片 | 欧美国产成人在线 | free japan xxxxhdsex69 | 国产乱淫av | 久久国产精品成人免费网站 | 久久精品一区二区三区国产主播 | 欧美日韩成人一区二区 | 精品一区二区三区免费看 | www国产成人免费观看视频,深夜成人网 | 精品国产一区二区三区久久久 | 精品欧美一区二区精品久久 | 婷婷久久久久久 | 狠狠色成色综合网 | 麻豆视频免费网站 | 国产精品一区二区三区在线看 | 青青操精品 | 麻豆91精品91久久久 | 免费国产视频大全入口 | 国内精品久久久久久久久久久久 | 成人午夜在线免费 | 欧美偷拍一区二区 | 毛片一级免费看 | 草草视频免费观看 | av噜噜在线| 毛片118极品美女写真 | arabxxxxvideos| 精品国产一区二区三区在线观看 | www.69色| xxxx18韩国护士hd老师 | 黄色大片在线观看 | 看片一区 | 国产成人精品区 | 毛片电影在线看 | 成人一级毛片 | 免费久久久 | 91精品国产777在线观看 | 国产午夜免费不卡精品理论片 | 狠狠干五月天 | 四季久久免费一区二区三区四区 |