题解报告:poj 1094 Sorting It All Out(拓扑排序)
Description
Input
Output
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.
Sample Input
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
Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
解题思路:拓扑排序裸题。做法:边读边进行拓扑排序,如果最后拓扑的个数小于n,说明图中有环,返回0,输出此时的位置i和对应的信息,下次直接跳过字符串的操作即可;如果返回值为1,说明拓扑次序唯一,直接输出对应信息和拓扑序列即可,同样下次直接跳过对字符串的操作;如果前两者都没有发生,即某次队列中顶点的入度数为0的个数不止一个,说明拓扑次序是不唯一的,输出对应的结果即可。
AC代码:
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
#include<map>
#include<cstdio>
using namespace std;
const int maxn=;
int n,m,out[maxn],InDeg[maxn];char cs[];
vector<int> vec[maxn];
queue<int> que;
int topsort(){//flag:-1次序不确定;1:次序唯一
while(!que.empty())que.pop();//清空
int cnt=,flag=,in[maxn];
for(int i=;i<n;++i)in[i]=InDeg[i];//拷贝每个节点的入度数
for(int i=;i<n;++i)
if(!in[i])que.push(i);//先将入度为0的进队
while(!que.empty()){
if(que.size()>)flag=-;//次序不确定
int now=que.front();out[cnt++]=now;que.pop();
for(size_t i=;i<vec[now].size();++i)
if(--in[vec[now][i]]==)que.push(vec[now][i]);
}
if(cnt!=n)return ;//成环
return flag;
}
int main(){
while(cin>>n>>m&&(n+m)){
getchar();
memset(InDeg,,sizeof(InDeg));
for(int i=;i<n;++i)vec[i].clear();//全部清空
int sign=;
for(int i=;i<=m;++i){
cin>>cs;
if(sign)continue;//跳过下面的操作,表示结论已出
int f1=cs[]-'A',f2=cs[]-'A';
vec[f1].push_back(f2);
InDeg[f2]++;
int k=topsort();
if(k==){//成环
printf("Inconsistency found after %d relations.\n",i);
sign=;//同时标记为1
}
if(k==){//有序
printf("Sorted sequence determined after %d relations: ",i);
for(int j=;j<n;++j)printf("%c",out[j]+'A');
printf(".\n");
sign=;
}
}
if(!sign)printf("Sorted sequence cannot be determined.\n");//最后才确定次序是否唯一
}
return ;
}
题解报告:poj 1094 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 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& ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- 题解报告:hdu 5695 Gym Class(拓扑排序)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=5695 Problem Description 众所周知,度度熊喜欢各类体育活动.今天,它终于当上了梦寐以求的体育课老 ...
- poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
- poj 1094 Sorting It All Out(图论)
http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...
随机推荐
- POJ 3518 (后缀自动机)
POJ 3518 Boring Problem : 给一个串S,询问串S有多个子串出现至少两次且位置不重叠. Solution : 对S串建立后缀自动机,再建立后缀树,dfs一遍统计处每个结点的子树中 ...
- POJ 2104 K-th Number (可持久化线段树)
题目大意 给一个长度为n的序列,有m个询问,每次询问一个区间里面第k小的数. 解题分析 静态的区间第k大.复习了一下可持久化线段树. 首先对数值离散化,建一颗权值线段树.按照序列的顺序依次插入,每一个 ...
- 从零开始写STL-容器-list
从零开始写STL-容器-list List 是STL 中的链表容器,今天我们将通过阅读和实现list源码来解决一下问题: List内部的内存结构是如何实现的? 为什么List的插入复杂度为O(1)? ...
- 洛谷——P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- springboot + mybatis 完成图片上传并保存到数据库
添加依赖 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons- ...
- Bug记载2之Vue.JS路由定义的位置
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- STL中常用的c++语法
函数调用操作(c++语法中的左右小括号)可以被重载,STL的特殊版本都以仿函数形式呈现.如果对某个class进行operator()重载,它就成为一个仿函数. 仿函数(functor),就是使一个类的 ...
- 解决fragmentTransaction.replace不能全屏
今天遇到个问题,使用fragmentTransaction.replace替换后的内容不能全屏.. FragmentManager fragmentManager = getSupportFragme ...
- Cracking the Coding Interview 150题(一)
1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...
- php高效获取数据分页
mysql.php 获取数据库中的记录,全然个人经验总结,仅供參考! <? php /** *PHP+MYSQL数据库基本功能 *http://blog.csdn.net/yown */ ### ...