poj 1094 / zoj 1060 Sorting It All Out
Description 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 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.
Output 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. 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 Sample Output Sorted sequence determined after 4 relations: ABCD. Source |
[Submit] [Go Back] [Status] [Discuss]
解题思路:这题是一个拓扑排序问题,不过过程比较复杂,要处理的情况也比较多!首先题目条件和要求要很清楚,这样分析问题思路就会清晰!
解题代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
struct Node{ //与之相连的节点结构体
int to;
struct Node *next;
};
Node *Link[]; //链表保存连接关系
Node *tm_node;
bool vis[];
int count[], tm_count[]; //保存节点入度数据,前者为备份数据,后者为运算数据
int n, m, num;
char deal[];
const int A = 'A';
int result[], pos; //保存结果 int TopSort(){
int i, j, cnt;
bool flag = false;
pos = cnt = ;// cnt为入度为0的节点数,pos为保存结果的下标
j = -; //入度为0的字母
for (i = ; i < n; i ++) //寻找入度为0的位置
if(tm_count[i] == && vis[i]){
cnt ++;
j = i;
}
while (~j){
if(cnt > ) //如果cnt > 1 则表示有多个入度为0的节点,也就是说可能无法排序
flag = true; //不直接return是因为还有可能图中有环,导致数据矛盾
for (tm_node = Link[j]; tm_node != NULL; tm_node = tm_node ->next)
tm_count[tm_node ->to] --;
result[pos ++] = j;
tm_count[j] --;
j = -;
cnt = ;
for (i = ; i < n; i ++)
if(tm_count[i] == && vis[i]){
cnt ++;
j = i;
}
}
if(!flag && pos != num) // 图中有环,数据矛盾
return -;
if(!flag && pos == num) //图中为一个有序序列,是否是最终结果还需进一步判断
return ;
if(flag && pos == num) //图中有多个有序序列,还需进一步判断
return ;
if(flag && pos != num) //图中有环,数据矛盾
return -;
} void init(){
num = ;
memset(vis, , sizeof(vis));
memset(Link, , sizeof(Link));
memset(count, , sizeof(count));
memset(tm_count, , sizeof(tm_count));
} void input(){
int d1, d2;
scanf("%s", deal);
d1 = deal[] - A;
d2 = deal[] - A;
tm_node = new Node;
tm_node ->next = NULL;
tm_node ->to = d2;
count[d2] ++;
if(Link[d1] == NULL)
Link[d1] = tm_node;
else{
tm_node ->next = Link[d1];
Link[d1] = tm_node;
}
if(vis[d1] == ){
num ++;
vis[d1] = true;
}
if(vis[d2] == ){
num ++;
vis[d2] = true;
}
} int main(){
int i, j;
int value;
bool had_ans;
while(~scanf("%d%d", &n, &m) && (n && m)){
init(); //初始化
had_ans = false; //是否已经得出结果
for(i = ; i <= m; i ++){
if(had_ans){ //如果已有结果,则无须处理后续数据
scanf("%s", deal);
continue;
}
input(); //数据处理
for(j = ; j < n; j ++)
tm_count[j] = count[j];
value = TopSort(); //拓扑排序
if (value == -){
printf ("Inconsistency found after %d relations.\n", i);
had_ans = true;
}
else if(value == && num == n){ //数据量以满足要求,且能排序
printf ("Sorted sequence determined after %d relations: ", i);
for (j = ; j < n; j ++)
printf ("%c", result[j] + A);
printf(".\n");
had_ans = true;
}
}
if (value == || (value == && n != num)) // 无法排序
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
poj 1094 / zoj 1060 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 1562 && ZOJ 1709 Oil Deposits(简单DFS)
题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...
- POJ 3076 / ZOJ 3122 Sudoku(DLX)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)
水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...
- POJ 1094 (传递闭包 + 拓扑排序)
题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- poj 1094 Sorting It All Out(图论)
http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...
- poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
随机推荐
- Hive编程指南_学习笔记01
第四章: HQl的数据定义 1:创建数据库 create database financials; create database if not exists financials; 2: ...
- 51nod-1253: Kundu and Tree
[传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...
- nyoj--138--找球号(二)(hash+邻接表)
找球号(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=1 ...
- VMware虚拟机ubuntu显示屏幕太小解决办法
使用VMware安装的ubuntu虚拟机的显示屏幕太小,可以通过在VMware里安装"VMware Tool"插件解决,安装步骤记录一下. 1. 更改ISO文件路径 安装VMwar ...
- CentOS Linux 加硬盘,分区和设置自动挂载
sda 表示第1块SCSI硬盘hda 表示第1块IDE硬盘(即连接在第1个IDE接口的Master口上)scd0 表示第1个USB光驱当添加了新硬盘后,在/dev目录下会有相应的设备文件产生.ccis ...
- html单行、多行文本溢出隐藏
欢迎加入前端交流群来py:749539640 单行: div{/* 单行溢出隐藏 */ width: 150px; white-space: nowrap; overflow: hidden; tex ...
- Gulp 相关
获取执行在文件列表: http://www.thinksaas.cn/ask/question/21950/ 用through2这个插件. var through = require('through ...
- ZOJ 3321 Circle【并查集】
解题思路:给定n个点,m条边,判断是否构成一个环 注意到构成一个环,所有点的度数为2,即一个点只有两条边与之相连,再有就是判断合并之后这n个点是否在同一个连通块 Circle Time Limit: ...
- [Java] Protect, Private and Public的区别
Java中的private.protected.public和default的区别 (2014-03-21 22:29:14) 转载▼ 标签: java java修饰符 it (1)对于publi ...
- pthread_join/pthread_exit的用法解析
官方说法: 函数pthread_join用来等待一个线程的结束.函数原型为: extern int pthread_join __P ((pthread_t __th, void **__thread ...