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(拓扑排序)的更多相关文章

  1. NYOJ 349 Sorting It All Out (拓扑排序 )

    题目链接 描述 An ascending sorted sequence of distinct values is one in which some form of a less-than ope ...

  2. poj 1270(dfs+拓扑排序)

    题目链接:http://poj.org/problem?id=1270 思路:就是一简单的dfs+拓扑排序,然后就是按字典序输出所有的情况. http://paste.ubuntu.com/59872 ...

  3. poj 1094 Sorting It All Out(nyoj 349)

    点击打开链接 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24544   Accep ...

  4. nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

  5. nyoj 349 (poj 1094) (拓扑排序)

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

  6. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  7. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. poj 1094 Sorting It All Out(图论)

    http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...

  9. poj 1094 Sorting It All Out 解题报告

    题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...

  10. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

随机推荐

  1. Java基础教程:泛型基础

    Java基础教程:泛型基础 引入泛型 传统编写的限制: 在Java中一般的类和方法,只能使用具体的类型,要么是基本数据类型,要么是自定义类型.如果要编写可以应用于多种类型的代码,这种刻板的限制就会束缚 ...

  2. dom树改变监听

    function unwrap(el, target) { if ( !target ) { target = el.parentNode; } while (el.firstChild) { tar ...

  3. 【后缀数组之SA数组】【真难懂啊】

    基本上一搜后缀数组网上的模板都是<后缀数组——处理字符串的有力工具>这一篇的注释,O(nlogn)的复杂度确实很强大,但对于初次接触(比如窝)的人来说理解起来也着实有些困难(比如窝就活活好 ...

  4. 转:C语言嵌入式系统编程之软件架构篇

    http://blog.csdn.net/ce123_zhouwei/article/details/6978672

  5. time函数计算时间

    学过C语言的都知道有个time函数可以计算时间, 也好像知道time(NULL)返回的是一个距离1970年1月1日0时0分0秒的秒数. #include <stdio.h> #includ ...

  6. vscode使用vue中的v-for提示错误

    "vetur.validation.template": false 在设置里面把vetur.validation.template改为false 文件→首选项→设置 搜索vetu ...

  7. 【HackerRank】Closest Numbers

    Sorting is often useful as the first step in many different tasks. The most common task is to make f ...

  8. 【leetcode刷题笔记】Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. Linux图像系统框架-理解X11与Qt的层次结构

    转:http://blog.csdn.net/kjfureone/article/details/52848550 1. 前言 图形子系统是linux系统中比较复杂的子系统之一:对下,它要管理形态各异 ...

  10. python之json模块的基本使用

    json模块的作用:将字符串和字典相互转换 json和eval的区别: eval函数不能识别null转换成None json可以将null转换成python可以识别的None json序列化和反序列化 ...