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

#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. codecomb 2092【课程选择】

    题目描述 大学选课总是烦恼着很多人.现在X同学选出了很多备选课,但是有的课程之间是有时间冲突的.X不会分身,自然无法在同一个时间上不同的课.每个课可能有很多备选时间,但是每个课只需要选一个时间上就可以 ...

  2. MongoDB appendix

    mongo 是数据库shell.一般假定它和mongod 运行在同一台机器上,还假定mongod 绑定了默认端口. eg.  mongo  staging.example.com:20000,这样就会 ...

  3. Python 面向对象基础

    面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机 ...

  4. [Java Web]Struts2解决中文乱码问题

    1.设置struts的字符编码,能够在struts.xml中添加下面代码: <constant name="struts.i18n.encoding" value=" ...

  5. Java算法简介及排序剖析

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 从小白晋升,一路走来:从helloworld,到JFrame,再到Android:从城外小子,到内城 ...

  6. 使用CSS达到阴阳八卦图等图形

    CSS还是比較强大的,能够实现中国古典的"阴阳八卦图"等形状. 正方形 #rectangle { width: 200px; height: 100px; backgrount-c ...

  7. sqlplus常用操作命令2

    常用编辑命令:A[ppend] text 将text附加到当前行之后C[hange] /old /new 将当前行中的old替换为newCLear] buff[er] 清除缓冲区中的所有行DEL 删除 ...

  8. gauge.js的应用

    最近项目要做个手机端的仪表盘,但是画风太给力,echarts.highcharts.D3等等都不能满足业务的需求,你懂的!开找,找到个gauge.js 下面简单介绍下这个插件官网http://bern ...

  9. 导出Eclipse环境配置

    第一种方法: Eclipse的 File -> Export(导出), 在窗口中展开 General(常规) -> Perferences(首选项)-->Export all(全部导 ...

  10. 【转载自友盟消息推送iOS文档】在appDelegate中注册推送

    1.2   基本功能集成指南 提示 请先在友盟的消息推送管理后台中创建App,获得AppKey和AppSecret 导入SDK 下载 UMessage_Sdk_All_x.x.x.zip并解压缩 导入 ...