比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径。

算法导论里面的拓扑排序运用的是DFS the DAG,记录每个顶点的进入时间和离开时间,根据其先后插入单链表的做法。而我认为一种方法是更直观的,就是维护一个入度为0的顶点集合(我用的队列其实没差),每次对其中一个加入结果序列——同时删除它的所有出边——更新其他点的入度的做法,在我的算法数据结构实现模板里有正确实现https://github.com/jily16/ACMCodes(打广告

在判断拓扑排序结果唯一性时这种方法也表现出了一个优势,每次访问0入度集合时查看大小,当元素多于1的时候可行的选择就出现了分歧——即可判定此DAG的拓扑排序不唯一(当然本题的信息在不断更新,所以不能立刻判死)。

AC代码:

 #include <vector>
#include <iostream>
#include <queue>
using namespace std;
//POJ1094
int const INF = 0x3f3f3f3f;
//返回空数组说明暂时不行
//cyclic说明矛盾
vector<int> ts(vector<vector<int> > const &g, bool &cyclic)
{
int n = g.size();
vector<int> d(n);
for (int i = ; i < n; ++i)
{
for (int j = ; j < n; ++j)
{
if (g[i][j] < INF)
{
++d[j];
}
}
}
queue<int> q;
for (int i = ; i < n; ++i) if (d[i] == ) q.push(i);
int d0;
int tot = ;
bool not_unique = false;
vector<int> ans;
while (!q.empty())
{
if (q.size() > )
{
not_unique = true; //解不唯一
}
d0 = q.front();
q.pop();
ans.push_back(d0);
++tot;
for (int i = ; i < n; ++i)
{
if (d[i] != && g[d0][i] < INF)
{
--d[i];
if (d[i] == )
{
q.push(i);
}
}
}
}
if (tot != n) cyclic = true;
if (not_unique) return vector<int>();
else return ans;
}
int main()
{
int n, m;
while (cin >> n >> m && n && m)
{
bool done = false;
vector<vector<int> > g;
char alp1, alp2;
char lessthan;
int num1, num2;
for (int i = ; i < m; ++i)
{
cin >> alp1 >> lessthan >> alp2;
if (done) continue;
num1 = alp1 - 'A';
num2 = alp2 - 'A';
int bignum = max(num1, num2);
//extend the graph
if (g.size() < bignum + )
{
int ori_size = g.size();
for (int i = ; i < ori_size; ++i)
{
for (int j = ; j < bignum + - ori_size; ++j)
{
g[i].push_back(INF);
}
}
for (int i = ; i < bignum + - ori_size; ++i)
{
g.push_back(vector<int>(bignum + , INF));
}
}
g[num1][num2] = ;
//judge from here
bool cycle = false;
if (g.size() < n)
{
ts(g, cycle);
if (cycle)
{
cout << "Inconsistency found after " << i + << " relations.\n";
done = true;
}
else
{
if (i == m - )
{
cout << "Sorted sequence cannot be determined.\n";
done = true;
}
}
}
else
{
vector<int> ans = ts(g, cycle);
if (cycle)
{
cout << "Inconsistency found after " << i + << " relations.\n";
done = true;
}
else
{
if (ans.size() != )
{
cout << "Sorted sequence determined after " << i + << " relations: ";
for (int i = ; i < n; ++i) cout << (char)(ans[i] + 'A');
cout << ".\n";
done = true;
}
else
{
if (i < m - ) continue;
else
{
cout << "Sorted sequence cannot be determined.\n";
done = true;
}
}
}
}
}
}
return ;
}

POJ1094——拓扑排序和它的唯一性的更多相关文章

  1. POJ1094 拓扑排序

    问题:POJ1094   本题考查拓扑排序算法   拓扑排序:   1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组.   循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...

  2. poj1094拓扑排序

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

  3. poj1094 拓扑排序(出度入度简单使用)

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

  4. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  5. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

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

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

  7. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  8. POJ1094 字母排序(拓扑排序)

    该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.该序列不能判断是否有序: 3.该序列字母次 ...

  9. 拓扑排序(topsort)

    本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...

随机推荐

  1. CSS| 颜色名

    CSS 颜色名 所有浏览器都支持的颜色名. HTML 和 CSS 颜色规范中定义了 147 中颜色名(17 种标准颜色加 130 种其他颜色).下面的表格中列出了所有这些颜色,以及它们的十六进制值. ...

  2. IE中操作粘贴板复制和粘贴

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Go 在 TiDB 的实践

    https://blog.csdn.net/RA681t58CJxsgCkJ31/article/details/79215751 更多TiDB链接: https://my.oschina.net/z ...

  4. AD用户移除所属组

    AD用户移除所属组: $Membership = Get-ADPrincipalGroupMembership $Users $Membership.distinguishedName Remove- ...

  5. Python实例---爬去酷狗音乐

    项目一:获取酷狗TOP 100 http://www.kugou.com/yy/rank/home/1-8888.html 排名 文件&&歌手 时长 效果: 附源码: import t ...

  6. Yii2 使用 RESTful 写API接口 实例

    Yii2 使用 RESTful? 其实 Yii2 框架本身就对 RESTful 是友好支持的,具体可以看官方文档(http://www.yiichina.com/doc/guide/2.0/rest- ...

  7. Docker容器学习与分享02

    1.docker容器的创建 首先运行一个centos容器,感受一下Docker容器的便捷 首先先看一下镜像仓库 发现仓库里没有镜像,也就是没有创建容器的模板,这时考虑从REPOSITORY中拉取镜像( ...

  8. 在Word2007,2010,2016中分栏但不换页的方法

    解决方法: word2007:界面左上角的按钮->选择word选项->依次点击“高级”->“版式选项”->点开加号,“按word6.x/95/97的方式排放脚注”. Word2 ...

  9. 投稿核心期刊、中文重要期刊、SCI二区及以上期刊目录

    大家在研究生期间想必均经历过投稿核心期刊的烦恼,不知道哪些是核心期刊,那些是普通期刊,万一选的不对岂不是浪费了时间,因此小顾在网络上收集了了2018北大核心期刊目录及全国中文重要期刊目录和SCI二区及 ...

  10. python 之 string() 模块

    common string oprationsimport string1. string constants(常量) 1) string.ascii_letters       The concat ...