poj1094 拓撲排序
http://poj.org/problem?id=1094
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.
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.
/**
poj 1094 拓撲排序
題目大意:
對於N個大寫字母,給定它們的一些偏序關係,要求判斷出經過多少個偏序關係之後可以確定它們的排序或者存在衝突,
或者所有的偏序關係用上之後依舊無法確定唯一的排序。
解題思路:(來自網上)
利用拓撲排序即可解決這個問題,但由於題目要求的是經過多少個關係之後就可以確定答案,因此每讀入一個關係,
就要進行一次拓撲排序,如果某一次拓撲排序之後可以確定它們的唯一排序或者發現衝突存在,則後面的直接略過。
若讀入所有關係之後依然無法確定唯一關係,同時也沒有衝突,則輸出不能確定唯一排序。若拓撲排序的過程中每次
僅有一個點入度為0,則該排序是可以確定的排序,否則若多個點入度為0,則不知道哪個點先哪個點後。若拓撲排序
進行到某一步之後無法再繼續,則說明存在迴路,此時說明存在衝突。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=30;
int head[maxn],ip,indegree[maxn];
int n,m,seq[maxn];
struct note
{
int v,next;
} edge[maxn*maxn];
void init()
{
memset(head,-1,sizeof(head));
ip=0;
}
void addedge(int u,int v)
{
edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}
int topo()///拓撲,可做模板
{
queue<int>q;
int indeg[maxn];
for(int i=0; i<n; i++)
{
indeg[i]=indegree[i];
if(indeg[i]==0)
q.push(i);
}
int k=0;
bool res=false;
while(!q.empty())
{
if(q.size()!=1)res=true;
int u=q.front();
q.pop();
seq[k++]=u;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
indeg[v]--;
if(indeg[v]==0)
q.push(v);
}
}
if(k<n)return -1;///存在有向環或者圖不連通,總之不能進行拓撲排序
if(res)return 0;///可以進行拓撲排序,並且只有唯一一種方式,seq陣列即是排序完好的序列
return 1;///可以進行拓撲排序,有多種情況,seq陣列是其中一種序列
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
init();
int ans=-1,err=-1;
memset(indegree,0,sizeof(indegree));
for(int i=0; i<m; i++)
{
char s[5];
scanf("%s",s);
if(ans!=-1||err!=-1)
continue;
int u=s[0]-'A';
int v=s[2]-'A';
addedge(u,v);
indegree[v]++;
int res=topo();
if(res==1)
ans=i+1;
else if(res==-1)
err=i+1;
}
if (ans != -1)
{
printf("Sorted sequence determined after %d relations: ", ans);
for (int i = 0; i < n; ++i)
putchar('A' + seq[i]);
printf(".\n");
}
else if (err != -1)
{
printf("Inconsistency found after %d relations.\n", err);
}
else
{
printf("Sorted sequence cannot be determined.\n");
}
}
return 0;
}
相關文章
- 拓撲排序排序
- 拓撲排序,YYDS排序
- 筆記:拓撲排序筆記排序
- 拓撲排序小結排序
- 圖論——拓撲排序圖論排序
- 【筆記/模板】拓撲排序筆記排序
- DFS實現拓撲排序排序
- AOV網與拓撲排序排序
- Reward (圖論+拓撲排序)圖論排序
- 拓撲排序就這麼回事排序
- 演算法-圖論-拓撲排序演算法圖論排序
- 有向圖的拓撲排序——DFS排序
- (set+拓撲排序) CF1572A Book排序
- 圖解拓撲排序+程式碼實現圖解排序
- 拓撲排序 (BFS )DAG (有向無環圖)排序
- 圖的拓撲排序詳解與實現排序
- 【Tarjan 拓撲排序 dp】P3387 【模板】縮點排序
- VOL.2 拓撲排序與關鍵路徑排序
- 拓撲排序詳解(梅開二度之dfs版按字典序輸出拓撲路徑+dfs版輸出全部拓撲路徑排序
- Noc拓撲
- 牛客 51011 可達性統計(拓撲排序,bitset)排序
- 洛谷P3953 逛公園(dp 拓撲排序)排序
- 【BZOJ-1565】植物大戰殭屍 拓撲排序 + 最小割排序
- Day2 尤拉路,拓撲排序和差分約束排序
- Leetcode 1691. 堆疊長方體的最大高度(拓撲排序 + DP)LeetCode排序
- 網路拓撲結構
- StratoVirt 的 vCPU 拓撲(SMP)
- BZOJ2535: [Noi2010]Plane 航空管制2(拓撲排序 貪心)排序
- Android程式設計師會遇到的演算法(part 7 拓撲排序)Android程式設計師演算法排序
- 網路拓撲圖:網路拓撲圖介紹及線上製作
- 演算法資料結構 | 圖論基礎演算法——拓撲排序演算法資料結構圖論排序
- 藍橋杯 卡勒沃夫之弱水路三千(提高型) 拓撲排序+Map排序
- 樹的拓撲序計數
- 拓撲序的三種功能
- tidb拓撲查詢工具qtidbTiDBQT
- Istio全景監控與拓撲
- 拓撲AC NOIP模擬賽2
- 網路拓撲—FTP服務搭建FTP
- 商企網路拓撲的搭建