题解报告: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,再判断是不是存在唯一的 ...
随机推荐
- input range音乐进度条
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 快速幂取模模板 && 51nod 1013 3的幂的和
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...
- Servlet CDI 例子分析
@WebServlet("/cdiservlet") //以@WebServlet注释开头,注释指定相对于上下文根的URL模式,即在根目录下使用/cdiservlet来访问 pub ...
- dubbo的泛化调用研究
结论: 泛化调用需要继承一个类,在配置文件里需要明确指出generic=true; 泛化调用在书写provider代码时,变化不大: 泛化调用和普通调用的区别主要在consumer,从‘调用’的表面意 ...
- WIN10中使用Hyper-V 配置虚拟机宿主机互ping
在Windows10 Hyper-V 中安装 Linux (Ubuntu16.04)虚拟机无法 ping 通宿主机,宿主机可以ping通虚拟机. 这种情况下关闭 Windows 防火墙就能ping通 ...
- Android多线程研究(3)——线程同步和相互排斥及死锁
为什么会有线程同步的概念呢?为什么要同步?什么是线程同步?先看一段代码: package com.maso.test; public class ThreadTest2 implements Runn ...
- System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。
错误背景: 操作系统:编程环境:VS2013. 语言:VB.net: 数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...
- Hadoop HDFS NFS GateWay部署深入具体解释
目的:通过挂载的方式,能够相似訪问本地磁盘的方式一样的訪问Hadoop文件.简单.方便.快捷. 0.系统版本号&hadoop版本号 1)系统版本号 [root@WEB-W031 sbin]# ...
- Android架构的简单探讨(一)
在CSDN上看到这样一篇译文,虽然最终的解决方案要按照自己特定的项目去设计,但该文还是引起了很多自己的共鸣,原文猛戳这里. 这是他提出的基于Messaging的MVC框架: 其中包含的设计思想在于:哪 ...
- oracle type类型
转载 http://blog.sina.com.cn/s/blog_6cfb6b090100ve92.html 转自网络,具体用法我会再细化 1.概念 方法:是在对象类型说明中用关键字 MEM ...