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. bitbucket迁移

    bitbucket 迁移 1.停止向旧仓库地址提交代码 [dev]2.导入代码至新仓库地址 [op]3.修改本地仓库地址 第一种方式:git remote set-url origin [url] ; ...

  2. linux centos7添加ip黑名单禁止某个ip访问

    centos7用的是firewall 添加单个黑名单只需要把ip添加到 /etc/hosts.deny 格式  sshd:$IP:deny vim /etc/hosts.deny   添加你要禁止的i ...

  3. vmware workstation 14 快速安装操作系统

    一.NAT网络配置 设置子网IP.子网掩码 网关IP:子网IP前三位+2 DHCP设置 二.网卡配置 vi /etc/sysconfig/network-scripts/ifcfg-ens33 TYP ...

  4. PTA编程总结1—打印沙漏

    题目:7-1 打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓&q ...

  5. MySQL连接java

    1.创建一个java project,建一个lib文件. 下载MySQL-connecor. 将下载好的文件打开,将mysql-connector-java-5.0.8-bin.jar文件粘贴复制到l ...

  6. 关于ExpressionChangedAfterItHasBeenCheckedError

    最近在stackoverflow上似乎每天都有一些关于angular报错‘ExpressionChangedAfterItHasBeenCheckedError’的问题.发生这些问题通常是由于angu ...

  7. webview元素定位

    genymotion 模拟器:android  5.0.0 python 2.7 appium 1.4.16.1 1.app原生元素定位(常用) driver.find_element_by_id(“ ...

  8. [easyUI] datagrid 数据格 可以进行分页

    1. 新建一个GridNode的类: public class GridNode { private Long id; private String title;//投票标题 private Inte ...

  9. Token国内地铁使用城市

    天津 广州 深圳 南京 武汉 台北 高雄

  10. fcn+caffe+voc2012实验记录

    参考博客: http://blog.csdn.net/haoji007/article/details/77148374 http://blog.csdn.net/jacke121/article/d ...