POJ1094 拓扑排序
//Memory: 224K Time: 0MS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
;
bool g[maxn][maxn];
int indegree[maxn];
int in[maxn];
int vis[maxn];
int flag;
int n, m;
int out[maxn];
string s;
int size;
int ans;
int topologicalSort()
{
memset(vis, , sizeof(vis));
memcpy(in, indegree, sizeof(in));
size = ;
flag = ;
while (size < n){
;
int next;
; i < n; i++){
if (vis[i]) continue;
) {
num++;
) break;
next = i;
}
}
) ;
else {
out[size++] = next;
vis[next] = ;
; i < n; i++) {
if ( g[next][i] && !vis[i] )
in[i]--;
}
)
flag = ;
}
}
) ;
;
}
int main()
{
while (cin >> n >> m && n) {
memset(g, , sizeof(g));
memset(indegree, , sizeof(indegree));
ans = ;
; i < m; i++){
cin >> s;
) continue;
] - ] - 'A']) {
g[s[] - ] - ;
indegree[s[] - 'A']++;
ans = topologicalSort();
) {
cout << <<" relations." << endl;
}
){
cout << << " relations: ";
; i < n; i++)
cout << (char)(out[i] + 'A');
cout << "." << endl;
}
}
}
)
cout << "Sorted sequence cannot be determined." << endl;
}
;
}
POJ1094 拓扑排序的更多相关文章
- POJ1094——拓扑排序和它的唯一性
比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径. 算法导论里面的拓扑 ...
- 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个字母,均为大写. 想法:显然,很容 ...
- 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.该序列字母次 ...
- ACM/ICPC 之 拓扑排序范例(POJ1094-POJ2585)
两道拓扑排序问题的范例,用拓扑排序解决的实质是一个单向关系问题 POJ1094(ZOJ1060)-Sortng It All Out 题意简单,但需要考虑的地方很多,因此很容易将code写繁琐了,会给 ...
- POJ - 1094 Sorting It All Out(拓扑排序)
https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...
随机推荐
- [1] Tornado Todo Day0
Github地址: day0 初始化数据库: jakeychen@JC:~/Public/tornado_todo$ pwd /home/jakeychen/Public/tornado_todo j ...
- Android SDCard和内部存储中gcc编译后的可执行文件无法运行提示 cannot execute - Permission denied
原因是mount的方式问题,root后运行 su mount -o rw,remount /mnt/sdcard //SDCard mount -o rw,remount /mnt/interna ...
- 实现在线阅读pdf功能--php
在网上找了很久,想要实现一个在线阅读word,pdf文件的功能,网上的资料很多,但是提到真正怎么实现的比较少.现在我来简单说明一下,我实现的过程. 我现在只能实现在线阅读pdf(将word等转换成pd ...
- DBA 思想天空笔记
/*+leading(t1) use_nl(t2*/这个HINT的含义,其中use_nl表示强制用嵌套循环连接方式.Leading(t1)表示强制先访问t1表,也就是t1表作为驱动表,增加HINT的目 ...
- Linux下编译第三方库的问题
因为各个Linux发行版之间的差异还是挺大的,有一些预安装在系统上的基本库是不一样的(不仅仅是版本,有一些是有和无的区别). 那么问题来了: 编译第三方库./configure的时候一般我们不会定制那 ...
- 使用linux系统做路由转发
使用linux系统(PC机)做路由转发 关键字:linux,Fedora,route,iptables,ip_forward 最近做网络实验,在实验过程中需要用到linux的转发功能,但是遇到一些问题 ...
- hdu 1159 Common Subsequence(LCS最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- linux时钟管理
ref https://access.redhat.com/solutions/18627 在el5中 如何查看系统现在使用的clock source是什么? 答: 方式1:需要说明的是不能保证这个两 ...
- Direct3D 索引缓存
小学的时候我们知道3个顶点组成一个三角形,那么四个顶点我们会说有4个三角形.这就是一个顶点同时参与了四次绘制三角形的结果. 在程序中也一样,比如我们绘制的两个三角形是挨着一起的,总有几个顶点是重合的. ...
- python高级编程之选择好名称:pepe8和命名最佳实践
# # -*- coding: utf-8 -*- # # python:2.x # __author__ = 'Administrator' # my_list=['a','b','c','d'] ...