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 ...
随机推荐
- F110 参数保存和重新运行录屏
**初始界面回车 PERFORM frm_dynpro USING ' 'X'. PERFORM frm_dynpro USING '' 'BDC_CURSOR' 'F110V-LAUFD'. PER ...
- cut命令学习
cut最基本的用法: -f 列号:提取第几列 -d 分隔符:按照指定分隔符分割列(默认是制表符tab) 测试用例:(制表符)
- phoneGap实现离线缓存
引自:http://www.iteye.com/topic/1128173 正在做一个跨平台的应用,需要使用phonegap进行文件的一些基本操作. 需求如下:可以选择本地图片,或者从相机选择图片,并 ...
- Binary Search in Java
关于折半查找中的几个注意点. Version 1: public static <T extends Comparable<? super T>> int binSearch( ...
- SpringMVC:学习笔记(6)——转换器和格式化
转换器和格式化 说明 SpringMVC的数据绑定并非没有限制,有案例表明,在SpringMVC如何正确绑定数据方面是杂乱无章的,比如在处理日期映射到Date对象上. 为了能够让SpringMVC进行 ...
- 联合文件系统 unionfs
- win10系统修改Intel VT-x时进入不了BIOS问题
一般电脑进入BIOS的方式都是在开机的时候不停的按F2或者F12,但是Win10系统由于支持快速启动,当win10系统快速启动的时候,按F12或者F2是没反应的,解决方式: 第一步:修改win10的启 ...
- 建议42:使用pandas处理大型CSV文件
# -*- coding:utf-8 -*- ''' CSV 常用API 1)reader(csvfile[, dialect='excel'][, fmtparam]),主要用于CSV 文件的读取, ...
- 80211n标准建链速率计算
转:https://wenku.baidu.com/view/93f99dd3ad51f01dc281f1af.html 转:http://www.docin.com/p-1851128644.htm ...
- Django---Blog系统开发之注册页面(验证码&ajax发送文件)
前端页面及渲染: 静态文件的配置:setting.py: static 文件放在app下 STATIC_URL = '/static/' STATIC_ROOT = ( os.path.join(BA ...