poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出
#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_拓扑排序的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [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 ...
- 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 ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
随机推荐
- cf479C Exams
C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_Tournament CDOJ 124
A sports company is planning to advertise in a tournament. It is a single round-robin tournament, th ...
- Course Schedule 解答
Question There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...
- ios的一些开源资源
1. http://www.open-open.com/lib/view/open1428646127375.html vim插件:https://github.com/Valloric/YouCom ...
- 小KING教你做android项目(一)
写在项目开始之前: 最近关于android的视频教程,入门的书籍也已经有很多了,例如我的入门就是看了mars的视频教程.但是这么一圈学习下来,觉得真正快速提高的,不是在看视频,而是在实际工作中动手做项 ...
- asp数组的使用
定义简单数组 有两种方法在asp中定义和初始化数组,让我们看看每种的例子: 方法一:MyArray = Array("Jan","Feb","Mar& ...
- django 启动和请求
Django运行方式 调试模式 直接 python manage.py runserver python manage.py runserver python manage.py runserver ...
- Linux使用技巧9--用dpkg管理你的软件
dpkg(package manager for Debian): debian体系中的包管理工具. Commands: -i|--install <.deb file name> ... ...
- Android系统匿名共享内存(Anonymous Shared Memory)C++调用接口分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6939890 在Android系统中,针对移动设 ...