POJ1094——拓扑排序和它的唯一性
比较模板的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——拓扑排序和它的唯一性的更多相关文章
- POJ1094 拓扑排序
问题:POJ1094 本题考查拓扑排序算法 拓扑排序: 1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组. 循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...
- poj1094拓扑排序
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29539 Accepted: 10 ...
- poj1094 拓扑排序(出度入度简单使用)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37764 Accepted: 13 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ1094 字母排序(拓扑排序)
该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.该序列不能判断是否有序: 3.该序列字母次 ...
- 拓扑排序(topsort)
本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...
随机推荐
- python的函数(一)
摘要: python的函数(一)主要写函数的基础部分. 1,函数的好处 2,函数的定义与调用 1,函数的好处 函数应该有2个好处: 1,是降低代码的复杂度, 2,是减少代码量,避免重复的写相同的代码. ...
- Oracle EBS AR 收款取数
-- 收款核销,贷项通知单核销也是通过ar_receivable_applications_all表 SELECT cr.receipt_number ,ad.amount_dr ,ad.amount ...
- CSS未知宽高元素水平垂直居中
方法一 :table.cell-table 思路:显示设置父元素为:table,子元素为:cell-table,这样就可以使用vertical-align: center,实现水平居中优点:父元素(p ...
- 单例模式实现 Volitile , interlocked
//单例模式: //1. 双检锁机制 Volatile.write() //2. 静态变量 //3. Interlocked.CompareExchange(ref single, temp, nul ...
- python图像处理:pytesseract和PIL
大概介绍下相关模块的概念: Python-tesseract 是光学字符识别Tesseract OCR引擎的Python封装类.能够读取任何常规的图片文件(JPG, GIF ,PNG , TIFF等) ...
- python使用mechanize模拟登陆新浪邮箱
mechanize相关知识准备: mechanize.Browser()<br># 设置是否处理HTML http-equiv标头 set_handle_equiv(True)<br ...
- bower包管理工具
安装: npm install bower -g (全局安装) 验证: bower --version 指令( 以vue为例 ): 1. bower info vue 查看 ...
- etcd管理
操作etcd有命令行工具etcdctl,有两个api版本互不兼容的,系统默认的v2版本,kubernetes集群使用的是v3版本,v2版本下是看不到v3版本的数据的,我也是找了些资料才了解这个情况. ...
- python+requests实现接口测试 - get与post请求使用(转载)
转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...
- XMl转Map-map调用公共模板
效果 <?xmlversion="1.0"encoding="utf-8"?> <SERVICE> <SERVICE_HEADER ...