[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上立马引 ...
随机推荐
- JS关于scrollTop和可视区域clientWidth
一.scrollTop,scrollLeft 要获得页面的scrollTop及scrollLeft,在不同的浏览器中是不一样的: 谷歌浏览器和没声明DTD的文档,通过document.body.scr ...
- C#音视频网络流解码:H264视频和ACC音频
下面两种方式是直接翻译过来的,还有问题,比如指针的使用和值的传入.考虑C#和C++的差异,还是要抱着怀疑的态度去看待,不一定是对的. H264视频解码网络流: using FFmpeg.AutoGen ...
- ubuntu环境变量及其配置
Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的环境变量.所以管理环境变量的文件也分为系统级和用户级的. ...
- 实战Nginx负载均衡高冗余高可用WEB架构
最近公司主力网站之一改版完成终于上线了,牵扯了我大半年的时间,现在终于有时间坐下来写点东西,总结沉淀一下自己的技术心得.此次,根据服务器的数量和质量,我采用负载均衡高冗余的架构,考虑单点故障,W ...
- springboot页面缓存和url缓存实例
@Autowired RedisService redisService; @Autowired GoodsService goodsService; @Autowired ThymeleafView ...
- Eclipse中屏蔽日志
如何在Eclipse中屏蔽日志 //屏蔽日志 Eclipse Java import org.apache.log4j.Level; import org.apache.log4j.Logger; L ...
- Atitti. 语法树AST、后缀表达式、DAG、三地址代码
Atitti. 语法树AST.后缀表达式.DAG.三地址代码 抽象语法树的观点认为任何复杂的语句嵌套情况都可以借助于树的形式加以描述.确实,不得不承认应用抽象语法树可以使语句翻译变得相对容易,它很好地 ...
- 代码覆盖率?coverage
http://www.infoq.com/cn/articles/test-coverage-rate-role 代码覆盖率?coverage http://www.cnblogs.com/cod ...
- makefile之call函数
call函数是唯一一个可以创建定制化参数函数的引用函数. 支持对自定义函数的引用; 支持将一个变量定义为一个复杂的表达式,用call函数根据不同的参数对它进行展开来获取不同的结果; 函数语法: $(c ...
- tensorflow之损失函数
#coding:utf-8 __author__ = 'similarface' import tensorflow as tf sess=tf.Session() #max(features, 0) ...