nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out
- 描述
-
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
- 输入
- Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.
- 输出
- For each problem instance, output consists of one line. This line should be one of the following three:
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.
- 样例输入
-
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 - 样例输出
-
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined./**
拓扑排序实现步骤:
①、在有向图中找到没有前驱节点的顶点并输出
②、删除该点和该点所到的下一个点之间的线
③、重复①、②操作
**//**
分析:
Ⅰ、数据 n 表示有 n 个字母, 且字母由A开始到 char('A' + n - 1)
Ⅱ、 数据 m 表示有 m 个输入, 用来判断是否能够确定前 n 个字符的关系
Ⅲ、如果确定其前 n 个字母的先后顺序就输出,同时对后面的数据不做判断
Ⅳ、如果能够确定有环的存在也可以不用对后面的数据进行判断 (输出 Inconsistency...)
**/核心代码:
int topo_sort () {
int flag = , temp [], c = , Q[], in_num, pos;
for (int i = ; i <= n; ++ i) {
temp [i] = my_in [i];
}
for (int i = ; i <= n; ++ i) {
in_num = ;
for (int j = ; j <= n; ++ j) {
if (!temp [j]) {
pos = j;
in_num ++;
}
}
if (!in_num) return ; // 环
if (in_num > ) flag = -; // 不可能有序,但有可能有环,所以不能return Q [c ++] = pos;
temp [pos] = -;
for (int j = ; j <= n; ++ j) {
if (my_map [pos][j] == ) {
temp [j] --;
}
}
}
return flag;
}C/C++代码实现(AC):
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring> using namespace std; int n, m, my_map [][], my_in [], Q[], c; int topo_sort () {
int flag = , temp [], num_in, pos;
c = ;
for (int i = ; i <= n; ++ i) {
temp [i] = my_in [i];
} for (int i = ; i <= n; ++ i) {
num_in = ;
for (int j = ; j <= n; ++ j) {
if (!temp [j]) {
++ num_in;
pos = j;
}
}
if (!num_in) return ; // 环
if (num_in > ) flag = -; // 不能确定 temp [pos] = -;
Q [c ++] = pos;
for (int j = ; j <= n; ++ j) {
if (my_map [pos][j] == ) {
temp [j] --;
}
}
}
return flag;
} int main () {
while (scanf ("%d%d", &n, &m), n != || m != ) {
int flag = , a1, b1;
char a, b, d;
memset (my_map, , sizeof (my_map));
memset (my_in, , sizeof (my_in)); for (int i = ; i <= m; ++ i) {
getchar ();
scanf ("%c%c%c", &a, &d, &b);
if (!flag) continue; a1 = int (a - 'A' + );
b1 = int (b - 'A' + );
my_map [a1][b1] = ;
my_in [b1] ++; int x = topo_sort ();
if (x == ) {
printf("Sorted sequence determined after %d relations: ",i);
for (int i = ; i < c; ++ i) {
printf ("%c", 'A' + Q [i] - );
}
printf (".\n");
flag = ;
} else if (x == ) {
printf("Inconsistency found after %d relations.\n",i);
flag = ;
}
} if (flag) {
printf("Sorted sequence cannot be determined.\n");
}
}
}
nyoj 349 (poj 1094) (拓扑排序)的更多相关文章
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- Sorting It All Out POJ - 1094 拓扑排序
题意:给N个字母,和M个偏序关系 求一个可确定的全序,可确定是指没有其他的可能例如A>B D>B 那么有ADB DAB两种,这就是不可确定的其中,M个偏序关系可以看做是一个一个按时间给出的 ...
- POJ 1094 拓扑排序
Description: 规定对于一个只有大写字母的字符串是有大小顺序的.如ABCD.即A<B.B<C.C<D.那么问题来了.现在第一行给你n, m代表序列里只会出现前n的 ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- poj 3249 拓扑排序 and 动态规划
思路:我们首先来一遍拓扑排序,将点按先后顺序排列于一维数组中,然后扫描一遍数组,将每个点的出边所连接的点进行更新,即可得到最优解. #include<iostream> #include& ...
- poj 2585 拓扑排序
这题主要在于建图.对9个2*2的小块,第i块如果出现了不等于i的数字,那么一定是在i之后被brought的.可以从i到该数字建一条边. 图建好后,进行一次拓扑排序,判段是否存在环.若存在环,那么就是B ...
- Poj(3687),拓扑排序,
题目链接:http://poj.org/problem?id=3687 题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号 ...
- POJ 1128 拓扑排序 + 深搜
/* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对2 ...
随机推荐
- PHP array_unique
1.函数的作用:移除数组中重复的值 2.函数的参数: @params array $array @params int $sort_flag SORT_REGULAR : 通常方法比较(不改变类型) ...
- 关于seaJs合并压缩(gulp-seajs-combine )路径与文件ID匹配问题。
前段时间和有大家介绍过用 gulp-seajs-combine 来打包seaJs文件.大家会发现合并seaJs一个很奇怪的现象,那就是它的 ID和路径匹配原则.使得有些文件已经合并过去了,但还是会提示 ...
- [LUOGU1122] 最大子树和 - 树形动规
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- python中list切片详解
语法:[start:stop:step] step代表切片步长:切片区间为[start,stop),包含start但不包含stop 1.step > 0,从左往右切片 2.step <0, ...
- Linux用到的常用命令
Linux常用命令
- 浅谈线段树 Segment Tree
众所周知,线段树是algo中很重要的一项! 一.简介 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 使用线段树可以快速的查找某一个节点在 ...
- Web登录中的信心安全问题
1. 一个简单的HTML例子看看用户信息安全 标准的HTML语法中,支持在form表单中使用<input></input>标签来创建一个HTTP提交的属性,现代的WEB登录中, ...
- 从Go语言编码角度解释实现简易区块链——实现交易
在公链基础上实现区块链交易 区块链的目的,是能够安全可靠的存储交易,比如我们常见的比特币的交易,这里我们会以比特币为例实现区块链上的通用交易.上一节用简单的数据结构完成了区块链的公链,本节在此基础上对 ...
- (乱入)FingerGesture
偶然的机会遇到FingerGesture插件,此插件也有很多方便的功能,比如控制主相机查看模型以及缩放等功能,如Component-FingerGestures-Toolbox-Camera-Orbi ...
- SpringBoot系列之YAML配置用法
1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...