题解报告: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,再判断是不是存在唯一的 ...
随机推荐
- 【BZOJ1070】修车(费用流)
题意:同一时刻有N位车主带着他们的爱车来到了汽车维修中心. 维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的. 现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待 ...
- Google Protocol Buffer 的使用(二)
一.protobuf应用场景 protobuf 在Java中的应用场景可以是序列化和反序列化,流可以通过文件或者通过网络TCP/UDP等方式传输.新建一个.proto文件 syntax = " ...
- T1003 电话连线 codevs
http://codevs.cn/problem/1003/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市 ...
- 利用Python爬虫实现百度网盘自动化添加资源
事情的起因是这样的,由于我想找几部经典电影欣赏欣赏,于是便向某老司机寻求资源(我备注了需要正规视频,绝对不是他想的那种资源),然后他丢给了我一个视频资源网站,说是比较有名的视频资源网站.我信以为真,便 ...
- epoll 的accept , read, write
http://www.ccvita.com/515.html 在一个非阻塞(fcntl)的socket上调用read/write函数, 返回EAGAIN或者EWOULDBLOCK(注: EAGAIN就 ...
- Windows 2008 R2 SP1部署WSUS 3.0 SP2
1 实验环境 1)域: 域名为fengxja.com: 网段:192.168.0网段,不连接外网. 域功能级别和林功能级别为Windows server 2003模式. 2)DC服务器: 域控制器: ...
- C#如何发布项目 发布软件
如下图所示,我随便搞了一个小软件,为了测试还在Debug目录下放了一个一副图片和一个LOGO 直接点击生成-发布 "软件名称",然后点击完成 结果报错说没找到SignToo ...
- Binary Tree Maximum Path Sum 自底向上求解(重重重重)
题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,假设左右子树最大路径和小于0.那么返回零. 用这个最大路径和和根节点的值相加.来更新最大值,同一 ...
- Java实现二叉排序树的插入、查找、删除
import java.util.Random; /** * 二叉排序树(又称二叉查找树) * (1)能够是一颗空树 * (2)若左子树不空,则左子树上全部的结点的值均小于她的根节点的值 * (3)若 ...
- iOS单例设计模式具体解说(单例设计模式不断完好的过程)
在iOS中有非常多的设计模式,有一本书<Elements of Reusable Object-Oriented Software>(中文名字为<设计模式>)讲述了23种软件设 ...