今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题。

于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了。。。

于是检查了一遍又交了一发,还是WA。。。

我还以为是用queue的问题,改成stack也WA,然后干脆放弃STL,手敲了队列,还是WA了。。。

我抓狂了。

感觉没什么问题的,卡了我一个多小时。最后用样例0 1测试,发现是在输入的循环判断时出错了,他要求两个都为0时结束,我只要有一个为0就结束了。。。

坑爹,血的教训。。。

然后我把之前的代码修改后都过了,还尝试了dfs。

于是:

用queue过的:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 101;
int n, m;
int indegree[maxn] = {0}, gragh[maxn][maxn], res[maxn]; void topologic(void) {
queue <int> q;
int cnt = 0;
for (int i = 0; i < n; i++)
if (indegree[i] == 0)
q.push(i);
while (!q.empty()) {
int tmp = q.front();
q.pop();
res[cnt++] = tmp;
for (int i = 0; i < n; i++)
if (gragh[tmp][i] != 0)
if (--indegree[i] == 0)
q.push(i);
}//while
printf("%d", res[0] + 1);
for (int i = 1; i < cnt; i++)
printf(" %d", res[i] + 1);
printf("\n");
} int main() {
while (scanf("%d%d", &n, &m) && (n || m)) {
memset(gragh, 0, sizeof(gragh));
memset(indegree, 0, sizeof(indegree));
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
gragh[a-1][b-1] = 1;
indegree[b-1]++;
}//for input
/*
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%d ", gragh[i][j]);
printf("\n");
}
for (int i = 0; i < n; i++)
printf("%d ", indegree[i]);
printf("\n");
*/
topologic();
}//while
return 0;
}

手敲队列:

#include <cstdio>
#include <cstring>
const int maxn = 101; int g[maxn][maxn], id[maxn], q[maxn]; int main() {
// freopen("in", "r", stdin);
int n, m, a, b, tmp;
int front, tail;
while (scanf("%d%d", &n, &m)) {
if (n == 0 && m == 0)
break;
memset(g, 0, sizeof(g));
memset(id, 0, sizeof(id));
front = tail = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
g[a][b] = 1;
id[b]++;
}
for (int i = 1; i <= n; i++)
if (id[i] == 0)
q[tail++] = i;
while (tail != front) {
tmp = q[front++];
if (front == 1)
printf("%d", tmp);
else
printf(" %d", tmp);
for (int i = 1; i <= n; i++)
if (g[tmp][i] != 0)
if (--id[i] == 0)
q[tail++] = i;
}//while
printf("\n");
}//while
return 0;
}

dfs方法:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 101;
int n, m, t;
int gragh[maxn][maxn], res[maxn], c[maxn]; bool dfs(int u) {
c[u] = -1;
for (int v = 0; v < n; v++)
if (gragh[u][v])
if (c[v] < 0)
return false;
else if (!c[v] && !dfs(v))
return false;
c[u] = 1;
res[--t] = u;
return true;
} bool toposort() {
t = n;
memset(c, 0, sizeof(c));
for (int u = 0; u < n; u++)
if (!c[u])
if (!dfs(u))
return false;
return true;
} int main() {
while (scanf("%d%d", &n, &m) && (n || m)) {
memset(gragh, 0, sizeof(gragh));
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
gragh[a-1][b-1] = 1;
}//for input
toposort();
for (int i = 0; i < n; i++)
printf("%d ", res[i] + 1);
printf("\n");
}//while
return 0;
}

这次是血淋淋的教训啊:

一、输入部分一定要提前测试

二、测试数据要多一点,多想些特殊测试点

三、必须提高敲题效率。

Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现的更多相关文章

  1. UVA.10305 Ordering Tasks (拓扑排序)

    UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...

  2. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  3. UVA 10305 Ordering Tasks(拓扑排序的队列解法)

    题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...

  4. Ordering Tasks UVA - 10305 图的拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  5. UVA - 10305 Ordering Tasks(拓扑排序)

    题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...

  6. UVa 10305 Ordering Tasks (例题 6-15)

    传送门: https://uva.onlinejudge.org/external/103/10305.pdf 拓扑排序(topological sort)简单题 自己代码的思路来自: ==>  ...

  7. M - Ordering Tasks(拓扑排序)

    M - Ordering Tasks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descri ...

  8. Ordering Tasks 拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  9. uva 10305 ordering tasks(超级烂题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHIAAAHDCAYAAABI5T2bAAAgAElEQVR4nOydPY7svLW1awQGNABHCm

随机推荐

  1. redis 重用命令

    一. set 1.smembers key 查看所有元素

  2. chart.js接口开发:X轴步长和Labels旋转角

    一. 当初为什么选择chart.js 当初项目使用库是Zepto,Zepto能支持的chart处理库太少.也是为了使得项目比较轻量化,所以选择了chart.js. 但是最后的显示结果实在太差,放弃了c ...

  3. css中的img和input标签

    一般情况下,行内元素设置宽高是无效的,常见的有a标签.img和input也属于行内元素,但他们却可以设置宽高!!!! 查阅了一些资料才明白,原来css的元素还有另外一种分类方法,可替换元素,不可替换元 ...

  4. 微信分享朋友圈监听(PHP)

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

  5. 一种基于PTP 协议的局域网高精度时钟同步方法(转)

    原文地址 http://www.dzsc.com/data/html/2011-1-17/88338.html 1 引言 在分布式系统中, 常常需要一个全局时间, 用来确定系统中各种事件发生的先后.协 ...

  6. 有关于CSS的面试题和练习

    如果你处在一个需要面试别人有关于CSS方面技巧和知识,一时想起来还真有点难.我把我能想出来的整理在一起,提供给大家参考. 练习要做 正如他们说的,大家实际工作很重要.当然,大家通过工作练习更为重要.这 ...

  7. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  8. CloudStack的VO在调用setRemoved方法抛异常的原因

    今天在开发中发现一个问题,本来想对一个VO对象的removed值赋值,然后去update一下这条记录,一个最简单的set方法,但是在调用时直接抛异常了. 1: public void setRemov ...

  9. 使用TypeDescriptor给类动态添加Attribute

    给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...

  10. .net 调用C++类库

    事实上这一直是个非常无解的问题.最好的办法是将C++类库写成COM. 可是有时候往往不能这个做.那就仅仅有两种办法:转成C函数形式或者Manage C++封装. 下文就介绍了这两种方法. 原帖:htt ...