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 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
随机推荐
- WPF中多线程统计拆箱装箱和泛型的运行效率
WPF中多线程统计拆箱装箱和泛型的执行效率.使用的知识点有泛型.多线程.托付.从样例中能够看到使用泛型的效率至少提升2倍 MainWindow.xaml <Window x:Class=&quo ...
- lucene构建restful风格的简单搜索引擎服务
来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...
- Java 实现适配器(Adapter)模式
平时我们会常常碰到这种情况,有了两个现成的类,它们之间没有什么联系.可是我们如今既想用当中一个类的方法.同一时候也想用另外一个类的方法.有一个解决方法是.改动它们各自的接口.可是这是我们最不愿意看到的 ...
- 应用Spring和Hibernate(C3P0数据池)写数据库交互项目
一.xml的配置文件(application.xml) <?xml version="1.0" encoding="UTF-8"?> <bea ...
- Xshell dns tunnel攻击
该域名还会向多个超长域名做渗出,且域名采用了 DGA 生成算法,通过 DNS 解析时渗出数据. 部分生成域名如下: sajajlyoogrmkjlkmosbxowcrmwlvajdkbtbjoylyp ...
- [NOI.AC 2018NOIP模拟赛 第三场 ] 染色 解题报告 (DP)
题目链接:http://noi.ac/contest/12/problem/37 题目: 小W收到了一张纸带,纸带上有 n个位置.现在他想把这个纸带染色,他一共有 m 种颜色,每个位置都可以染任意颜色 ...
- 关于JQuery中的事件冒泡
什么是事件冒泡? 事件冒泡就是当父元素和子元素存在同一事件时在子元素的事件处理程序中会自动调用其父级元素的事件处理程序. demo: <!DOCTYPE html> <html xm ...
- 51nod 1562 玻璃切割 (STL map+一点点的思考)
1562 玻璃切割 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 现在有一块玻璃,是长方形的(w 毫米× h 毫米),现在要 ...
- jQuery学习(一)——jQuery入门
1.jQuery基础 Jquery它是一个库(框架),要想使用它,必须先引入! jquery-1.8.3.js:一般用于学习阶段. jquery-1.8.3.min.js:用于项目使用阶段 官网下载后 ...
- hiho1041 - 树,遍历
题目链接 给一棵树,给一个序列,问能不能按这个序列遍历这棵树,满足每条边最多经过两次. -------------------------------------------------------- ...