Skip to content

Commit c101b39

Browse files
committed
Scanner is no longer recursive. Const-size vectors replaced by Box<[]>
1 parent 606ccd6 commit c101b39

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
language: rust
22
rust:
3+
- 1.31.0 # Version currently supported by Codeforces
4+
- stable
35
- beta
46
- nightly
57
matrix:
68
allow_failures:
79
- rust: nightly
10+
fast_finish: true

src/graph/connectivity.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use super::Graph;
55
/// ConnectivityGraph's constructor.
66
struct ConnectivityData {
77
time: usize,
8-
vis: Vec<usize>,
9-
low: Vec<usize>,
8+
vis: Box<[usize]>,
9+
low: Box<[usize]>,
1010
v_stack: Vec<usize>,
1111
e_stack: Vec<usize>,
1212
}
@@ -15,8 +15,8 @@ impl ConnectivityData {
1515
fn new(num_v: usize) -> Self {
1616
Self {
1717
time: 0,
18-
vis: vec![0; num_v],
19-
low: vec![0; num_v],
18+
vis: vec![0; num_v].into_boxed_slice(),
19+
low: vec![0; num_v].into_boxed_slice(),
2020
v_stack: Vec::new(),
2121
e_stack: Vec::new(),
2222
}
@@ -77,20 +77,20 @@ impl<'a> ConnectivityGraph<'a> {
7777
for u in 0..graph.num_v() {
7878
if data.vis[u] == 0 {
7979
if is_directed {
80-
connect.scc(u, &mut data);
80+
connect.scc(&mut data, u);
8181
} else {
82-
connect.bcc(u, graph.num_e() + 1, &mut data);
82+
connect.bcc(&mut data, u, graph.num_e() + 1);
8383
}
8484
}
8585
}
8686
connect
8787
}
8888

89-
fn scc(&mut self, u: usize, data: &mut ConnectivityData) {
89+
fn scc(&mut self, data: &mut ConnectivityData, u: usize) {
9090
data.visit(u);
9191
for (_, v) in self.graph.adj_list(u) {
9292
if data.vis[v] == 0 {
93-
self.scc(v, data);
93+
self.scc(data, v);
9494
}
9595
if self.cc[v] == 0 {
9696
data.lower(u, data.low[v]);
@@ -122,20 +122,20 @@ impl<'a> ConnectivityGraph<'a> {
122122
}).collect()
123123
}
124124

125-
/// Gets the vertices of a directed acyclic graph (DAG) in topological
126-
/// order. Undefined behavior if the graph is not a DAG.
125+
/// Gets the vertices of a graph according to a topological order of the
126+
/// strongly connected components. Most often used on DAGs.
127127
pub fn topological_sort(&self) -> Vec<usize> {
128128
let mut vertices = (0..self.graph.num_v()).collect::<Vec<_>>();
129129
vertices.sort_unstable_by_key(|&u| self.num_cc - self.cc[u]);
130130
vertices
131131
}
132132

133-
fn bcc(&mut self, u: usize, par: usize, data: &mut ConnectivityData) {
133+
fn bcc(&mut self, data: &mut ConnectivityData, u: usize, par: usize) {
134134
data.visit(u);
135135
for (e, v) in self.graph.adj_list(u) {
136136
if data.vis[v] == 0 {
137137
data.e_stack.push(e);
138-
self.bcc(v, e, data);
138+
self.bcc(data, v, e);
139139
data.lower(u, data.low[v]);
140140
if data.vis[u] <= data.low[v] {
141141
// u is a cut vertex unless it's a one-child root

src/scanner.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ impl<B: io::BufRead> Scanner<B> {
2525
where
2626
T::Err: ::std::fmt::Debug,
2727
{
28-
if let Some(front) = self.buffer.pop() {
29-
front.parse::<T>().expect(&front)
30-
} else {
28+
loop {
29+
if let Some(front) = self.buffer.pop() {
30+
return front.parse::<T>().expect(&front);
31+
}
3132
let mut input = String::new();
3233
self.reader.read_line(&mut input).expect("Line not read");
3334
self.buffer = input.split_whitespace().rev().map(String::from).collect();
34-
self.read()
3535
}
3636
}
3737
}

src/string_proc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct SuffixArray {
6363

6464
impl SuffixArray {
6565
/// O(n + max_key) stable sort on the items generated by vals.
66-
// Items v in vals are sorted according to val_to_key[v].
66+
/// Items v in vals are sorted according to val_to_key[v].
6767
fn counting_sort(
6868
vals: impl Iterator<Item = usize> + Clone,
6969
val_to_key: &[usize],

0 commit comments

Comments
 (0)