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. 剑指offer 面试3题

    面试3题: 题:数组中重复的数字 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复 ...

  2. AWK Demo

    AWK is so poweful. AWK功能太强大了: @1:处理格式化文本(感觉主要还是以这个为主) @2:对文本逐行进行处理(过滤某些行,保留符合条件的) @3:将文件根据不同的条件分成多个文 ...

  3. 每天一个Linux命令(60)ip命令

        ip命令是Linux下较新的功能强大的网络配置工具.     (1)用法:     用法:  ip  [OPTIONS]  OBJECT  [COMMAND [ARGUMENTS]]     ...

  4. 每天一个Linux命令(47)route命令

        Linux系统的route命令用于显示和操作内核IP路由表(show / manipulate the IP routing table).     (1)用法:     用法:  route ...

  5. cocos2d关于glew32.lib错误(转)

    应项目需要使用cocos2d-x开发,又要学习新东东了.·cocos2d-x 是一个支持多平台的 2D 手机游戏引擎,用C++重写cocos2d-iphone引擎的一个开源项目,想了解更多的童鞋美去百 ...

  6. Nginx之Location模块

    location 的语法 locltion可以把不同方式的请求,定位到不同的处理方式上 location分类及用法 location大致分为三类: location = patt {} [精准匹配] ...

  7. Sagan

    来自Sagan官网:https://quadrantsec.com/services_technology/product_technology/ Sagan是一个多线程的,实时的安全信息事件管理分析 ...

  8. Android系统属性SystemProperties在应用层的用法【转】

    本文转载自:https://blog.csdn.net/lilidejing/article/details/53288243 如果你看到这篇文章了,说明你已经是资深程序员,会发现整个Android系 ...

  9. Kubernetes client-go

    Github地址:https://github.com/kubernetes/client-go 访问kubernetes集群有几下几种方式: 方式 特点 支持者 Kubernetes dashboa ...

  10. Java I/O 小结

    主要内容: 一.输入流基类:InputStream 和 OutputStream(字节流). Reader 和 Writer(字符流) 二.文件字节流:FileInputStream和FileOutp ...