The hints of the problem has given detailed information to implement the topological sorting algorithm. In fact, the toposort algorithm of the hint is the BFS version, which uses the indegrees of each node.

The code is as follows.

 #include <iostream>
#include <vector>
#include <queue> using namespace std; struct DirectedGraphNode {
int label;
vector<int> neighbors;
DirectedGraphNode(int i) : label(i) {}
}; vector<DirectedGraphNode*> init_graph(int nodes) {
vector<DirectedGraphNode*> graph(nodes);
for (int i = ; i < nodes; i++)
graph[i] = new DirectedGraphNode(i);
return graph;
} vector<int> compute_indegree(vector<DirectedGraphNode*>& graph) {
vector<int> degrees(graph.size(), );
for (int i = ; i < (int)graph.size(); i++)
for (int j = ; j < (int)graph[i] -> neighbors.size(); j++)
degrees[graph[i] -> neighbors[j]]++;
return degrees;
} bool noCycle(vector<DirectedGraphNode*>& graph) {
vector<int> degrees = compute_indegree(graph);
int nodes = graph.size();
queue<int> zeros;
for (int i = ; i < (int)degrees.size(); i++)
if (!degrees[i]) zeros.push(i);
while (!zeros.empty()) {
int zero = zeros.front();
zeros.pop();
for (int i = ; i < (int)graph[zero] -> neighbors.size(); i++)
if (--degrees[graph[zero] -> neighbors[i]] == )
zeros.push(graph[zero] -> neighbors[i]);
nodes--;
}
return nodes == ;
} int main(void) {
int cases;
scanf("%d", &cases);
for (int i = ; i < cases; i++) {
int nodes, edges;
scanf("%d %d", &nodes, &edges);
vector<DirectedGraphNode*> graph = init_graph(nodes);
int node, neigh;
for (int j = ; j < edges; j++) {
scanf("%d %d", &node, &neigh);
graph[node - ] -> neighbors.push_back(neigh - );
}
if (noCycle(graph)) printf("Correct\n");
else printf("Wrong\n");
}
return ;
}

[hihoCoder] 拓扑排序·一的更多相关文章

  1. hihoCoder #1457 : 后缀自动机四·重复旋律7(后缀自动机 + 拓扑排序)

    http://hihocoder.com/problemset/problem/1457 val[i] 表示状态i所表示的所有字符串的十进制之和 ans= ∑ val[i]在后缀自动机上,从起始状态走 ...

  2. hihocoder 1343 : Stable Members【拓扑排序】

    hihocoder #1343:题目 解释:一个学习小组,一共有N个学员,一个主管.每个学员都有自己的导师(一个或者多个),导师可以是其他学员也可以是主管.每周学员都要把自己的学习报告和收到的报告提交 ...

  3. hihoCoder 1175:拓扑排序二

    题目链接: http://hihocoder.com/problemset/problem/1175 题目难度:一星级(简单题) 今天闲来无事,决定刷一道水题.结果发现这道水题居然把我卡了将近一个钟头 ...

  4. hihoCoder #1174:拓扑排序&#183;一

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 因为今天上课的老师讲的特别无聊.小Hi和小Ho偷偷地聊了起来. 小Ho:小Hi ...

  5. [hihoCoder] 第四十八周: 拓扑排序·二

    题目1 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当 ...

  6. hihoCoder #1185 : 连通性·三(强联通分量+拓扑排序)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  7. hihocoder 1457(后缀自动机+拓扑排序)

    题意 给定若干组由数字构成的字符串,求所有不重复子串的和(把他们看成十进制),答案mod(1e9+7) 题解: 类似后缀数组的做法,把字符串之间用':'连接,这里用':'是因为':'的ascii码恰好 ...

  8. hihoCoder#1175拓扑排序应用

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当然小Hi和小Ho也参与到了 ...

  9. hihoCoder #1175 : 拓扑排序&#183;二

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立马引 ...

随机推荐

  1. Python 使用 UTF-8 编码(转)

    Python 使用 UTF-8 编码(转) 原文出处:http://blog.chenlb.com/2010/01/python-use-utf-8.html 一般我喜欢用 utf-8 编码,在 py ...

  2. Bootstrap学习笔记面板(Panels)

    本文将讲解Bootstrap面板(Panels).面板组件用于把DOM组件插入到一个盒子中.创建一个基本的面板,只需要向div元素添加class .panel和 panel-default即可,如下面 ...

  3. canvas学习笔记(上篇)-- canvas入门教程 -- canvas标签/方块/描边/路径/圆形/曲线

    [上篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

  4. Atitit. 脚本语言的断点单步调试的设计与实现 attialx 总结 php 参照java

    Atitit. 脚本语言的断点单步调试的设计与实现 attialx 总结 php 参照java 1. 断点的实现:手动断点 die和exit是等价的 1 2. 变量表的实现 1 3. print_r( ...

  5. phpMyAdmin安装教程

    phpMyAdmin安装教程: 解压:将下载文件解压缩到 WEB 访问路径下.文件目录如phpmyadmin. 配置文件:然后配置目录下libraries文件下的 config.default.php ...

  6. Java遍历包中所有类

    PackageUtil 类 import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java. ...

  7. poj Sudoku(数独) DFS

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13665   Accepted: 6767   Special ...

  8. [转]UI、GUI、UE、UX、ID、UED、UCD的区别

    UI (User Interface):用户界面 UE (User Experience):用户体验 ID (Interaction design):交互设计 UID (User Interface ...

  9. traceroute/tracert--获取网络路由路径

    traceroute 是用来检测发出数据包的主机到目标主机之间所经过的网关数量的工具.traceroute 的原理是试图以最小的TTL发出探测包来跟踪数据包到达目标主机所经过的网关,然后监听一个来自网 ...

  10. Flex桌面AIR软件日志添加

    日志包装类 package log { import com.adobe.air.logging.FileTarget; import flash.filesystem.File; import fl ...