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

于是按自己想法敲了一遍,用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. [原创]mac终端前面的计算机名怎么改??

    1.修改-之前的名称 mac环境,系统 OS X Yisemite,打开终端, 执行下面命令“Tmp”是你想要改的电脑名称 sudo scutil --set HostName Tmp 执行前,执行后 ...

  2. 第三次作业随笔(new)包含了补作业

    第三次作业的题目:http://www.cnblogs.com/fzuoop/p/5187275.html 第一次看到题目的时候觉得应该是挺简单的,只要把输入的那一串东西挨个判断,用数列的方法,如果碰 ...

  3. CodeForces 711A Bus to Udayland (水题)

    题意:给定一个n*4的矩阵,然后O表示空座位,X表示已经有人了,问你是不能找到一对相邻的座位,都是空的,并且前两个是一对,后两个是一对. 析:直接暴力找就行. 代码如下: #pragma commen ...

  4. sql2008来远程访问sql2005数据库服务器

    今天搞了一个下午终于搞定了数据库的远程访问.其基本步骤如下: sql2008的配置: sql server 2008默认是不允许远程连接的,sa帐户默认禁用的,如果想要在本地用SSMS连接远程服务器上 ...

  5. CString和string的互相转换

    CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...

  6. PHP发送邮件。

    第三方类库: ①.email.class.php. ②.phpmailer:https://github.com/PHPMailer/PHPMailer. PHPMailer发送邮件”SMTP 错误: ...

  7. chrome 41 空格 &nbsp;

    chrome 41对半角空格的解析 当做一个汉字宽度来处理了. 导致很多网站出现异常. 目前能想到的方法是删掉用来规范格式的空格. 老版chrome chrome41 和讯网也有这种问题 有更好的处理 ...

  8. VC++ 网络编程总结(二)

    2.基本的Windows Socket API编程 需要在程序中添加下面的包含语句:#include <winsock2.h>   #pragma comment( lib, " ...

  9. 远程调用WMI安装软件

     Invoke-WMIMethod  -ComputerName $ip -Credential $cred -path Win32_product -Name Install -ArgumentLi ...

  10. Java多线程技术学习笔记(一)

    目录: 概述 多线程的好处与弊端 JVM中的多线程解析 多线程的创建方式之一:继承Thread类 线程的状态 多线程创建的方式之二:实现Runnable接口 使用方式二创建多线程的好处 多线程示例 线 ...