拓扑排序 POJ 1049 Sorting It All Out
/*
拓扑排序裸题:有三种情况:
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的更多相关文章
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- 拓扑排序(Topological Sorting)
一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...
- 拓扑排序 POJ 2367
今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- 拓扑排序 (Topological Sorting)
拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...
- 使用 C# 代码实现拓扑排序
0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. https://www.codeproject.com/Articles/869059/Topological-sorti ...
- [LintCode] 拓扑排序
http://www.lintcode.com/zh-cn/problem/topological-sorting/# 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则 ...
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
[C#]使用 C# 代码实现拓扑排序 目录 0.参考资料 1.介绍 2.原理 3.实现 4.深度优先搜索实现 回到顶部 0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
随机推荐
- 淘宝(阿里百川)手机客户端开发日记第四篇 自定义ListView详解
我们知道,如果采用官方的ListView,实现的功能在很多时候,并不能满足自己的业务需求,比如在设计到复杂的列表的时候,这一节,我们就开始动手自己实现自定义的ListView. 在上一节中,我们采用了 ...
- ubuntu12 apache2部署多个网站的方法
由于对apache2的配置不是很精通.吃了不少瘪. 这里总结下,希望对碰到同样问题的人带来帮助: 假设需求是有两个站点,site1和site2.其端口分别为80和8080. 1. 配置监听端口 修改 ...
- 2014.7建兰NOIP模拟Day1 Running
突然间翻到着题,想想那时的我真是垃圾,这么简单的tarjan缩点+树上倍增都不会..还想了3h+.. 什么时候写了它吧...
- PHP很有用的一个函数ignore_user_abort ()
PHP很有用的一个函数ignore_user_abort () 2013-01-16 14:21:31| 分类: PHP | 标签:php 函数 |举报|字号 订阅 ignore_us ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- Object-c 控制语句
控制语句: 分支语句 if-else 有控制机制 switch 循环语句 while do-while for 跳转语句 break,continue,goto
- Android 中的AsyncTask
在后台下载图片,下载完成后更新UI是一个很常见的需求.在没有AsyncTask类之前,我们需要写许多thread和Handler的代码去实现这个功能,有了AsyncTask,一切变得简单了.下面摘抄谷 ...
- iOS 中的Push Notifications简单实现(APNS)
Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications.ios的这2种noti ...
- codeforces 374A Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行 m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...
- HDU 4334 Trouble (暴力)
Trouble Time Limit: 5000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...