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. linux下各种形式的shell加法操作总结

    linux 下shell加法操作总结: #!/bin/bash   n=1;echo -n "$n "   let "n = $n + 1" echo -n & ...

  2. 微信小程序挑一挑辅助

    1.窗体 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...

  3. C#指南,重温基础,展望远方!(6)C#类和对象

    类是最基本的 C# 类型. 类是一种数据结构,可在一个单元中就将状态(字段)和操作(方法和其他函数成员)结合起来. 类为动态创建的类实例(亦称为“对象”)提供了定义. 类支持继承和多形性,即派生类可以 ...

  4. mysql-shell的安装和使用

    mysql-shell是一个高级的mysql命令行工具.它直接两种模式(交互式&批处理式)三种语言(javascript\python\sql) 1.下载地址 https://dev.mysq ...

  5. 利用dd命令制作u盘iso镜像

    现在安装系统都是用u盘安装,那么制作u盘的iso镜像就是必须的了.现在此类工具倒是不少,但是,好用的不多,有的还收费.唉,还是用dd吧,老配方,老味道. 首先:要df -h一下,看看u盘的盘符,类似  ...

  6. CentOS6.2下安装中文输入法

    因为在程序中需要输入中文,但是系统没有预装中文输入法,所以就安装一下,顺便记录 1.用root登录 ,或su root2.yum install "@Chinese Support" ...

  7. H3C-WA2210升级

    前几天升级了2210固件,做个记录 首先吐槽一点:华为官方给出的公共下载账号yx800根本没用..下不了官方提供的固件资料 组网准备: 升级之前要先看看原机版本(dis ver): 在升级版本之前,请 ...

  8. struts-config message-resources配置问题总结

    问题:我的app无法读取配置好的ApplicationResources.properties中的内容 解答:文件目录为 /webapp /WEB-INF /classes ApplicationRe ...

  9. 关于EasyUI的Layout总结

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.layout以html标签方式建立的 <div id="content" region="center&q ...

  10. linux系统中-E,-S,-c的区别和作用(怎么讲代码转化为机器识别的语言)

    1707 许多初学者都有比较大的疑惑,电脑是怎么识别我们写的代码并进行处理的呢?其实这个问题对我们初学者来说是很重要的,只有了解机器的运行原理,我们才能真正地学号留下.那么今天我就以此为题为大家略讲一 ...