【POJ 1094】拓扑排序
题意
给出n,代表有以A开始的n个字母,给出它们的m个小于关系(A<B)。
如果前i个关系可以确定n个字母的一个顺序就输出:
Sorted sequence determined after i relations: 排好的字母.
如果前i个关系开始导致矛盾,就输出:
Inconsistency found after i relations.
m个关系后还不能确定顺序就输出:
Sorted sequence cannot be determined.
代码
/*
g[i][j]为邻接矩阵,e[i]为i的入度
in==1代表有矛盾,d==1代表确定顺序
so存排好的顺序
*/
#include<cstdio>
#include<cstring>
#define N 30
int n, m, g[N][N], e[N], c[N], a, b, in, d, so[N];
char va, vb;
void topo()
{
memcpy(c, e, sizeof e);//复制e到c,因为后面要对c改动,所以不能直接用e
int i, top = -;
for(i = ; i < n; i++)
if(!c[i])
{
c[i] = top;//模拟下标堆栈,把入度为0的点通过下标堆栈到c中
top = i;
}
for(i = ; i < n; i++)
{
if(top == -)//n次循环中,若某次找不到入度为0的点,就会回到-1
{
in = ;//说明存在环
return;
}
int u = top;//以top为起点
top = c[u];
so[i] = u;
for(int v = ; v < n; v++)
if(g[u][v] && (--c[v]) == )//去掉u出发的边后,入度为0的点
{
c[v] = top; //堆栈
top = v;
}
}
d = ;
for(i = ; i < n; i++)//判断是否是确定的顺序
if(!g[so[i - ]][so[i]])d = ;//如果出现相邻两个没有给过偏序关系,那么就是不确定的顺序 }
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d ", &n, &m) && n)
{
memset(g, , sizeof g);
memset(e, , sizeof e);
d = in = ;
for(int i = ; i <= m; i++)
{
scanf("%c<%c ", &va, &vb);
a = va - 'A';
b = vb - 'A';
if(!g[a][b] && !d && !in)
{
g[a][b] = ;
e[b]++;
topo();
if(in)
printf("Inconsistency found after %d relations.\n", i);
else if(d)
{
printf("Sorted sequence determined after %d relations: ", i);
for(int j = ; j < n; j++)printf("%c", so[j] + 'A');
printf(".\n");
}
}
}
if(!d && !in)printf("Sorted sequence cannot be determined.\n");
}
}
这个是我参考lrj的《入门经典》的板子写的,耗时0ms
#include <cstdio>
#include <cstring>
#define N 30
int n, m, g[N][N], c[N], t, ans;
char a, b, topo[N];
int dfs(int u)
{
c[u] = -;
for(int v = ; v < n; v++)if(g[u][v])
{
if(c[v] < )return ;
else if(!c[v] && !dfs(v))return ;
}
c[u] = ;
topo[--t] = u;
return ;
}
int toposort()
{
t = n;
memset(c, , sizeof c);
for(int u = ; u < n; u++)
if(!c[u] && !dfs(u))return ;
for(int i = ; i < n; i++)
if(!g[topo[i - ]][topo[i]])return -;
return ;
}
int main()
{
// freopen("in.txt", "r", stdin);
while(~scanf("%d%d ", &n, &m) && n)
{
ans = -;
memset(g, , sizeof g);
for(int i = ; i <= m; i++)
{
scanf("%c<%c ", &a, &b);
g[a - 'A'][b - 'A'] = ;
if(ans == -)
{
ans = toposort();
if(!ans)
printf("Inconsistency found after %d relations.\n", i);
else if(ans == )
{
printf("Sorted sequence determined after %d relations: ", i);
for(int j = ; j < n; j++)printf("%c", topo[j] + 'A');
printf(".\n");
}
}
}
if(ans == -)printf("Sorted sequence cannot be determined.\n");
}
return ;
}
【POJ 1094】拓扑排序的更多相关文章
- Sorting It All Out POJ - 1094 拓扑排序
题意:给N个字母,和M个偏序关系 求一个可确定的全序,可确定是指没有其他的可能例如A>B D>B 那么有ADB DAB两种,这就是不可确定的其中,M个偏序关系可以看做是一个一个按时间给出的 ...
- nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- POJ 1094 拓扑排序
Description: 规定对于一个只有大写字母的字符串是有大小顺序的.如ABCD.即A<B.B<C.C<D.那么问题来了.现在第一行给你n, m代表序列里只会出现前n的 ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- poj 3249 拓扑排序 and 动态规划
思路:我们首先来一遍拓扑排序,将点按先后顺序排列于一维数组中,然后扫描一遍数组,将每个点的出边所连接的点进行更新,即可得到最优解. #include<iostream> #include& ...
- poj 2585 拓扑排序
这题主要在于建图.对9个2*2的小块,第i块如果出现了不等于i的数字,那么一定是在i之后被brought的.可以从i到该数字建一条边. 图建好后,进行一次拓扑排序,判段是否存在环.若存在环,那么就是B ...
- Poj(3687),拓扑排序,
题目链接:http://poj.org/problem?id=3687 题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号 ...
- POJ 1128 拓扑排序 + 深搜
/* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对2 ...
- poj 2367 拓扑排序入门
Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when ...
随机推荐
- Golang tool:include spider library,image library and some other db library such as mysql,redis,mogodb,hbase,cassandra
一.Go_tool This is a tool library for Golang.Dont't worry about not understant it! All comment writes ...
- MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 《至少有那天》——IU
//<你的意义>中文版 那天冷的让人好想哭 可是我那一天看到了爱情 湖边的薄冰和老树 就在零下七度那一天 你和我牵手 慢慢聊着以前那些童话 可能它们都有些意义吧 是吗 一个吻 一滴眼泪 一 ...
- uGUI练习(三) KeyBoard Navigation
练习目标 练习通过键盘在按钮或其它Selectable类型组件上导航 步骤 创建一排的Button,及一个右边的Button 2.查看Button的属性里有一栏下拉列表Navigation,默认选择的 ...
- MySQL的重装问题解决方法
最近在工作上遇到了在Windows环境中将MySQL重装的问题,今天记录一下我的解决过程. 首先因为某些原因,我不得不把mysql卸载,然后重装,不论我用控制面板的卸载删除程序方式还是安全卫士的卸载, ...
- 只有 DBA 才能导入由其他 DBA 导出的文件
两句话搞定问题: grant dba to testuser ; 如果还不行,再执行: alter user testuser default role DBA:
- 我为什么反对推荐新人编程C/C++语言入门?
虽然我接触编程以及计算机时间比较早,但是正式打算转入程序员这个行当差不多是大学第四年的事情 从03年接触计算机,07年开始接触计算机编程, 期间接触过的技术包括 缓冲区溢出(看高手写的shellcod ...
- NOI 1.7编程基础之字符串(35题)
01:统计数字字符个数 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一行字符,统计出其中数字字符的个数. 输入 一行字符串,总长度不超过255. 输出 ...
- 理解SQL Server中的权限体系(下)----安全对象和权限
原文:http://www.cnblogs.com/CareySon/archive/2012/04/12/SQL-Security-SecurableAndPermission.html 在开始阅读 ...
- customized English word breaker for sql server 2008
Open the Registry Editor, by: Clicking Start, and clicking Run. In the Run dialog box, in the Open b ...