题意:是否唯一确定顺序,根据情况输出

#include <iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define N 30
int n,m;
int map[N][N],indegree[N],list[N];
int toposort(int n){
int in[N],flag,t,num;
memcpy(in,indegree,sizeof(indegree));//复制入度数组,以免对主函数中的indegree有影响
stack<int> s;
int i;
for(i=0;i<n;i++)
if(!in[i])
s.push(i);//所有入度为0的点入栈,如果这些点多于1的话,序列不确定
num=0;
flag=0;
while(!s.empty()){
if(s.size()>1)
flag=1; //序列不确定
t=s.top();
s.pop();
list[num++]=t; //记录出栈的数字
for(i=0;i<n;i++)
if(map[t][i])
if(--in[i]==0)
s.push(i); //入度为0的点入栈
}
if(num!=n)//不能拓扑排序,即有环
return 1;
else if(flag==1)//有多种排序方式,不能唯一确定
return 2;
return 0;//序列能够被唯一确定
}
int main(int argc, char** argv) {
int determined,inconsistency;
int i,j,res;
// freopen("in.txt","r",stdin);
char a,b;
while(scanf("%d%d",&n,&m)&&n||m){
getchar();
determined=0;
inconsistency=0;
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
for(i=1;i<=m;i++){
scanf("%c<%c",&a,&b);
getchar();
if(!determined&&!inconsistency){
if(map[b-'A'][a-'A']==1){
inconsistency=1;
printf("Inconsistency found after %d relations.\n",i);
continue;
}
if(map[a-'A'][b-'A']==0){
map[a-'A'][b-'A']=1;
indegree[b-'A']++;
}
res=toposort(n);
if(res==0){
printf("Sorted sequence determined after %d relations: ",i);
for(j=0;j<n;j++)
printf("%c",list[j]+'A');
printf(".\n");
determined=1;
}
else if(res==1){
inconsistency=1;
printf("Inconsistency found after %d relations.\n",i);
}
}
}
if(!determined&&!inconsistency)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}

poj 1094 Sorting It All Out_拓扑排序的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  4. POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  5. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  6. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  7. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

  8. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  9. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 92 ...

随机推荐

  1. Axure快捷键大全

    基本快捷键: 打开:Ctrl + O 新建:Ctrl + N 保存:Ctrl + S 退出:Alt + F4 打印:Ctrl + P 查找:Ctrl + F 替换:Ctrl + H 复制:Ctrl + ...

  2. Windows多线程同步系列之二-----关键区

    关键区对象为:CRITICAL_SECTION 当某个线程进入关键区之后,其他线程将阻塞等待,知道该线程释放关键区的拥有权. 关键区同步主要有以下几个API 初始化关键区对象,无返回值,传入一个关键区 ...

  3. 2.7 Structured Regression Models

    $RSS(f)=\sum_i^N \left(y_i-f(x_i)\right)^2$ 当数据量足够大时,数据存在相同$x_i$,不同$y_{il},l=1\cdots t$ 则得到的f即为条件均值$ ...

  4. iOS Get方式带中文不能请求网络

    今天发现一个蛋疼的问题,使用ASIHTTPRequest Get方式请求数据时候带中文,iOS客户端不能正确进行网络请求. NSURL *url = [NSURL URLWithString:@htt ...

  5. crm使用soap更改下拉框的文本值

    //C#代码 //UpdateStateValueRequest updateStateValue = new UpdateStateValueRequest //{ //    AttributeL ...

  6. 用JSmooth制作java jar文件的可运行exe文件教程【图文】

    这是我之前在个人博客3yj上面写的一篇文章,如今转载过来,原文地址 (这不是广告哦) 几年前,刚接触java的是,就想用一些方法把自己的劳动果实保护起来,曾经也用过非常多这种工具,有一个特别好用,今天 ...

  7. NTP-ntpdate:no server suitable for synchronization found

    NTP-ntpdate 问题处理 解决ntp的错误 no server suitable for synchronization found 当用ntpdate -d 来查询时会发现导致 no ser ...

  8. 安装Oracle11后在SQL Developer启动时提示:enter the full pathname for the java.exe

    1) Open the file ..\sqldeveloper\sqldeveloper\bin\sqldeveloper.conf and add the following line to se ...

  9. winform —— 常用控件

    1.textbox: 属性:text:    文本selectedtext:         获或设置选中文本canundo:         是否能够撤销 passwordchar:替换字符实现密码 ...

  10. 实现接口IDisposable的示例

    想使用using(...), 如: using (Getter process = new Getter()) { //... } 就必须给类实现接口IDisposable public sealed ...