题目传送门

 /*
拓扑排序裸题:有三种情况:
1. 输入时发现与之前的矛盾,Inconsistency
2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环);
flag = -1 表示不确定;return 2 表示拓扑序唯一
3. 其他情况都是 Sorted sequence cannot be determined. */
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int in[MAXN], ans[MAXN];
vector<int> G[MAXN];
bool used[MAXN][MAXN];
int n, m; int TopoSort(void)
{
int flag = ;
memset (in, , sizeof (in));
for (int i=; i<=n; ++i)
{
for (int j=; j<G[i].size (); ++j) in[G[i][j]]++;
} queue<int> Q; int cnt = ;
for (int i=; i<=n; ++i) if (!in[i]) Q.push (i); while (!Q.empty ())
{
if (Q.size () > ) flag = -;
int u = Q.front (); Q.pop ();
ans[++cnt] = u;
for (int i=; i<G[u].size (); ++i)
{
int v = G[u][i];
in[v]--;
if (!in[v]) Q.push (v);
}
} if (cnt != n) return ;
else if (flag == -) return -;
else return ;
} int main(void) //POJ 1049 Sorting It All Out
{
//freopen ("POJ_1094.in", "r", stdin); char s[];
while (scanf ("%d%d", &n, &m) == )
{
if (n == && m == ) break; for (int i=; i<=n; ++i) G[i].clear ();
memset (used, , sizeof (used)); int flag = ;
for (int i=; i<=m; ++i)
{
scanf ("%s", &s);
if (flag) continue; int u = s[] - 'A' + ;
int v = s[] - 'A' + ; if (used[v][u]) {flag = ; printf ("Inconsistency found after %d relations.\n", i); continue;} used[u][v] = true;
G[u].push_back (v); int res = TopoSort ();
if (res == ) {flag = ; printf ("Inconsistency found after %d relations.\n", i); continue;}
else if (res == )
{
flag = ;
printf ("Sorted sequence determined after %d relations: ", i);
for (int i=; i<=n; ++i) printf ("%c", ans[i] + 'A' - );
printf (".\n");
}
} if (!flag) puts ("Sorted sequence cannot be determined.");
} return ;
} /*
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
*/

拓扑排序 POJ 1049 Sorting It All Out的更多相关文章

  1. 拓扑排序 POJ 1094 Sorting It All Out

    题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...

  2. 拓扑排序(Topological Sorting)

    一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...

  3. 拓扑排序 POJ 2367

    今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...

  4. 图论之拓扑排序 poj 2367 Genealogical tree

    题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...

  5. 拓扑排序 (Topological Sorting)

    拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...

  6. 使用 C# 代码实现拓扑排序

    0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. https://www.codeproject.com/Articles/869059/Topological-sorti ...

  7. [LintCode] 拓扑排序

    http://www.lintcode.com/zh-cn/problem/topological-sorting/# 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则 ...

  8. [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果

    [C#]使用 C# 代码实现拓扑排序   目录 0.参考资料 1.介绍 2.原理 3.实现 4.深度优先搜索实现 回到顶部 0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. ...

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

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

随机推荐

  1. show slave各项参数解释

    how slave status 各个参数的解释 -- mysql 分类: mysql基础2012-08-23 11:03 2315人阅读 评论(0) 收藏 举报 服务器sslfilesqltable ...

  2. FineUI第二天

    原博文http://www.cnblogs.com/sanshi/archive/2012/02/12/2347789.html 1.首先复制extJS的文件夹到根目录. 2.引用程序集 3.配置配置 ...

  3. [OpenJudge 3064]坠落的蚂蚁

    [OpenJudge 3064]坠落的蚂蚁 试题描述 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如果两只蚂蚁碰头,则它们立即交换速度并继续 ...

  4. 练习英语ing——[POJ1004]Financial Management

    [POJ1004]Financial Management 试题描述 Larry graduated this year and finally has a job. He's making a lo ...

  5. lvs之ip-tun(ip隧道)技术的学习与实践

    1.配置测试环境 修改IP windows 200.168.10.4 lvs server  ip:200.168.10.1 因为IP隧道模式只需要一个网卡  所以就停掉其他网卡 web server ...

  6. jquery 常用类别选择器

    1.$('#showDiv'):  id选择器,相当于javascript中的documentgetElementById("showDiv"); 2.$("onecla ...

  7. 转MYSQL学习(四) 查询

    MySQL中select的基本语法形式: select 属性列表 from 表名和视图列表 [where 条件表达式] [group by 属性名[having 条件表达式]] [order by 属 ...

  8. Windows下尝试PHP7提示丢失VCRUNTIME140.DLL的问题解决

    前天PHP7.0.0正式版发布了,有一些比较好的改进,官方也说速度比php5.6快了两倍,性能上有了很大提升,并且也发布了从php5.x向php7迁移的问题,所以今后php网站迁移后能够大幅度的提升网 ...

  9. mybatis前台传给带年月日时分秒的数据给后台,后台接收不到时分秒

    框架spring+springMVC+mybatis, 前台给后台传数据传不了时分秒,所以用springMVC的注解解决了,记录一下 controller中如下: /** * * 方法描述 : 使用@ ...

  10. Ubuntu13.04 安装 chrome

    1.chrome官网下载deb安装包:https://www.google.com/intl/zh-CN/chrome/browser/ 2.进入下载好的目录执行:sudo dpkg -i googl ...