File tree Expand file tree Collapse file tree 3 files changed +81
-3
lines changed Expand file tree Collapse file tree 3 files changed +81
-3
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -92,3 +92,28 @@ template <typename Cost = int>
92
92
bool is_tree (const Graph<Cost>& graph) {
93
93
return graph.m == graph.n - 1 && is_connected (graph);
94
94
}
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
+ }
Original file line number Diff line number Diff line change 3
3
#include < iostream>
4
4
#include < algorithm>
5
5
6
- #include " ../cpp/graph .hpp"
6
+ #include " ../cpp/graph_util .hpp"
7
7
8
8
int main (void ){
9
9
@@ -12,8 +12,8 @@ int main(void){
12
12
Graph G (N);
13
13
G.read (M, -1 , false , true );
14
14
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 ){
17
17
for (int y : G[x]) dist[y] = std::max (dist[y], dist[x] + 1 );
18
18
}
19
19
std::cout << *std::max_element (dist.begin (), dist.end ()) << std::endl;
You can’t perform that action at this time.
0 commit comments