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 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
随机推荐
- u-boot学习(六):自己写bootloader
依照前面分析的u-boot的启动流程,自己写一个简单的Bootloader.这是參考韦东山老师的视频写的. 1.初始化硬件:关看门狗.设置时钟.设置SDRAM.初始化NAND Flash 2.假设Bo ...
- Oracle 实现 mysql 更新 update limit
oracle给人的感觉非常落后.使用非常不方便,Toad 这个软件又笨又迟钝.pl/sql更是,90年代的界面风格,速度还卡得要死.并且oracle不支持limit .by default7#zbph ...
- Android程序全然退出的三种方法
1. Dalvik VM的本地方法 android.os.Process.killProcess(android.os.Process.myPid()) //获取PID,眼下获取自己的也仅仅有该 ...
- C#上传文件
QQ:1187362408 欢迎技术交流和学习 关于C#上传文件(产品开发): TODO: 1.文件大小不足500M(web.config配置直接处理) 2,文件大小超过500M(ASP.NET分段读 ...
- [JZOJ 5908] [NOIP2018模拟10.16] 开荒(kaihuang)解题报告 (树状数组+思维)
题目链接: https://jzoj.net/senior/#contest/show/2529/1 题目: 题目背景:尊者神高达作为一个萌新,在升级路上死亡无数次后被一只大黄叽带回了师门.他加入师门 ...
- 15-11-24 system同步与异步
//打开关闭qq #define _CRT_SECURE_N0_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #i ...
- 我网站用session做的登录,为什么清除浏览器数据后还是得重新登录?session是存在服务器上的。
答案一: 你清除了浏览器数据,相当于把cookie也清了,那么你的sessionId也就没有了,所以你再次请求的时候服务器无法根据你携带的sessionid来获取对应的session,所以说需要重新登 ...
- CentOS 安装 PHP7
下载地址:http://php.net/downloads.php 上传目录:/usr/local/src 安装目录:/usr/local/php ## 参考资料 PHP官网: http://php. ...
- Glidar测试安装
在上一篇随笔中,我们完成了对Glidar 仿真器的概念层面的认识.接下来,我们将着手对该该仿真器进行安装测试. 1 依赖库的安装 安装环境为Windows 7 64位+Ubuntu14.04 LTS的 ...
- h5 input失去焦点软键盘把页面顶起
var broswer=localStorage.getItem('temp') //浏览器环境 var u = navigator.userAgent var isiOS = !!u.match(/ ...