拓扑排序 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 & ...
随机推荐
- $gte 必须使用双引号,且冒号后面不能有空格
pry(main)> puts "showlastupdate:{\"$gte\":\"#{Date.today.to_s(:number)}\" ...
- [BZOJ2303][Apio2011]方格染色
[BZOJ2303][Apio2011]方格染色 试题描述 Sam和他的妹妹Sara有一个包含n × m个方格的 表格.她们想要将其的每个方格都染成红色或蓝色. 出于个人喜好,他们想要表格中每个2 × ...
- 12 day 1
#include <cstdio> int i,j,m,n,t; long long f[6000][6000]; inline int min(int a,int b){ return ...
- JS的trim()方法
去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim.ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写.下面的实现方法是用到了正则表达式,效率不错,并把这三 ...
- Java锁之自旋锁详解
锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) .这些已经写好提供的锁为我们开发提供了便利,但是锁的具体性质以及类 ...
- Spring面试题集
一.Spring简介 * Spring框架有哪几部分组成? Spring框架有七个模块组成组成,这7个模块(或组件)均可以单独存在,也可以与其它一个或多个模块联合使用,主要功能表现如下: ...
- velocity的string转数字,numberTool
velocity的string转数字,非常有意思: 1.通过java的包装类进行转换 #set($intString = "20") #set($Integer = 0) $Int ...
- php 今天 昨天 明天 时间戳
2013年9月10日 16:26:25 echo strtotime('now'),'<br>'; echo strtotime('today'),'<br>'; echo s ...
- HDU1850 Being a Good Boy in Spring Festival(博弈)
Being a Good Boy in Spring Festival Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I ...
- ECSHOP去版权与标志
前台部分: 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop 前者在后台商店设置 - 商店标题修改 后者打开includes/lib_main.php $page_t ...