Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample code for the practice contest #52

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
23 changes: 23 additions & 0 deletions examples/practice2_a_disjoint_set_union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use ac_library_rs::Dsu;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let mut dsu = Dsu::new(n);
for _ in 0..input.next().unwrap().parse().unwrap() {
let t = input.next().unwrap().parse().unwrap();
let u = input.next().unwrap().parse().unwrap();
let v = input.next().unwrap().parse().unwrap();
match t {
0 => {
dsu.merge(u, v);
}
1 => println!("{}", dsu.same(u, v) as i32),
_ => {}
}
}
}
31 changes: 31 additions & 0 deletions examples/practice2_b_fenwick_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use ac_library_rs::FenwickTree;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let q = input.next().unwrap().parse().unwrap();
let mut tree = FenwickTree::<u64>::new(n, 0);
for i in 0..n {
let a: u64 = input.next().unwrap().parse().unwrap();
tree.add(i, a);
}
for _ in 0..q {
match input.next().unwrap().parse().unwrap() {
0 => {
let p = input.next().unwrap().parse().unwrap();
let x: u64 = input.next().unwrap().parse().unwrap();
tree.add(p, x);
}
1 => {
let l = input.next().unwrap().parse().unwrap();
let r = input.next().unwrap().parse().unwrap();
println!("{}", tree.sum(l, r));
}
_ => {}
}
}
}
16 changes: 16 additions & 0 deletions examples/practice2_c_floor_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ac_library_rs::floor_sum;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

for _ in 0..input.next().unwrap().parse().unwrap() {
let n = input.next().unwrap().parse().unwrap();
let m = input.next().unwrap().parse().unwrap();
let a = input.next().unwrap().parse().unwrap();
let b = input.next().unwrap().parse().unwrap();
println!("{}", floor_sum(n, m, a, b));
}
}
43 changes: 43 additions & 0 deletions examples/practice2_e_mincostflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use ac_library_rs::MinCostFlowGraph;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

const MAX: i64 = 1_000_000_000;

#[allow(clippy::needless_range_loop)]
fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let k = input.next().unwrap().parse().unwrap();
let a: Vec<Vec<i64>> = (0..n)
.map(|_| input.by_ref().take(n).map(|s| s.parse().unwrap()).collect())
.collect();

let mut graph = MinCostFlowGraph::new(102);
for i in 0..n {
for j in 0..n {
graph.add_edge(i, 50 + j, 1, MAX - a[i][j]);
}
}
for i in 0..n {
graph.add_edge(100, i, k, 0);
graph.add_edge(50 + i, 101, k, 0);
}
graph.add_edge(100, 101, n as i64 * k, MAX);

let (max_flow, min_cost) = graph.flow(100, 101, n as i64 * k);
println!("{}", max_flow * MAX - min_cost);

(0..n)
.map(|i| {
(0..n)
.map(|j| match graph.get_edge(i * n + j).flow {
1 => 'X',
_ => '.',
})
.collect()
})
.for_each(|s: String| println!("{}", s));
}
26 changes: 26 additions & 0 deletions examples/practice2_g_scc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use ac_library_rs::SccGraph;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let m = input.next().unwrap().parse().unwrap();
let mut graph = SccGraph::new(n);
for _ in 0..m {
let a = input.next().unwrap().parse().unwrap();
let b = input.next().unwrap().parse().unwrap();
graph.add_edge(a, b);
}
let scc = graph.scc();
println!("{}", scc.len());
for cc in scc {
print!("{}", cc.len());
for v in cc {
print!(" {}", v);
}
println!();
}
}
35 changes: 35 additions & 0 deletions examples/practice2_h_two_sat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use ac_library_rs::TwoSat;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let d = input.next().unwrap().parse().unwrap();
let xs = (0..2 * n)
.map(|_| input.next().unwrap().parse().unwrap())
.collect::<Vec<i32>>();

let mut sat = TwoSat::new(2 * n);
for i in 0..2 * n {
sat.add_clause(i, i % 2 == 0, i ^ 1, i % 2 == 0);
}
for (i, x) in xs.iter().enumerate() {
for (j, y) in xs[..i].iter().enumerate() {
if (x - y).abs() < d {
sat.add_clause(i, false, j, false);
}
}
}
if sat.satisfiable() {
println!("Yes");
let ans = sat.answer();
for i in 0..n {
println!("{}", xs[2 * i + ans[2 * i + 1] as usize]);
}
} else {
println!("No");
}
}
16 changes: 16 additions & 0 deletions examples/practice2_i_number_of_substrings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ac_library_rs::{lcp_array, suffix_array};
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;
use std::iter;

fn main() {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
let s = s.trim();
let suffix_array = suffix_array(s);
let ans: u64 = iter::once(0)
.chain(lcp_array(s, &suffix_array))
.zip(suffix_array)
.map(|(c, i)| (s.len() - i - c) as u64)
.sum();
println!("{}", ans);
}