[hihoCoder] 拓扑排序·一
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] 拓扑排序·一的更多相关文章
- hihoCoder #1457 : 后缀自动机四·重复旋律7(后缀自动机 + 拓扑排序)
http://hihocoder.com/problemset/problem/1457 val[i] 表示状态i所表示的所有字符串的十进制之和 ans= ∑ val[i]在后缀自动机上,从起始状态走 ...
- hihocoder 1343 : Stable Members【拓扑排序】
hihocoder #1343:题目 解释:一个学习小组,一共有N个学员,一个主管.每个学员都有自己的导师(一个或者多个),导师可以是其他学员也可以是主管.每周学员都要把自己的学习报告和收到的报告提交 ...
- hihoCoder 1175:拓扑排序二
题目链接: http://hihocoder.com/problemset/problem/1175 题目难度:一星级(简单题) 今天闲来无事,决定刷一道水题.结果发现这道水题居然把我卡了将近一个钟头 ...
- hihoCoder #1174:拓扑排序·一
[题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 因为今天上课的老师讲的特别无聊.小Hi和小Ho偷偷地聊了起来. 小Ho:小Hi ...
- [hihoCoder] 第四十八周: 拓扑排序·二
题目1 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当 ...
- hihoCoder #1185 : 连通性·三(强联通分量+拓扑排序)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- hihocoder 1457(后缀自动机+拓扑排序)
题意 给定若干组由数字构成的字符串,求所有不重复子串的和(把他们看成十进制),答案mod(1e9+7) 题解: 类似后缀数组的做法,把字符串之间用':'连接,这里用':'是因为':'的ascii码恰好 ...
- hihoCoder#1175拓扑排序应用
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论,当然小Hi和小Ho也参与到了 ...
- hihoCoder #1175 : 拓扑排序·二
[题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立马引 ...
随机推荐
- WebApi接口传参不再困惑(4):传参详解
前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料.如今,使用WebApi也有段时间了,今天就记录下API接口传参的一些方式方法,算是一个笔记,也希望能帮初学者少走弯路.本 ...
- 怎样防止应用因获取IDFA被AppStore拒绝
由于Appstore禁止不使用广告而採集IDFA的app上架,友盟提供IDFA版和不含IDFA版两个SDK,两个SDK在数据上并没有差异.採集IDFA是为了防止今后由于苹果可能禁止眼下使用的openu ...
- 监听用户的后退键,解决部分浏览器回退的bug
监听用户的后退键,解决部分浏览器回退的bug $(document).keydown(function (event) { // 监听backspace键 if (event.keyCode == 8 ...
- RESTORE DATABASE命令还原SQLServer 数据库 bak
今天在sqlServer20005 的management studio里使用bak文件还原数据库的时候,总是失败!Restore failed for Server 'ADANDELI'. (Mi ...
- 多线程-synchronized
引言 synchronized是Java线程同步中的一个重要的概念,synchronized是独占锁(互斥锁),同时也是可重入锁(可重入锁一定程度上避免了死锁的问题,内部是关联一个计数器,加一次锁计数 ...
- Atitit.隔行换色 变色 css3 结构性伪类选择器
Atitit.隔行换色 变色 css3 结构性伪类选择器 1.1. css3隔行换色扩展阅读 1 1.2. 结构伪选择器 1 1.3. jQuery 选择器2 1.1. css3隔行换色扩展阅读 原 ...
- POJ--3529--Wormholes
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36889 Accepted: 13520 Descr ...
- 修正ECMALL在PHP5.3以上版本中无法开启支付方式的BUG
修正ECMALL在PHP5.3以上版本中无法开启支付方式的BUG 很多用户反映说PHP5.3.3下,ECMALL的商家无法安装支付方式,这个是比较严重的事情,不能安装支付方式那什么都不能干呢,那我就免 ...
- java虚拟机的逃逸分析
逃逸分析作为其他优化手段提供依据的分析技术,其基本行为就是分析对象动态作用域:当一个对象在方法中被定义后,它可能被外部方法所引用,例如作为调用参数传递到其他方法中,称为方法逃逸.甚至还有可能被外部线程 ...
- [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PAnsiChar
Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessAnsi(pvData: P ...