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 ...
随机推荐
- PHPExcel导出导入
便于记忆 这里写一个PHPexcel导出的demo 我们构造一个数据 $letter = array('A','B','D','E'); //sheet索引 $date = array( ar ...
- POJ 2029 Palindromes _easy version
#include<cstdio> #include<cstring> using namespace std; int main() { int n; ]; scanf(&qu ...
- vc++ windows 创建桌面快捷方式
创建桌面快捷方式 在windows软件开发中,软件安装过程中总是需要在桌面创建快捷方式,下面介绍一种创建桌面快捷方式的方法,具体代码如下: /* * 创建快捷方式 * szExePath[in]:要创 ...
- java 并发runable,callable,future,futureTask
转载自:http://www.cnblogs.com/dolphin0520/p/3949310.html package future_call; import java.util.concurre ...
- [Err] 1067 - Invalid default value for 'xxxTime'
下面是导入sql脚本的的局部脚本 `xxxTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 发现是NO_ZERO_IN_DATE,NO_Z ...
- 洛谷 1155 (NOIp2008)双栈排序——仔细分析不合法的条件
题目:https://www.luogu.org/problemnew/show/P1155 这道题教会我们要多思考. 好好分析过后发现同一个栈里不能有升序.就用它写了一个30分. #include& ...
- 升级3.4成3.6 ubuntu14.04 和miniconda虚拟环境
打开Anaconda Prompt窗口conda update conda 先升级conda激活要升级python的虚拟环境conda install python=3.6.6 再升级pythonco ...
- SQL万能密码:' or 1='1
select name,pass from tbAdmin where name='admin' and pass='123456' 输入用户名:' or 1='1 SQL变成下面这个样子: sele ...
- java代码----equals和==区别
总结: ==的意义在于比较的是整型 package com.aa; // public class Bd { public static void main(String[] args) { Inte ...
- java里的switch循环--你妹考试落榜了
总结:switch循环,不用break.那么程序每一个case都会运行到,直到遇到break停止 package com.aa; //格子区域 //3行3列的格子 public class Bu { ...