Skip to content

Commit dd92a30

Browse files
committed
mv topo
1 parent 6788bb5 commit dd92a30

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

.vscode/settings.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"atomic": "cpp",
5+
"bit": "cpp",
6+
"*.tcc": "cpp",
7+
"cctype": "cpp",
8+
"clocale": "cpp",
9+
"cmath": "cpp",
10+
"compare": "cpp",
11+
"concepts": "cpp",
12+
"cstdarg": "cpp",
13+
"cstddef": "cpp",
14+
"cstdint": "cpp",
15+
"cstdio": "cpp",
16+
"cstdlib": "cpp",
17+
"ctime": "cpp",
18+
"cwchar": "cpp",
19+
"cwctype": "cpp",
20+
"deque": "cpp",
21+
"set": "cpp",
22+
"string": "cpp",
23+
"unordered_map": "cpp",
24+
"vector": "cpp",
25+
"exception": "cpp",
26+
"algorithm": "cpp",
27+
"functional": "cpp",
28+
"iterator": "cpp",
29+
"memory": "cpp",
30+
"memory_resource": "cpp",
31+
"numeric": "cpp",
32+
"random": "cpp",
33+
"string_view": "cpp",
34+
"system_error": "cpp",
35+
"tuple": "cpp",
36+
"type_traits": "cpp",
37+
"utility": "cpp",
38+
"initializer_list": "cpp",
39+
"iomanip": "cpp",
40+
"iosfwd": "cpp",
41+
"iostream": "cpp",
42+
"istream": "cpp",
43+
"limits": "cpp",
44+
"new": "cpp",
45+
"numbers": "cpp",
46+
"ostream": "cpp",
47+
"sstream": "cpp",
48+
"stdexcept": "cpp",
49+
"streambuf": "cpp",
50+
"typeinfo": "cpp",
51+
"valarray": "cpp"
52+
}
53+
}

cpp/graph_util.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,28 @@ template <typename Cost = int>
9292
bool is_tree(const Graph<Cost>& graph) {
9393
return graph.m == graph.n - 1 && is_connected(graph);
9494
}
95+
96+
/**
97+
* @brief 有向グラフをトポロジカルソートする
98+
* @param G トポロジカルソートするグラフ
99+
* @return ソートされたノード番号のvector DAGでなければ長さがG.n未満になる
100+
*/
101+
template<typename Cost>
102+
std::vector<int> topological_sort(const Graph<Cost> &G) {
103+
std::vector<int> indeg(G.n), sorted;
104+
std::queue<int> q;
105+
for (int i = 0; i < G.n; i++) {
106+
for (int dst : G[i]) indeg[dst]++;
107+
}
108+
for (int i = 0; i < G.n; i++) {
109+
if (!indeg[i]) q.push(i);
110+
}
111+
while (!q.empty()) {
112+
int cur = q.front(); q.pop();
113+
for (int dst : G[cur]) {
114+
if (!--indeg[dst]) q.push(dst);
115+
}
116+
sorted.push_back(cur);
117+
}
118+
return sorted;
119+
}

test/atcoder-edpc-g.test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <iostream>
44
#include <algorithm>
55

6-
#include "../cpp/graph.hpp"
6+
#include "../cpp/graph_util.hpp"
77

88
int main(void){
99

@@ -12,8 +12,8 @@ int main(void){
1212
Graph G(N);
1313
G.read(M, -1, false, true);
1414

15-
std::vector<int> dist(N);
16-
for(int x : G.topological_sort()){
15+
std::vector<int> dist(N), ord(topological_sort(G));
16+
for(int x : ord){
1717
for(int y : G[x]) dist[y] = std::max(dist[y], dist[x] + 1);
1818
}
1919
std::cout << *std::max_element(dist.begin(), dist.end()) << std::endl;

0 commit comments

Comments
 (0)