题解报告: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,再判断是不是存在唯一的 ...
随机推荐
- codevs1792 分解质因数
题目描述 Description 编写一个把整数N分解为质因数乘积的程序. 输入描述 Input Description 输入一个整数 N 输出描述 Output Description 输出 分解质 ...
- ***CSS总结-原创
CSS规则 第一个是:选择符.和jquery的选择器是一个意思大括号是:申明块里面KV是:属性和属性值分号是结束符 类别选择符(器) p.red的意思,就是对p标签中类为red的进行CSS作用 ID选 ...
- 易碎的鸟蛋 概率DP
1007: 易碎的鸟蛋 时间限制: 1 Sec 内存限制: 128 MB提交: 396 解决: 80[提交][状态][讨论版] 题目描述 你们知道吗?西电的跳楼塔上面有一个鸟巢.某SXBK的教授对 ...
- Codeforces 628D Magic Numbers
题意: 求在[a,b](a,b不含前导0)中的d−magic数中有多少个是m的倍数. 分析: 计数dp Let's call a number d-magic if digit d appears i ...
- wget: unable to resolve host address “mirrors.163.com” 的解决办法
wget:无法解析主机地址.这就能看出是DNS解析的问题. 解决办法: 登入root(VPS). 进入/etc/resolv.conf. 修改内容为下nameserver 8.8.8.8 #googl ...
- Oracle SqlPlus导出查询结果
Oracle SqlPlus导出查询结果 在sqlplus下导出查询的结果保存到本地sql文件中,可以采用如下方式:1.连接数据库: sqlplus xmq/xmqpwd@192.168.1.57:1 ...
- 物理内存、虚拟内存、buffers、cached、共享内存、swap
物理内存: 实际使用的内存: 虚拟内存: 虚拟内存是操作系统内核为了对进程地址空间进行管理(process address space management)而精心设计的一个逻辑意义上的内存空间概念. ...
- Java RMI之HelloWorld程序以及相关的安全管理器的知识
Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,可以让在某个 Java 虚拟机上的对象调用还有一个 Java 虚拟机中的对象上的方法.可以用此 ...
- DG备库磁盘空间满导致无法创建归档
上周五去某客户那里做数据库巡检.是window 2008系统上10g的一套NC系统的库,已经配置了DG,可是巡检时发现数据库报错: Tue Nov 11 10:13:57 2014 LNS: Stan ...
- Jmeter技巧
Jmeter参数化的4种方法 用Jmeter测试时包含两种情况的参数,一种是在url中,一种是请求中需要发送的参数. URL中的参数,如:http://blog.da-fang.com/index.p ...