dfs
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = 1e5+;
struct Edge{
int lst;
int to;
}edge[maxn*];
int head[maxn]; int qsz; inline void add(int u, int v) {
edge[qsz].lst = head[u];
edge[qsz].to = v;
head[u] = qsz++;
} int vis[maxn];
int ans[maxn];
int qtot;
bool dfs(int u) {
int v, i;
vis[u] = -;
for (i=head[u]; i; i=edge[i].lst) {
v = edge[i].to;
if (vis[v] == -) return false;
else if (!vis[v] && !dfs(v)) return false;
}
ans[qtot--] = u;
vis[u] = ;
return true;
} int main()
{
int t, n, m, i, j, u, v;
scanf("%d", &t);
while (t--) {
// init;
qsz = ;
memset(head, , sizeof(head));
memset( vis, , sizeof( vis));
scanf("%d%d", &n, &m);
qtot = n;
for (i=; i<=m; ++i) {
scanf("%d%d", &u, &v);
add(v, u);
}
for (i=; i<=n; ++i)
if (!vis[i])
if (!dfs(i))
break; if (!qtot) printf("Correct\n");
else printf("Wrong\n");
} return ;
}

bfs
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = 1e5+;
struct Edge{
int lst;
int to;
}edge[maxn*];
int head[maxn];
int qsz; int inq[maxn];
int q[maxn];
int qhead;
inline void add(int u, int v) {
edge[qsz].lst = head[u];
edge[qsz].to = v;
head[u] = qsz++;
} int main()
{
int t, n, m, i, j, u, v;
scanf("%d", &t);
while (t--) {
// init;
qsz = ;
memset(head, , sizeof(head));
memset(inq, , sizeof( inq));
qhead = ; scanf("%d%d", &n, &m);
for (i=; i<=m; ++i) {
scanf("%d%d", &u, &v);
add(u, v);
inq[v]++;
}
for (i=; i<=n; ++i)
if (!inq[i])
q[qhead++] = i;
for (i=; i<qhead; ++i) {
for (j=head[q[i]]; j; j=edge[j].lst) {
v = edge[j].to;
inq[v]--;
if (!inq[v]) q[qhead++] = v;
}
}
if (qhead == n) printf("Correct\n");
else printf("Wrong\n");
} return ;
}

 

拓扑排序bfs_dfs的更多相关文章

  1. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  2. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  3. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  4. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  5. 图——拓扑排序(uva10305)

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

  6. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  7. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  8. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  9. *HDU1285 拓扑排序

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. C# HttpWebResponse WebClient 基础连接已经关闭: 发送时发生错误.

    https://blog.csdn.net/sun49842566/article/details/82802297 net 4.0 设置: ServicePointManager.SecurityP ...

  2. Linux 查看进程之PS命令

    要对进程进行检测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程运行状态.Linux 系统中 我们可以使用 ps 命令查看进程. ps 命令介绍       ps(process stat ...

  3. _quest_random

    -- 随机任务-- 可以实现玩家随机获取任务-- 小技巧:需要控制物品法防,在_function_menu表配置物品indexID为28 `comment`备注 `questId`任务ID `chan ...

  4. Nginx 配置负载均衡

    nginx负载均衡配置,主要是proxy_pass,upstream的使用. 注意问题,多台机器间session的共享问题. 不用session,用户cookie.或者用redis替代session. ...

  5. Windows to go 慢,更换 user profile 路径

    用 wintousb 安装了 windwos 10 到 u盘 之后, 发觉这个windows 贼慢,卡的不行. 想起以前台式机上用[太阳花]SDD,硬盘满了也是这个感觉的. 就知道 C盘的userpr ...

  6. 【你不一定知晓的】C#取消异步操作

    [你不一定知晓的]C#取消异步操作 在.Net和C#中运行异步代码相当简单,因为我们有时候需要取消正在进行的异步操作,通过本文,可以掌握 通过CancellationToken取消任务(包括non-c ...

  7. Lab 9-2

    Analyze the malware found in the file Lab09-02.exe using OllyDbg to answer the following questions. ...

  8. Mac+Apache+PHP 安装 Xdebug 方法

    MAC homebrew自2018/3/31之后弃用homebrew/php By 31st March we will deprecate and archive the Homebrew/php ...

  9. 『TensorFlow』函数查询列表_神经网络相关

    tf.Graph 操作 描述 class tf.Graph tensorflow中的计算以图数据流的方式表示一个图包含一系列表示计算单元的操作对象以及在图中流动的数据单元以tensor对象表现 tf. ...

  10. openLDAP环境搭建

    OpenLDAP搭建   PS:本次实验是基于centos7,OpenLDAP使用2.4.44版本. 一.初始化环境 1.初始化环境 命令如下: ntpdate -u ntp.api.bz & ...