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 ...
随机推荐
- VPS虚拟专用服务器
目录 0x00 VPS服务器概述 0x01 VPS工作原理 0x02 VPS用途 0x03 VPS优势 0x04 VPS特点 0x00 VPS服务器概述 VPS服务器(虚拟专用服务器)(" ...
- PHP reset
1.函数的作用:重置数组内部指针,并返回第一个元素 2.函数的参数: @param array $array 3. 例子一: <?php $arr1 = []; $arr2 = [false, ...
- std::shared_future/future
std::future提供了一种访问异步操作结果的机制.
- boost::multi_index 多索引容器
#include "stdafx.h" #include <string> #include <boost/multi_index_container.hpp&g ...
- [Abp vNext 源码分析] - 12. 后台作业与后台工作者
一.简要说明 文章信息: 基于的 ABP vNext 版本:1.0.0 创作日期:2019 年 10 月 24 日晚 更新日期:暂无 ABP vNext 提供了后台工作者和后台作业的支持,基本实现与原 ...
- jenkins中使用变量
查看jenkins内置变量: 1.新建一个job: 2.构建-增加构建步骤-执行shell: 3.点击 可用的环境变量列表 即可查看 如WORKSPACE : 作为工作空间分配给构建目录的绝对路径 ...
- ESP8266开发之旅 网络篇⑯ 无线更新——OTA固件更新
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- 学习了解Shiro框架
有关Shiro安全框架 实现权限的几种方式 1)通过表来实现 2)shiro框架 3)Spring Security框架 shiro有哪些主要功能 1.授权 访问控制的过程,即确定谁有权访问 2.身份 ...
- C#控件的简单应用
listview 创建columns: ImageList imgList = new ImageList(); imgList.ImageSize = , ); FaceListview.Small ...
- MySQL、Oracle、SqlServer的区别
鉴于和数据库打交道日益频繁,遂决定写一篇关于Oracle.SqlServer.MySQL区别的个人观点. MySQL是大学时的主要学习对象,但刚参加工作时转到了SqlServer,现在主要接触的是Or ...