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

于是按自己想法敲了一遍,用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. class dict

    class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dic ...

  2. Domino 迁移到Exchange 服务器 之在Domino Server 创建用户!

    我们打开Lotus Admin,导航到注册,点击到需要设置的人,然后再选择验证者标识符选择相应的组织配置标识符:

  3. Remove Duplicates from Sorted List @LeetCode

    /** * Remove Duplicates from Sorted List * * Given a sorted linked list, delete all duplicates such ...

  4. EasyUI Accordion下的Panel面板初始化时全部折叠

    EasyUI Accordion下的Panel面板有一个属性:selected,默认值为:false.初始化时,若设置'selected:true',则面板默认打开,效果如下: <div tit ...

  5. C# ASP.NET Webservice调用外部exe无效的解决方法

    最近用asp.net做webservice,其中有个功能是调用执行外部的exe(类似cmd中执行),但执行Process.Start之后就没有结果,同样代码在winform下正常,折腾两天终于找到解决 ...

  6. js原生appendChild的bug

    appendChild 主要是用来追加节点 插入到最后 window.onload = function(){ var ul2 = document.getElementById('ul2'); va ...

  7. jQuery操作checkbox选择

    1.checkbox list选择 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  8. cocos2d-x如何新建一个模板项目

    方法一: $ cd cocos2d-x$ ./setup.py$ source FILE_TO_SAVE_SYSTEM_VARIABLE$ cocos new MyGame -p com.your_c ...

  9. Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp

    D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...

  10. 【原创】AltiumDesigner 6 的自定义菜单

    AltiumDesigner 6 的自定义菜单,数据保存在DXP.RCS文件中通过测试,添加一个自定义菜单名为HHH,然后用Editplus在C:\Users\pcittsy\AppData\Roam ...