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. Java程序猿的JavaScript学习笔记(10—— jQuery-在“类”层面扩展)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  2. memcache基础知识-stats参数

    安装memcache: #tar -xvf libevent-1.4.13-stable.tar.gz#cd libevent-1.4.13-stable#./configure && ...

  3. zookeeper(二):linux centos下安装zookeeper(单机和集群)

    下载 http://zookeeper.apache.org/releases.html 解压 tar –zxvf zookeeper-3.4.6.tar.gz 解压文件到"/usr/loc ...

  4. PCIe to AXI Translation——PCIe 内存空间到AXI内存空间的转换

    PCIe to AXI Translation——PCIe 内存空间到AXI内存空间的转换 UltraScale系列芯片包含PCIe的Gen3 Integrated Block IP核在内的多种不同功 ...

  5. Pedometer_forAndroid

    https://github.com/Nicky213Zhang/Pedometer_forAndroid 自行封装了一个计步器控件,采用:计步传感器Sensor.TYPE_STEP_COUNTER计 ...

  6. ubuntu 安装python3.5

    wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz xz -d Python-.tar.xz .tar cd Python ...

  7. vs2010 MSDN文档安装方法

    vs2010的MSDN是不能独立安装,必须安装VS2010后才能安装. 安装方法: 1.vs2010的ISO光盘文件中,里面会有个ProductDocumentation文件夹,其实这个就是安装MSD ...

  8. 跟着百度学PHP[14]-PDO之Mysql的事务处理1

    事务处理:在实际案例当中干一件事的mysql语句(好比转账,小一同学转账100,小二同学收账,在mysql当中小一就要减去转账的钱,小二就要增加100快)倘若该语句执行过程中有任何一条的sql语句出错 ...

  9. golang https server分析

    https: HTTPS是http安全版本的实现,在http与tcp之间加了一层ssl/tls安全传输协议 为了防止请求被监听.篡改.冒充,在tls实现过程中引入了数字证书机制,数字证书由第三方权威机 ...

  10. JAVA设置HttpOnly Cookies

    HttpOnly Cookies是一个cookie安全行的解决方案. 在支持HttpOnly cookies的浏览器中(IE6+,FF3.0+),如果在Cookie中设置了"HttpOnly ...