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 ...
随机推荐
- Farming
Problem Description You have a big farm, and you want to grow vegetables in it. You're too lazy to s ...
- Course Schedule 解答
Question There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...
- Best Time to Buy and Sell Stock III 解答
Question Say you have an array for which the ith element is the price of a given stock on day i. Des ...
- tomcat配置访问日志,访问首页主目录
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" ...
- MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
- Zookeeper 1、Zookeeper 定义与工作原理
1.什么是Zookeeper » Zookeeper 是 Google 的 Chubby一个开源的实现,是 Hadoop 的分布式协调服务 » 它包含一个简单的原语集,分布式应用程序可以基于它实现同步 ...
- flappy bird游戏源代码揭秘和下载
转:http://blog.csdn.net/touchsnow/article/details/19071961 背景: 最近火爆全球的游戏flappy bird让笔者叹为观止,于是花了一天的时间山 ...
- 格而知之5:我所理解的Run Loop
1.什么是Run Loop? (1).Run Loop是线程的一项基础配备,它的主要作用是来让某一条线程在有任务的时候工作.没有任务的时候休眠. (2).线程和 Run Loop 之间的关系是一一对应 ...
- java的wait和notifyAll使用方法
class Num { private int num; public int getNum() { return num; } public void setNum(int num) { this. ...
- SQL Server 两种判断表名是否存在且删除的方式
邓老师(老邓)教的 if exists(select * from sysobjects where name='Table_88') drop table Table_88 偷的((*^__^ ...