POJ--1094--Sorting It All Out||NYOJ--349--Sorting It All Out(拓扑排序)
NYOJ的数据水一点,POJ过了是真的过了
/*
拓扑排序模板题:
每次输入都要判断有环与有序的情况,如果存在环路或者已经有序可以输出则跳过下面的输入
判断有序,通过是否在一个以上的入度为0的点,存在则不能有序排列
判断有环,如果拓扑排序完成存在一个有序的排列, 证明无环路
主要判断
1.有序
2.有环
在无序的境况下,优先判断是否有环
有序的情况下,优先判断是否能输出
*/ #include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
const int maxn = ;
vector<int> g[maxn];
int du[maxn], n, m, L[maxn];
int topsort() {
memset(du, , sizeof(du));
int flag = ;
for (int i=; i<n; i++) {
for (int j=; j<g[i].size(); j++) {
du[g[i][j]]++;
}
}
int tot = ;
int ct = ;
queue<int> Q;
for (int i=; i<n; i++) {
if (!du[i]) {
Q.push(i);
ct++;
}
}
if (ct > ) flag = -;//无序
while (!Q.empty()) {
ct = ;
int x = Q.front();
Q.pop();
L[tot++] = x;
for (int j=; j<g[x].size(); j++) {
int t = g[x][j];
du[t]--;
if (!du[t]) {
Q.push(t);
ct ++;
}
}
if (ct > ) flag = -;//无序
}
if (flag == -) {//无序情况下优先判断是否冲突
if (tot != n) return ;//有环
else return -;
}
if (tot == n) return ;
return ;//有环
}
int main() { while (cin>>n>>m && (m||n)) {
int flag = ;
memset(g, , sizeof(g));
for (int i=; i<m; i++) {
string str;
cin>>str;
if (flag) continue;
int a = str[] - 'A';
int b = str[] - 'A';
g[a].push_back(b);
int ans = topsort();
if (ans == ) {//有序
cout<<"Sorted sequence determined after "<<i+<<" relations: ";
for (int i=; i<n; i++) {
cout<<char(L[i]+'A');
}
cout<<"."<<endl;
flag = ;
}
if (ans == ) {//环 ,冲突
cout<<"Inconsistency found after "<<i+<<" relations."<<endl;
flag = ;
}
}
if (!flag) {//无序
cout<<"Sorted sequence cannot be determined."<<endl;
}
}
return ;
}
POJ--1094--Sorting It All Out||NYOJ--349--Sorting It All Out(拓扑排序)的更多相关文章
- NYOJ 349 Sorting It All Out (拓扑排序 )
题目链接 描述 An ascending sorted sequence of distinct values is one in which some form of a less-than ope ...
- poj 1270(dfs+拓扑排序)
题目链接:http://poj.org/problem?id=1270 思路:就是一简单的dfs+拓扑排序,然后就是按字典序输出所有的情况. http://paste.ubuntu.com/59872 ...
- poj 1094 Sorting It All Out(nyoj 349)
点击打开链接 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24544 Accep ...
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- 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(图论)
http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...
- poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
随机推荐
- id函数
描述 id() 函数用于获取对象的内存地址. 语法 id 语法: id([object]) 参数说明: object -- 对象. 返回值 返回对象的内存地址. 实例 以下实例展示了 id 的使用方法 ...
- try catch 事务不会滚
在spring机制中,在配置事务后,如果采用try catch 捕获异常后,因为异常已经被捕获,所以事务不会滚,从而产生许多脏数据.解决办法: 1.在catch中抛出异常,(throw new Run ...
- css系列(4)简介
本节用来简单介绍css. (1)css: CSS 指层叠样式表 (Cascading Style Sheets): 功能以及作用:1.样式定义如何显示 HTML 元素; 2.样式通常存 ...
- nginx负载均衡详情
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- copy deepcopy辨析
copy deepcopy讲的是复制源对象的改变对copy出来的对象的影响: 我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在. 所以改变原有被复制对象不会对已经复制 ...
- Python 循环的综合应用
# 循环综合应用1. # str = "hello,world" 把字符串给反转显示 str = "hello,world" temp = "&quo ...
- SQL题
1.取出sql表中第31到40的记录(以自动增长ID为主键) sql server方案: select top 10 * from t where id not in (select top 30 i ...
- SpringCloud-路由网关(Zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.只能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统. 在Spring Cloud ...
- 简学Python第五章__模块介绍,常用内置模块
Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群 群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...
- QT 学习记录:渐变-QLinearGradient,QRadialGradient,QConicalGradient)
http://blog.csdn.net/wangwei890702/article/details/8552482 QT:渐变 渐变,是指逐渐的,有规律性的变化,是一种规律性很强的现象.Qt提供了一 ...