diff --git a/examples/practice2_a_disjoint_set_union.rs b/examples/practice2_a_disjoint_set_union.rs new file mode 100644 index 0000000..ea25de5 --- /dev/null +++ b/examples/practice2_a_disjoint_set_union.rs @@ -0,0 +1,24 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_a +use ac_library_rs::Dsu; +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), + _ => {} + } + } +} diff --git a/examples/practice2_b_fenwick_tree.rs b/examples/practice2_b_fenwick_tree.rs new file mode 100644 index 0000000..2a003f6 --- /dev/null +++ b/examples/practice2_b_fenwick_tree.rs @@ -0,0 +1,32 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_b +use ac_library_rs::FenwickTree; +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::::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)); + } + _ => {} + } + } +} diff --git a/examples/practice2_c_floor_sum.rs b/examples/practice2_c_floor_sum.rs new file mode 100644 index 0000000..78f5657 --- /dev/null +++ b/examples/practice2_c_floor_sum.rs @@ -0,0 +1,17 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_c +use ac_library_rs::floor_sum; +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)); + } +} diff --git a/examples/practice2_e_mincostflow.rs b/examples/practice2_e_mincostflow.rs new file mode 100644 index 0000000..c8a237c --- /dev/null +++ b/examples/practice2_e_mincostflow.rs @@ -0,0 +1,44 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_e +use ac_library_rs::MinCostFlowGraph; +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> = (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)); +} diff --git a/examples/practice2_f_convolution.rs b/examples/practice2_f_convolution.rs new file mode 100644 index 0000000..7cb3276 --- /dev/null +++ b/examples/practice2_f_convolution.rs @@ -0,0 +1,39 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_f +use ac_library_rs::{convolution, modint::ModInt998244353 as Mint}; +use std::io::prelude::*; + +pub fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + let n: usize = input.next().unwrap().parse().unwrap(); + let m: usize = input.next().unwrap().parse().unwrap(); + let a: Vec = input + .by_ref() + .take(n) + .map(str::parse) + .map(Result::unwrap) + .collect(); + let b: Vec = input + .by_ref() + .take(m) + .map(str::parse) + .map(Result::unwrap) + .collect(); + + print_oneline(convolution::convolution(&a, &b)); +} + +fn print_oneline, T: std::fmt::Display>(values: I) { + let out = std::io::stdout(); + let mut out = std::io::BufWriter::new(out.lock()); + for (i, v) in values.into_iter().enumerate() { + if i == 0 { + write!(&mut out, "{}", v).unwrap(); + } else { + write!(&mut out, " {}", v).unwrap(); + } + } + writeln!(&mut out).unwrap(); +} diff --git a/examples/practice2_g_scc.rs b/examples/practice2_g_scc.rs new file mode 100644 index 0000000..aa67431 --- /dev/null +++ b/examples/practice2_g_scc.rs @@ -0,0 +1,27 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_g +use ac_library_rs::SccGraph; +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!(); + } +} diff --git a/examples/practice2_h_two_sat.rs b/examples/practice2_h_two_sat.rs new file mode 100644 index 0000000..b80b597 --- /dev/null +++ b/examples/practice2_h_two_sat.rs @@ -0,0 +1,36 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_h +use ac_library_rs::TwoSat; +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::>(); + + 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"); + } +} diff --git a/examples/practice2_i_number_of_substrings.rs b/examples/practice2_i_number_of_substrings.rs new file mode 100644 index 0000000..50806f1 --- /dev/null +++ b/examples/practice2_i_number_of_substrings.rs @@ -0,0 +1,17 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_i +use ac_library_rs::{lcp_array, suffix_array}; +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); +}