27-拓扑排序-poj1094
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 38379 | Accepted: 13534 |
Description
Input
Output
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.
Source
#include <iostream>
#include <cstring> using namespace std;
int in[30]; //入度
int out[30]; //输出序列
int map[30][30]; //有向图 int TopoSort(int n){
int ct = 0, cpin[30];
int flag = 1;
for(int i = 0; i < n; i++){
cpin[i] = in[i];
}
while(ct < n){
int p = -1, m = 0;
for(int i = 0; i < n; i++){
if(cpin[i] == 0){
p = i;
m++;
}
}
if(m == 0){
return -1; //有环
}
if(m > 1){
// return 0; //无法判断
flag = 0; //得出不能判断只有还有检测,因为可能后面可能有环
}
out[ct++] = p;
cpin[p] = -1;
for(int i = 0; i < n; i++){
if(map[p][i]){
cpin[i]--;
}
}
}
return flag; //排好序了
} int main(){
std::ios::sync_with_stdio(false);
int n, m;
char x, y;
char ch;
while(cin >> n >> m && (n != 0 && m != 0)){
memset(in, 0, sizeof(in));
memset(map, 0, sizeof(map));
int flag = 1;
for(int i = 0; i < m; i++){
cin >> x >> ch >> y;
if(flag == 0)
continue; //注意就算前面已经判断出结果,也要输入完,但不需要判断了
if(map[x - 'A'][y - 'A'] == 0){
map[x - 'A'][y - 'A'] = 1;
in[y - 'A']++;
int s = TopoSort(n);
if(s == -1){
cout << "Inconsistency found after ";
cout << i + 1;
cout << " relations." << endl;
flag = 0;
}
if(s == 1){
cout << "Sorted sequence determined after ";
cout << i + 1;
cout << " relations: ";
for(int i = 0; i < n; i++){
cout << char(out[i] + 'A');
}
cout << "." << endl;
flag = 0;
}
}
}
if(flag){
cout << "Sorted sequence cannot be determined." << endl;
}
}
return 0;
}
27-拓扑排序-poj1094的更多相关文章
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- POJ1094 拓扑排序
问题:POJ1094 本题考查拓扑排序算法 拓扑排序: 1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组. 循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...
- POJ1094——拓扑排序和它的唯一性
比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径. 算法导论里面的拓扑 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- poj1094拓扑排序
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29539 Accepted: 10 ...
- POJ1094 字母排序(拓扑排序)
该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.该序列不能判断是否有序: 3.该序列字母次 ...
- [NOIP2015模拟10.27] 挑竹签 解题报告(拓扑排序)
Description 挑竹签——小时候的游戏夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏.挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签.如果动了其他的竹签,就要换对手来挑.在所有 ...
- ACM/ICPC 之 拓扑排序范例(POJ1094-POJ2585)
两道拓扑排序问题的范例,用拓扑排序解决的实质是一个单向关系问题 POJ1094(ZOJ1060)-Sortng It All Out 题意简单,但需要考虑的地方很多,因此很容易将code写繁琐了,会给 ...
- UVA196-Spreadsheet(拓扑排序)
Spreadsheet In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet applicatio ...
随机推荐
- HihoCoder 1097 Prim算法
1097 : 最小生成树一·Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 最近,小Hi很喜欢玩的一款游戏模拟城市开放出了新Mod,在这个Mod中,玩家可以 ...
- Fillder手机抓包的使用
1.Fillder下载地址: http://www.onlinedown.net/soft/73207.htm 2.网络设置 手机和电脑需链接网络相同 3.fillder设置 3.1打开fillder ...
- 把UIImage保存到照片库和沙盒中
1.保存到iOS照片库需要引入QuartzCore.framework框架,具体代码如下: .h文件 #import <QuartzCore/QuartzCore.h> UIImageVi ...
- Cocos2d-x -自己定义动作 圆周运动
原文地址:http://blog.csdn.net/u012945598/article/details/17605409 在之前的文章中我们以前讲过Cocos2d-x中的各种动作的用法,我们先来简单 ...
- compoer 全局和单个项目切换源 composer update killed
演示地址: https://blog.csdn.net/hpugym/article/details/72588393 composer update killed https://cloud.ten ...
- jinja2的一些用法
1.split用法以及乘法运算 {% set user_list=l.users.split(',') %} <tr> <td>{{ l.name }}</td> ...
- Java-Maven-Runoob:Maven NetBeans
ylbtech-Java-Maven-Runoob:Maven NetBeans 1.返回顶部 1. Maven NetBeans NetBeans 6.7 及更新的版本已经内置了 Maven.对于以 ...
- logging模块讲解
logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...
- from表单
构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: 1 2 3 4 5 <form action="/your-name/" me ...
- C#简单操作XML
类文件: class OperatorXML { /// <summary> /// 确定资源文件路径,Resource为自己创建的目录 /// </summary> priv ...