QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#658701#9490. Sub Bracketsucup-team296#AC ✓2ms3020kbRust43.0kb2024-10-19 17:24:562024-10-19 17:24:56

Judging History

你现在查看的是最新测评结果

  • [2024-10-19 17:24:56]
  • 评测
  • 测评结果:AC
  • 用时:2ms
  • 内存:3020kb
  • [2024-10-19 17:24:56]
  • 提交

answer

// https://contest.ucup.ac/contest/1812/problem/9490
pub mod solution {
//{"name":"O. Sub Brackets","group":"Universal Cup - The 3rd Universal Cup. Stage 13: Sendai","url":"https://contest.ucup.ac/contest/1812/problem/9490","interactive":false,"timeLimit":1000,"tests":[{"input":"5 3\n1 2\n4 5\n2 5\n","output":"2\n"},{"input":"2 4\n1 2\n1 2\n1 2\n1 2\n","output":"4\n"},{"input":"32 11\n25 32\n19 32\n11 24\n20 31\n22 25\n21 26\n17 22\n30 31\n23 28\n4 15\n19 22\n","output":"8\n"}],"testType":"single","input":{"type":"stdin","fileName":null,"pattern":null},"output":{"type":"stdout","fileName":null,"pattern":null},"languages":{"java":{"taskClass":"OSubBrackets"}}}

use crate::algo_lib::graph::edges::flow_edge::FlowEdge;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::graph::max_flow::MaxFlow;
use crate::algo_lib::io::input::Input;
use crate::algo_lib::io::output::Output;
use crate::algo_lib::misc::extensions::do_with::DoWith;
use crate::algo_lib::misc::test_type::TaskType;

use crate::algo_lib::misc::test_type::TestType;

type PreCalc = ();

fn solve(input: &mut Input, out: &mut Output, _test_case: usize, _data: &mut PreCalc) {
    let _n = input.read_size();
    let m = input.read_size();
    let segments = input.read_size_pair_vec(m);

    let mut graph = Graph::new(m + 2).do_with(|graph| {
        for i in 0..m {
            if segments[i].0 % 2 == 0 {
                graph.add_edge(FlowEdge::new(m, i, 1));
                for j in 0..m {
                    if segments[j].0 % 2 == 1
                        && (segments[i].0 <= segments[j].0
                            && segments[j].0 <= segments[i].1
                            && segments[i].1 <= segments[j].1
                            || segments[j].0 <= segments[i].0
                                && segments[i].0 <= segments[j].1
                                && segments[j].1 <= segments[i].1)
                    {
                        graph.add_edge(FlowEdge::new(i, j, 1));
                    }
                }
            } else {
                graph.add_edge(FlowEdge::new(i, m + 1, 1));
            }
        }
    });
    out.print_line(m - graph.max_flow(m, m + 1));
}

pub static TEST_TYPE: TestType = TestType::Single;
pub static TASK_TYPE: TaskType = TaskType::Classic;

pub(crate) fn run(mut input: Input, mut output: Output) -> bool {
    let mut pre_calc = ();

    match TEST_TYPE {
        TestType::Single => solve(&mut input, &mut output, 1, &mut pre_calc),
        TestType::MultiNumber => {
            let t = input.read();
            for i in 1..=t {
                solve(&mut input, &mut output, i, &mut pre_calc);
            }
        }
        TestType::MultiEof => {
            let mut i = 1;
            while input.peek().is_some() {
                solve(&mut input, &mut output, i, &mut pre_calc);
                i += 1;
            }
        }
    }
    output.flush();
    match TASK_TYPE {
        TaskType::Classic => input.is_empty(),
        TaskType::Interactive => true,
    }
}

}
pub mod algo_lib {
pub mod collections {
pub mod dsu {
use crate::algo_lib::collections::iter_ext::collect::IterCollect;
use crate::algo_lib::collections::slice_ext::bounds::Bounds;
use crate::algo_lib::collections::slice_ext::legacy_fill::LegacyFill;
use std::cell::Cell;

#[derive(Clone)]
pub struct DSU {
    id: Vec<Cell<u32>>,
    size: Vec<u32>,
    count: usize,
}

impl DSU {
    pub fn new(n: usize) -> Self {
        Self {
            id: (0..n).map(|i| Cell::new(i as u32)).collect_vec(),
            size: vec![1; n],
            count: n,
        }
    }

    pub fn size(&self, i: usize) -> usize {
        self.size[self.get(i)] as usize
    }

    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.id.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
        self.id.iter().enumerate().filter_map(|(i, id)| {
            if (i as u32) == id.get() {
                Some(i)
            } else {
                None
            }
        })
    }

    pub fn set_count(&self) -> usize {
        self.count
    }

    pub fn join(&mut self, mut a: usize, mut b: usize) -> bool {
        a = self.get(a);
        b = self.get(b);
        if a == b {
            false
        } else {
            self.size[a] += self.size[b];
            self.id[b].replace(a as u32);
            self.count -= 1;
            true
        }
    }

    pub fn get(&self, i: usize) -> usize {
        if self.id[i].get() != i as u32 {
            let res = self.get(self.id[i].get() as usize);
            self.id[i].replace(res as u32);
        }
        self.id[i].get() as usize
    }

    pub fn clear(&mut self) {
        self.count = self.id.len();
        self.size.legacy_fill(1);
        self.id.iter().enumerate().for_each(|(i, id)| {
            id.replace(i as u32);
        });
    }

    pub fn parts(&self) -> Vec<Vec<usize>> {
        let roots = self.iter().collect_vec();
        let mut res = vec![Vec::new(); roots.len()];
        for i in 0..self.id.len() {
            res[roots.as_slice().bin_search(&self.get(i)).unwrap()].push(i);
        }
        res
    }
}
}
pub mod iter_ext {
pub mod collect {
pub trait IterCollect<T>: Iterator<Item = T> + Sized {
    fn collect_vec(self) -> Vec<T> {
        self.collect()
    }
}

impl<T, I: Iterator<Item = T> + Sized> IterCollect<T> for I {}
}
}
pub mod slice_ext {
pub mod bounds {
pub trait Bounds<T: PartialOrd> {
    fn lower_bound(&self, el: &T) -> usize;
    fn upper_bound(&self, el: &T) -> usize;
    fn bin_search(&self, el: &T) -> Option<usize>;
    fn more(&self, el: &T) -> usize;
    fn more_or_eq(&self, el: &T) -> usize;
    fn less(&self, el: &T) -> usize;
    fn less_or_eq(&self, el: &T) -> usize;
}

impl<T: PartialOrd> Bounds<T> for [T] {
    fn lower_bound(&self, el: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = left + ((right - left) >> 1);
            if &self[mid] < el {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        left
    }

    fn upper_bound(&self, el: &T) -> usize {
        let mut left = 0;
        let mut right = self.len();
        while left < right {
            let mid = left + ((right - left) >> 1);
            if &self[mid] <= el {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        left
    }

    fn bin_search(&self, el: &T) -> Option<usize> {
        let at = self.lower_bound(el);
        if at == self.len() || &self[at] != el {
            None
        } else {
            Some(at)
        }
    }

    fn more(&self, el: &T) -> usize {
        self.len() - self.upper_bound(el)
    }

    fn more_or_eq(&self, el: &T) -> usize {
        self.len() - self.lower_bound(el)
    }

    fn less(&self, el: &T) -> usize {
        self.lower_bound(el)
    }

    fn less_or_eq(&self, el: &T) -> usize {
        self.upper_bound(el)
    }
}
}
pub mod legacy_fill {
// 1.50
pub trait LegacyFill<T> {
    fn legacy_fill(&mut self, val: T);
}

impl<T: Clone> LegacyFill<T> for [T] {
    fn legacy_fill(&mut self, val: T) {
        for el in self.iter_mut() {
            *el = val.clone();
        }
    }
}
}
}
pub mod vec_ext {
pub mod default {
pub fn default_vec<T: Default>(len: usize) -> Vec<T> {
    let mut v = Vec::with_capacity(len);
    for _ in 0..len {
        v.push(T::default());
    }
    v
}
}
}
}
pub mod graph {
pub mod edges {
pub mod bi_edge {
use crate::algo_lib::graph::edges::bi_edge_trait::BiEdgeTrait;
use crate::algo_lib::graph::edges::edge_id::EdgeId;
use crate::algo_lib::graph::edges::edge_id::NoId;
use crate::algo_lib::graph::edges::edge_id::WithId;
use crate::algo_lib::graph::edges::edge_trait::BidirectionalEdgeTrait;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;

#[derive(Clone)]
pub struct BiEdgeRaw<Id: EdgeId, P> {
    to: u32,
    id: Id,
    payload: P,
}

impl<Id: EdgeId> BiEdgeRaw<Id, ()> {
    pub fn new(from: usize, to: usize) -> (usize, Self) {
        (
            from,
            Self {
                to: to as u32,
                id: Id::new(),
                payload: (),
            },
        )
    }
}

impl<Id: EdgeId, P> BiEdgeRaw<Id, P> {
    pub fn with_payload(from: usize, to: usize, payload: P) -> (usize, Self) {
        (from, Self::with_payload_impl(to, payload))
    }

    fn with_payload_impl(to: usize, payload: P) -> BiEdgeRaw<Id, P> {
        Self {
            to: to as u32,
            id: Id::new(),
            payload,
        }
    }
}

impl<Id: EdgeId, P: Clone> BidirectionalEdgeTrait for BiEdgeRaw<Id, P> {}

impl<Id: EdgeId, P: Clone> EdgeTrait for BiEdgeRaw<Id, P> {
    type Payload = P;

    const REVERSABLE: bool = true;

    fn to(&self) -> usize {
        self.to as usize
    }

    fn id(&self) -> usize {
        self.id.id()
    }

    fn set_id(&mut self, id: usize) {
        self.id.set_id(id);
    }

    fn reverse_id(&self) -> usize {
        panic!("no reverse id")
    }

    fn set_reverse_id(&mut self, _: usize) {}

    fn reverse_edge(&self, from: usize) -> Self {
        Self::with_payload_impl(from, self.payload.clone())
    }

    fn payload(&self) -> &P {
        &self.payload
    }
}

impl<Id: EdgeId, P: Clone> BiEdgeTrait for BiEdgeRaw<Id, P> {}

pub type BiEdge<P> = BiEdgeRaw<NoId, P>;
pub type BiEdgeWithId<P> = BiEdgeRaw<WithId, P>;
}
pub mod bi_edge_trait {
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;

pub trait BiEdgeTrait: EdgeTrait {}
}
pub mod edge {
use crate::algo_lib::graph::edges::edge_id::EdgeId;
use crate::algo_lib::graph::edges::edge_id::NoId;
use crate::algo_lib::graph::edges::edge_id::WithId;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;

#[derive(Clone)]
pub struct EdgeRaw<Id: EdgeId, P> {
    to: u32,
    id: Id,
    payload: P,
}

impl<Id: EdgeId> EdgeRaw<Id, ()> {
    pub fn new(from: usize, to: usize) -> (usize, Self) {
        (
            from,
            Self {
                to: to as u32,
                id: Id::new(),
                payload: (),
            },
        )
    }
}

impl<Id: EdgeId, P> EdgeRaw<Id, P> {
    pub fn with_payload(from: usize, to: usize, payload: P) -> (usize, Self) {
        (from, Self::with_payload_impl(to, payload))
    }

    fn with_payload_impl(to: usize, payload: P) -> Self {
        Self {
            to: to as u32,
            id: Id::new(),
            payload,
        }
    }
}

impl<Id: EdgeId, P: Clone> EdgeTrait for EdgeRaw<Id, P> {
    type Payload = P;

    const REVERSABLE: bool = false;

    fn to(&self) -> usize {
        self.to as usize
    }

    fn id(&self) -> usize {
        self.id.id()
    }

    fn set_id(&mut self, id: usize) {
        self.id.set_id(id);
    }

    fn reverse_id(&self) -> usize {
        panic!("no reverse")
    }

    fn set_reverse_id(&mut self, _: usize) {
        panic!("no reverse")
    }

    fn reverse_edge(&self, _: usize) -> Self {
        panic!("no reverse")
    }

    fn payload(&self) -> &P {
        &self.payload
    }
}

pub type Edge<P> = EdgeRaw<NoId, P>;
pub type EdgeWithId<P> = EdgeRaw<WithId, P>;
}
pub mod edge_id {
pub trait EdgeId: Clone {
    fn new() -> Self;
    fn id(&self) -> usize;
    fn set_id(&mut self, id: usize);
}

#[derive(Clone)]
pub struct WithId {
    id: u32,
}

impl EdgeId for WithId {
    fn new() -> Self {
        Self { id: 0 }
    }

    fn id(&self) -> usize {
        self.id as usize
    }

    fn set_id(&mut self, id: usize) {
        self.id = id as u32;
    }
}

#[derive(Clone)]
pub struct NoId {}

impl EdgeId for NoId {
    fn new() -> Self {
        Self {}
    }

    fn id(&self) -> usize {
        panic!("Id called on no id")
    }

    fn set_id(&mut self, _: usize) {}
}
}
pub mod edge_trait {
pub trait EdgeTrait: Clone {
    type Payload;
    
    const REVERSABLE: bool;

    fn to(&self) -> usize;
    fn id(&self) -> usize;
    fn set_id(&mut self, id: usize);
    fn reverse_id(&self) -> usize;
    fn set_reverse_id(&mut self, reverse_id: usize);
    #[must_use]
    fn reverse_edge(&self, from: usize) -> Self;
    fn payload(&self) -> &Self::Payload;
}

pub trait BidirectionalEdgeTrait: EdgeTrait {}
}
pub mod flow_edge {
use crate::algo_lib::graph::edges::edge_id::EdgeId;
use crate::algo_lib::graph::edges::edge_id::NoId;
use crate::algo_lib::graph::edges::edge_id::WithId;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
use crate::algo_lib::graph::edges::flow_edge_trait::FlowEdgeTrait;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;

#[derive(Clone)]
pub struct FlowEdgeRaw<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P> {
    to: u32,
    capacity: C,
    reverse_id: u32,
    id: Id,
    payload: P,
}

impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId> FlowEdgeRaw<C, Id, ()> {
    pub fn new(from: usize, to: usize, c: C) -> (usize, Self) {
        (
            from,
            Self {
                to: to as u32,
                capacity: c,
                reverse_id: 0,
                id: Id::new(),
                payload: (),
            },
        )
    }
}

impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P> FlowEdgeRaw<C, Id, P> {
    pub fn with_payload(from: usize, to: usize, c: C, payload: P) -> (usize, Self) {
        (from, Self::with_payload_impl(to, c, payload))
    }

    fn with_payload_impl(to: usize, c: C, payload: P) -> Self {
        Self {
            to: to as u32,
            capacity: c,
            reverse_id: 0,
            id: Id::new(),
            payload,
        }
    }
}

impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P: Default + Clone> EdgeTrait
    for FlowEdgeRaw<C, Id, P>
{
    type Payload = P;
    const REVERSABLE: bool = true;

    fn to(&self) -> usize {
        self.to as usize
    }

    fn id(&self) -> usize {
        self.id.id()
    }

    fn set_id(&mut self, id: usize) {
        self.id.set_id(id);
    }

    fn reverse_id(&self) -> usize {
        self.reverse_id as usize
    }

    fn set_reverse_id(&mut self, reverse_id: usize) {
        self.reverse_id = reverse_id as u32;
    }

    fn reverse_edge(&self, from: usize) -> Self {
        Self::with_payload_impl(from, C::zero(), P::default())
    }

    fn payload(&self) -> &P {
        &self.payload
    }
}

impl<C: AdditionMonoidWithSub + PartialOrd + Copy, Id: EdgeId, P: Default + Clone> FlowEdgeTrait<C>
    for FlowEdgeRaw<C, Id, P>
{
    fn capacity(&self) -> C {
        self.capacity
    }

    fn capacity_mut(&mut self) -> &mut C {
        &mut self.capacity
    }

    fn flow(&self, graph: &Graph<Self>) -> C {
        graph[self.to as usize][self.reverse_id as usize].capacity
    }
}

pub type FlowEdge<C, P> = FlowEdgeRaw<C, NoId, P>;
pub type FlowEdgeWithId<C, P> = FlowEdgeRaw<C, WithId, P>;
}
pub mod flow_edge_trait {
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;

pub trait FlowEdgeTrait<C: AdditionMonoidWithSub + PartialOrd + Copy>: EdgeTrait {
    fn capacity(&self) -> C;
    fn capacity_mut(&mut self) -> &mut C;
    fn flow(&self, graph: &Graph<Self>) -> C;
    fn push_flow(&self, flow: C) -> (usize, usize, C) {
        (self.to(), self.reverse_id(), flow)
    }
}
}
}
pub mod flow_graph {
use crate::algo_lib::graph::edges::flow_edge_trait::FlowEdgeTrait;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;

pub trait FlowGraph<C: AdditionMonoidWithSub + PartialOrd + Copy, E: FlowEdgeTrait<C>> {
    fn push_flow(&mut self, push_data: (usize, usize, C));
}

impl<C: AdditionMonoidWithSub + PartialOrd + Copy, E: FlowEdgeTrait<C>> FlowGraph<C, E>
    for Graph<E>
{
    fn push_flow(&mut self, (to, reverse_id, flow): (usize, usize, C)) {
        *self.edges[to][reverse_id].capacity_mut() += flow;
        let from = self.edges[to][reverse_id].to();
        let direct_id = self.edges[to][reverse_id].reverse_id();
        let direct = &mut self.edges[from][direct_id];
        assert!(flow >= C::zero() && flow <= direct.capacity());
        *direct.capacity_mut() -= flow;
    }
}
}
pub mod graph {
use crate::algo_lib::collections::dsu::DSU;
use crate::algo_lib::graph::edges::bi_edge::BiEdge;
use crate::algo_lib::graph::edges::edge::Edge;
use crate::algo_lib::graph::edges::edge_trait::BidirectionalEdgeTrait;
use crate::algo_lib::graph::edges::edge_trait::EdgeTrait;
use std::ops::Index;
use std::ops::IndexMut;

#[derive(Clone)]
pub struct Graph<E: EdgeTrait> {
    pub(super) edges: Vec<Vec<E>>,
    edge_count: usize,
}

impl<E: EdgeTrait> Graph<E> {
    pub fn new(vertex_count: usize) -> Self {
        Self {
            edges: vec![Vec::new(); vertex_count],
            edge_count: 0,
        }
    }

    pub fn add_edge(&mut self, (from, mut edge): (usize, E)) -> usize {
        let to = edge.to();
        assert!(to < self.edges.len());
        let direct_id = self.edges[from].len();
        edge.set_id(self.edge_count);
        self.edges[from].push(edge);
        if E::REVERSABLE {
            let rev_id = self.edges[to].len();
            self.edges[from][direct_id].set_reverse_id(rev_id);
            let mut rev_edge = self.edges[from][direct_id].reverse_edge(from);
            rev_edge.set_id(self.edge_count);
            rev_edge.set_reverse_id(direct_id);
            self.edges[to].push(rev_edge);
        }
        self.edge_count += 1;
        direct_id
    }

    pub fn add_vertices(&mut self, cnt: usize) {
        self.edges.resize(self.edges.len() + cnt, Vec::new());
    }

    pub fn clear(&mut self) {
        self.edge_count = 0;
        for ve in self.edges.iter_mut() {
            ve.clear();
        }
    }

    pub fn vertex_count(&self) -> usize {
        self.edges.len()
    }

    pub fn edge_count(&self) -> usize {
        self.edge_count
    }

    pub fn degrees(&self) -> Vec<usize> {
        self.edges.iter().map(|v| v.len()).collect()
    }
}

impl<E: BidirectionalEdgeTrait> Graph<E> {
    pub fn is_tree(&self) -> bool {
        if self.edge_count + 1 != self.vertex_count() {
            false
        } else {
            self.is_connected()
        }
    }

    pub fn is_forest(&self) -> bool {
        let mut dsu = DSU::new(self.vertex_count());
        for i in 0..self.vertex_count() {
            for e in self[i].iter() {
                if i <= e.to() && !dsu.join(i, e.to()) {
                    return false;
                }
            }
        }
        true
    }

    pub fn is_connected(&self) -> bool {
        let mut dsu = DSU::new(self.vertex_count());
        for i in 0..self.vertex_count() {
            for e in self[i].iter() {
                dsu.join(i, e.to());
            }
        }
        dsu.set_count() == 1
    }
}

impl<E: EdgeTrait> Index<usize> for Graph<E> {
    type Output = [E];

    fn index(&self, index: usize) -> &Self::Output {
        &self.edges[index]
    }
}

impl<E: EdgeTrait> IndexMut<usize> for Graph<E> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.edges[index]
    }
}

impl Graph<Edge<()>> {
    pub fn from_edges(n: usize, edges: &[(usize, usize)]) -> Self {
        let mut graph = Self::new(n);
        for &(from, to) in edges {
            graph.add_edge(Edge::new(from, to));
        }
        graph
    }
}

impl<P: Clone> Graph<Edge<P>> {
    pub fn from_edges_with_payload(n: usize, edges: &[(usize, usize, P)]) -> Self {
        let mut graph = Self::new(n);
        for (from, to, p) in edges.iter() {
            graph.add_edge(Edge::with_payload(*from, *to, p.clone()));
        }
        graph
    }
}

impl Graph<BiEdge<()>> {
    pub fn from_biedges(n: usize, edges: &[(usize, usize)]) -> Self {
        let mut graph = Self::new(n);
        for &(from, to) in edges {
            graph.add_edge(BiEdge::new(from, to));
        }
        graph
    }
}

impl<P: Clone> Graph<BiEdge<P>> {
    pub fn from_biedges_with_payload(n: usize, edges: &[(usize, usize, P)]) -> Self {
        let mut graph = Self::new(n);
        for (from, to, p) in edges.iter() {
            graph.add_edge(BiEdge::with_payload(*from, *to, p.clone()));
        }
        graph
    }
}
}
pub mod max_flow {
use crate::algo_lib::collections::slice_ext::legacy_fill::LegacyFill;
use crate::algo_lib::graph::edges::flow_edge_trait::FlowEdgeTrait;
use crate::algo_lib::graph::flow_graph::FlowGraph;
use crate::algo_lib::graph::graph::Graph;
use crate::algo_lib::misc::recursive_function::Callable2;
use crate::algo_lib::misc::recursive_function::RecursiveFunction2;
use crate::algo_lib::numbers::num_traits::algebra::AdditionMonoidWithSub;
use crate::algo_lib::numbers::num_traits::ord::MinMax;
use crate::when;
use std::collections::VecDeque;

pub trait MaxFlow<C: AdditionMonoidWithSub + Ord + Copy + MinMax> {
    fn max_flow(&mut self, source: usize, destination: usize) -> C;
}

impl<C: AdditionMonoidWithSub + Ord + Copy + MinMax, E: FlowEdgeTrait<C>> MaxFlow<C> for Graph<E> {
    fn max_flow(&mut self, source: usize, destination: usize) -> C {
        let n = self.vertex_count();
        let mut dist = vec![0u32; n];
        let mut next_edge = vec![0u32; n];
        let inf = C::max_val();
        let mut total_flow = C::zero();
        let mut q = VecDeque::new();
        loop {
            // 1.43
            dist.legacy_fill(std::u32::MAX);
            dist[source] = 0;
            q.push_back(source);
            while !q.is_empty() {
                let cur = q.pop_front().unwrap();
                for e in self[cur].iter() {
                    if e.capacity() != C::zero() {
                        let next = e.to();
                        // 1.43
                        if dist[next] == std::u32::MAX {
                            dist[next] = dist[cur] + 1;
                            q.push_back(next);
                        }
                    }
                }
            }
            // 1.43
            if dist[destination] == std::u32::MAX {
                break;
            }
            next_edge.legacy_fill(0);
            let mut dinic_impl = RecursiveFunction2::new(|f, source, mut flow| -> C {
                when! {
                    source == destination => flow,
                    flow == C::zero() || dist[source] == dist[destination] => C::zero(),
                    else => {
                        let mut total_pushed = C::zero();
                        while (next_edge[source] as usize) < self[source].len() {
                            let edge = &self[source][next_edge[source] as usize];
                            if edge.capacity() != C::zero() && dist[edge.to()] == dist[source] + 1 {
                                let pushed = f.call(edge.to(), flow.min(edge.capacity()));
                                if pushed != C::zero() {
                                    let push_data = edge.push_flow(pushed);
                                    self.push_flow(push_data);
                                    flow -= pushed;
                                    total_pushed += pushed;
                                    if flow == C::zero() {
                                        return total_pushed;
                                    }
                                }
                            }
                            next_edge[source] += 1;
                        }
                        total_pushed
                    },
                }
            });
            total_flow += dinic_impl.call(source, inf);
        }
        total_flow
    }
}
}
}
pub mod io {
pub mod input {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::io::Read;

pub struct Input<'s> {
    input: &'s mut (dyn Read + Send),
    buf: Vec<u8>,
    at: usize,
    buf_read: usize,
}

macro_rules! read_impl {
    ($t: ty, $read_name: ident, $read_vec_name: ident) => {
        pub fn $read_name(&mut self) -> $t {
            self.read()
        }

        pub fn $read_vec_name(&mut self, len: usize) -> Vec<$t> {
            self.read_vec(len)
        }
    };

    ($t: ty, $read_name: ident, $read_vec_name: ident, $read_pair_vec_name: ident) => {
        read_impl!($t, $read_name, $read_vec_name);

        pub fn $read_pair_vec_name(&mut self, len: usize) -> Vec<($t, $t)> {
            self.read_vec(len)
        }
    };
}

impl<'s> Input<'s> {
    const DEFAULT_BUF_SIZE: usize = 4096;

    pub fn new(input: &'s mut (dyn Read + Send)) -> Self {
        Self {
            input,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            buf_read: 0,
        }
    }

    pub fn new_with_size(input: &'s mut (dyn Read + Send), buf_size: usize) -> Self {
        Self {
            input,
            buf: default_vec(buf_size),
            at: 0,
            buf_read: 0,
        }
    }

    pub fn get(&mut self) -> Option<u8> {
        if self.refill_buffer() {
            let res = self.buf[self.at];
            self.at += 1;
            if res == b'\r' {
                if self.refill_buffer() && self.buf[self.at] == b'\n' {
                    self.at += 1;
                }
                return Some(b'\n');
            }
            Some(res)
        } else {
            None
        }
    }

    pub fn peek(&mut self) -> Option<u8> {
        if self.refill_buffer() {
            let res = self.buf[self.at];
            Some(if res == b'\r' { b'\n' } else { res })
        } else {
            None
        }
    }

    pub fn skip_whitespace(&mut self) {
        while let Some(b) = self.peek() {
            if !b.is_ascii_whitespace() {
                return;
            }
            self.get();
        }
    }

    pub fn next_token(&mut self) -> Option<Vec<u8>> {
        self.skip_whitespace();
        let mut res = Vec::new();
        while let Some(c) = self.get() {
            if c.is_ascii_whitespace() {
                break;
            }
            res.push(c);
        }
        if res.is_empty() {
            None
        } else {
            Some(res)
        }
    }

    //noinspection RsSelfConvention
    pub fn is_exhausted(&mut self) -> bool {
        self.peek().is_none()
    }

    //noinspection RsSelfConvention
    pub fn is_empty(&mut self) -> bool {
        self.skip_whitespace();
        self.is_exhausted()
    }

    pub fn read<T: Readable>(&mut self) -> T {
        T::read(self)
    }

    pub fn read_vec<T: Readable>(&mut self, size: usize) -> Vec<T> {
        let mut res = Vec::with_capacity(size);
        for _ in 0..size {
            res.push(self.read());
        }
        res
    }

    pub fn read_char(&mut self) -> u8 {
        self.skip_whitespace();
        self.get().unwrap()
    }

    read_impl!(u32, read_unsigned, read_unsigned_vec);
    read_impl!(u64, read_u64, read_u64_vec);
    read_impl!(usize, read_size, read_size_vec, read_size_pair_vec);
    read_impl!(i32, read_int, read_int_vec, read_int_pair_vec);
    read_impl!(i64, read_long, read_long_vec, read_long_pair_vec);
    read_impl!(i128, read_i128, read_i128_vec);

    fn refill_buffer(&mut self) -> bool {
        if self.at == self.buf_read {
            self.at = 0;
            self.buf_read = self.input.read(&mut self.buf).unwrap();
            self.buf_read != 0
        } else {
            true
        }
    }
}

pub trait Readable {
    fn read(input: &mut Input) -> Self;
}

impl Readable for u8 {
    fn read(input: &mut Input) -> Self {
        input.read_char()
    }
}

impl<T: Readable> Readable for Vec<T> {
    fn read(input: &mut Input) -> Self {
        let size = input.read();
        input.read_vec(size)
    }
}

macro_rules! read_integer {
    ($($t:ident)+) => {$(
        impl Readable for $t {
            fn read(input: &mut Input) -> Self {
                input.skip_whitespace();
                let mut c = input.get().unwrap();
                let sgn = match c {
                    b'-' => {
                        c = input.get().unwrap();
                        true
                    }
                    b'+' => {
                        c = input.get().unwrap();
                        false
                    }
                    _ => false,
                };
                let mut res = 0;
                loop {
                    assert!(c.is_ascii_digit());
                    res *= 10;
                    let d = (c - b'0') as $t;
                    if sgn {
                        res -= d;
                    } else {
                        res += d;
                    }
                    match input.get() {
                        None => break,
                        Some(ch) => {
                            if ch.is_ascii_whitespace() {
                                break;
                            } else {
                                c = ch;
                            }
                        }
                    }
                }
                res
            }
        }
    )+};
}

read_integer!(i8 i16 i32 i64 i128 isize u16 u32 u64 u128 usize);

macro_rules! tuple_readable {
    ($($name:ident)+) => {
        impl<$($name: Readable), +> Readable for ($($name,)+) {
            fn read(input: &mut Input) -> Self {
                ($($name::read(input),)+)
            }
        }
    }
}

tuple_readable! {T}
tuple_readable! {T U}
tuple_readable! {T U V}
tuple_readable! {T U V X}
tuple_readable! {T U V X Y}
tuple_readable! {T U V X Y Z}
tuple_readable! {T U V X Y Z A}
tuple_readable! {T U V X Y Z A B}
tuple_readable! {T U V X Y Z A B C}
tuple_readable! {T U V X Y Z A B C D}
tuple_readable! {T U V X Y Z A B C D E}
tuple_readable! {T U V X Y Z A B C D E F}

impl Read for Input<'_> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.at == self.buf_read {
            self.input.read(buf)
        } else {
            let mut i = 0;
            while i < buf.len() && self.at < self.buf_read {
                buf[i] = self.buf[self.at];
                i += 1;
                self.at += 1;
            }
            Ok(i)
        }
    }
}
}
pub mod output {
use crate::algo_lib::collections::vec_ext::default::default_vec;
use std::cmp::Reverse;
use std::io::stderr;
use std::io::Stderr;
use std::io::Write;

#[derive(Copy, Clone)]
pub enum BoolOutput {
    YesNo,
    YesNoCaps,
    PossibleImpossible,
    Custom(&'static str, &'static str),
}

impl BoolOutput {
    pub fn output(&self, output: &mut Output, val: bool) {
        (if val { self.yes() } else { self.no() }).write(output);
    }

    fn yes(&self) -> &str {
        match self {
            BoolOutput::YesNo => "Yes",
            BoolOutput::YesNoCaps => "YES",
            BoolOutput::PossibleImpossible => "Possible",
            BoolOutput::Custom(yes, _) => yes,
        }
    }

    fn no(&self) -> &str {
        match self {
            BoolOutput::YesNo => "No",
            BoolOutput::YesNoCaps => "NO",
            BoolOutput::PossibleImpossible => "Impossible",
            BoolOutput::Custom(_, no) => no,
        }
    }
}

pub struct Output<'s> {
    output: &'s mut dyn Write,
    buf: Vec<u8>,
    at: usize,
    auto_flush: bool,
    bool_output: BoolOutput,
}

impl<'s> Output<'s> {
    const DEFAULT_BUF_SIZE: usize = 4096;

    pub fn new(output: &'s mut dyn Write) -> Self {
        Self {
            output,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            auto_flush: false,
            bool_output: BoolOutput::YesNoCaps,
        }
    }

    pub fn new_with_auto_flush(output: &'s mut dyn Write) -> Self {
        Self {
            output,
            buf: default_vec(Self::DEFAULT_BUF_SIZE),
            at: 0,
            auto_flush: true,
            bool_output: BoolOutput::YesNoCaps,
        }
    }

    pub fn flush(&mut self) {
        if self.at != 0 {
            self.output.write_all(&self.buf[..self.at]).unwrap();
            self.output.flush().unwrap();
            self.at = 0;
        }
    }

    pub fn print<T: Writable>(&mut self, s: T) {
        s.write(self);
        self.maybe_flush();
    }

    pub fn print_line<T: Writable>(&mut self, s: T) {
        self.print(s);
        self.put(b'\n');
        self.maybe_flush();
    }

    pub fn put(&mut self, b: u8) {
        self.buf[self.at] = b;
        self.at += 1;
        if self.at == self.buf.len() {
            self.flush();
        }
    }

    pub fn maybe_flush(&mut self) {
        if self.auto_flush {
            self.flush();
        }
    }

    pub fn print_per_line<T: Writable>(&mut self, arg: &[T]) {
        self.print_per_line_iter(arg.iter());
    }

    pub fn print_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
        let mut first = true;
        for e in iter {
            if first {
                first = false;
            } else {
                self.put(b' ');
            }
            e.write(self);
        }
    }

    pub fn print_line_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
        self.print_iter(iter);
        self.put(b'\n');
    }

    pub fn print_per_line_iter<T: Writable, I: Iterator<Item = T>>(&mut self, iter: I) {
        for e in iter {
            e.write(self);
            self.put(b'\n');
        }
    }

    pub fn set_bool_output(&mut self, bool_output: BoolOutput) {
        self.bool_output = bool_output;
    }
}

impl Write for Output<'_> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut start = 0usize;
        let mut rem = buf.len();
        while rem > 0 {
            let len = (self.buf.len() - self.at).min(rem);
            self.buf[self.at..self.at + len].copy_from_slice(&buf[start..start + len]);
            self.at += len;
            if self.at == self.buf.len() {
                self.flush();
            }
            start += len;
            rem -= len;
        }
        self.maybe_flush();
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.flush();
        Ok(())
    }
}

pub trait Writable {
    fn write(&self, output: &mut Output);
}

impl Writable for &str {
    fn write(&self, output: &mut Output) {
        output.write_all(self.as_bytes()).unwrap();
    }
}

impl Writable for String {
    fn write(&self, output: &mut Output) {
        output.write_all(self.as_bytes()).unwrap();
    }
}

impl Writable for char {
    fn write(&self, output: &mut Output) {
        output.put(*self as u8);
    }
}

impl Writable for u8 {
    fn write(&self, output: &mut Output) {
        output.put(*self);
    }
}

impl<T: Writable> Writable for [T] {
    fn write(&self, output: &mut Output) {
        output.print_iter(self.iter());
    }
}

impl<T: Writable, const N: usize> Writable for [T; N] {
    fn write(&self, output: &mut Output) {
        output.print_iter(self.iter());
    }
}

impl<T: Writable + ?Sized> Writable for &T {
    fn write(&self, output: &mut Output) {
        T::write(self, output)
    }
}

impl<T: Writable> Writable for Vec<T> {
    fn write(&self, output: &mut Output) {
        self.as_slice().write(output);
    }
}

impl Writable for () {
    fn write(&self, _output: &mut Output) {}
}

macro_rules! write_to_string {
    ($($t:ident)+) => {$(
        impl Writable for $t {
            fn write(&self, output: &mut Output) {
                self.to_string().write(output);
            }
        }
    )+};
}

write_to_string!(u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);

macro_rules! tuple_writable {
    ($name0:ident $($name:ident: $id:tt )*) => {
        impl<$name0: Writable, $($name: Writable,)*> Writable for ($name0, $($name,)*) {
            fn write(&self, out: &mut Output) {
                self.0.write(out);
                $(
                out.put(b' ');
                self.$id.write(out);
                )*
            }
        }
    }
}

tuple_writable! {T}
tuple_writable! {T U:1}
tuple_writable! {T U:1 V:2}
tuple_writable! {T U:1 V:2 X:3}
tuple_writable! {T U:1 V:2 X:3 Y:4}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7}
tuple_writable! {T U:1 V:2 X:3 Y:4 Z:5 A:6 B:7 C:8}

impl<T: Writable> Writable for Option<T> {
    fn write(&self, output: &mut Output) {
        match self {
            None => (-1).write(output),
            Some(t) => t.write(output),
        }
    }
}

impl Writable for bool {
    fn write(&self, output: &mut Output) {
        let bool_output = output.bool_output;
        bool_output.output(output, *self)
    }
}

impl<T: Writable> Writable for Reverse<T> {
    fn write(&self, output: &mut Output) {
        self.0.write(output);
    }
}

static mut ERR: Option<Stderr> = None;

pub fn err() -> Output<'static> {
    unsafe {
        if ERR.is_none() {
            ERR = Some(stderr());
        }
        Output::new_with_auto_flush(ERR.as_mut().unwrap())
    }
}
}
}
pub mod misc {
pub mod extensions {
pub mod do_with {
pub trait DoWith: Sized {
    fn do_with<F>(mut self, f: F) -> Self
    where
        F: FnOnce(&mut Self),
    {
        f(&mut self);
        self
    }
}

impl<T> DoWith for T {}
}
}
pub mod recursive_function {
use std::marker::PhantomData;

macro_rules! recursive_function {
    ($name: ident, $trait: ident, ($($type: ident $arg: ident,)*)) => {
        pub trait $trait<$($type, )*Output> {
            fn call(&mut self, $($arg: $type,)*) -> Output;
        }

        pub struct $name<F, $($type, )*Output>
        where
            F: FnMut(&mut dyn $trait<$($type, )*Output>, $($type, )*) -> Output,
        {
            f: std::cell::UnsafeCell<F>,
            $($arg: PhantomData<$type>,
            )*
            phantom_output: PhantomData<Output>,
        }

        impl<F, $($type, )*Output> $name<F, $($type, )*Output>
        where
            F: FnMut(&mut dyn $trait<$($type, )*Output>, $($type, )*) -> Output,
        {
            pub fn new(f: F) -> Self {
                Self {
                    f: std::cell::UnsafeCell::new(f),
                    $($arg: Default::default(),
                    )*
                    phantom_output: Default::default(),
                }
            }
        }

        impl<F, $($type, )*Output> $trait<$($type, )*Output> for $name<F, $($type, )*Output>
        where
            F: FnMut(&mut dyn $trait<$($type, )*Output>, $($type, )*) -> Output,
        {
            fn call(&mut self, $($arg: $type,)*) -> Output {
                unsafe { (*self.f.get())(self, $($arg, )*) }
            }
        }
    }
}

recursive_function!(RecursiveFunction0, Callable0, ());
recursive_function!(RecursiveFunction, Callable, (Arg arg,));
recursive_function!(RecursiveFunction2, Callable2, (Arg1 arg1, Arg2 arg2,));
recursive_function!(RecursiveFunction3, Callable3, (Arg1 arg1, Arg2 arg2, Arg3 arg3,));
recursive_function!(RecursiveFunction4, Callable4, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4,));
recursive_function!(RecursiveFunction5, Callable5, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5,));
recursive_function!(RecursiveFunction6, Callable6, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6,));
recursive_function!(RecursiveFunction7, Callable7, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7,));
recursive_function!(RecursiveFunction8, Callable8, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7, Arg8 arg8,));
recursive_function!(RecursiveFunction9, Callable9, (Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7, Arg8 arg8, Arg9 arg9,));
}
pub mod test_type {
pub enum TestType {
    Single,
    MultiNumber,
    MultiEof,
}

pub enum TaskType {
    Classic,
    Interactive,
}
}
pub mod when {
#[macro_export]
macro_rules! when {
    {$($cond: expr => $then: expr,)*} => {
        match () {
            $(_ if $cond => $then,)*
            _ => unreachable!(),
        }
    };
    {$($cond: expr => $then: expr,)* else $(=>)? $else: expr$(,)?} => {
        match () {
            $(_ if $cond => $then,)*
            _ => $else,
        }
    };
}
}
}
pub mod numbers {
pub mod num_traits {
pub mod algebra {
use crate::algo_lib::numbers::num_traits::invertible::Invertible;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Div;
use std::ops::DivAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Neg;
use std::ops::Rem;
use std::ops::RemAssign;
use std::ops::Sub;
use std::ops::SubAssign;

pub trait Zero {
    fn zero() -> Self;
}

pub trait One {
    fn one() -> Self;
}

pub trait AdditionMonoid: Add<Output = Self> + AddAssign + Zero + Eq + Sized {}

impl<T: Add<Output = Self> + AddAssign + Zero + Eq> AdditionMonoid for T {}

pub trait AdditionMonoidWithSub: AdditionMonoid + Sub<Output = Self> + SubAssign {}

impl<T: AdditionMonoid + Sub<Output = Self> + SubAssign> AdditionMonoidWithSub for T {}

pub trait AdditionGroup: AdditionMonoidWithSub + Neg<Output = Self> {}

impl<T: AdditionMonoidWithSub + Neg<Output = Self>> AdditionGroup for T {}

pub trait MultiplicationMonoid: Mul<Output = Self> + MulAssign + One + Eq + Sized {}

impl<T: Mul<Output = Self> + MulAssign + One + Eq> MultiplicationMonoid for T {}

pub trait IntegerMultiplicationMonoid:
    MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign
{
}

impl<T: MultiplicationMonoid + Div<Output = Self> + Rem<Output = Self> + DivAssign + RemAssign>
    IntegerMultiplicationMonoid for T
{
}

pub trait MultiplicationGroup:
    MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>
{
}

impl<T: MultiplicationMonoid + Div<Output = Self> + DivAssign + Invertible<Output = Self>>
    MultiplicationGroup for T
{
}

pub trait SemiRing: AdditionMonoid + MultiplicationMonoid {}

impl<T: AdditionMonoid + MultiplicationMonoid> SemiRing for T {}

pub trait SemiRingWithSub: AdditionMonoidWithSub + SemiRing {}

impl<T: AdditionMonoidWithSub + SemiRing> SemiRingWithSub for T {}

pub trait Ring: SemiRing + AdditionGroup {}

impl<T: SemiRing + AdditionGroup> Ring for T {}

pub trait IntegerSemiRing: SemiRing + IntegerMultiplicationMonoid {}

impl<T: SemiRing + IntegerMultiplicationMonoid> IntegerSemiRing for T {}

pub trait IntegerSemiRingWithSub: SemiRingWithSub + IntegerSemiRing {}

impl<T: SemiRingWithSub + IntegerSemiRing> IntegerSemiRingWithSub for T {}

pub trait IntegerRing: IntegerSemiRing + Ring {}

impl<T: IntegerSemiRing + Ring> IntegerRing for T {}

pub trait Field: Ring + MultiplicationGroup {}

impl<T: Ring + MultiplicationGroup> Field for T {}

macro_rules! zero_one_integer_impl {
    ($($t: ident)+) => {$(
        impl Zero for $t {
            fn zero() -> Self {
                0
            }
        }

        impl One for $t {
            fn one() -> Self {
                1
            }
        }
    )+};
}

zero_one_integer_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
pub mod invertible {
pub trait Invertible {
    type Output;

    fn inv(&self) -> Option<Self::Output>;
}
}
pub mod ord {
pub trait MinMax: PartialOrd {
    fn min_val() -> Self;
    fn max_val() -> Self;
}

macro_rules! min_max_integer_impl {
    ($($t: ident)+) => {$(
        impl MinMax for $t {
            fn min_val() -> Self {
                // 1.43
                std::$t::MIN
            }

            fn max_val() -> Self {
                // 1.43
                std::$t::MAX
            }
        }
    )+};
}

min_max_integer_impl!(i128 i64 i32 i16 i8 isize u128 u64 u32 u16 u8 usize);
}
}
}
}
fn main() {
    let mut sin = std::io::stdin();
    let input = algo_lib::io::input::Input::new(&mut sin);
    let mut stdout = std::io::stdout();
    let output = algo_lib::io::output::Output::new(&mut stdout);
    solution::run(input, output);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 2068kb

input:

5 3
1 2
4 5
2 5

output:

2

result:

ok "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 2160kb

input:

2 4
1 2
1 2
1 2
1 2

output:

4

result:

ok "4"

Test #3:

score: 0
Accepted
time: 0ms
memory: 2292kb

input:

32 11
25 32
19 32
11 24
20 31
22 25
21 26
17 22
30 31
23 28
4 15
19 22

output:

8

result:

ok "8"

Test #4:

score: 0
Accepted
time: 0ms
memory: 2688kb

input:

499 500
162 343
318 339
57 480
390 409
35 390
244 355
86 245
65 130
239 386
208 219
63 444
366 419
393 420
169 390
219 446
491 494
235 498
205 328
380 483
485 490
243 448
467 488
10 249
339 470
332 465
38 469
359 430
378 465
447 490
414 461
347 476
197 360
111 122
209 460
3 364
445 470
212 337
495 4...

output:

264

result:

ok "264"

Test #5:

score: 0
Accepted
time: 0ms
memory: 2896kb

input:

500 500
171 410
142 433
337 392
158 205
290 373
148 315
414 465
344 381
86 103
221 466
42 315
242 433
281 344
85 268
442 469
60 415
436 461
345 378
343 488
276 443
102 409
475 496
281 352
286 439
306 477
365 464
331 462
287 296
245 266
164 213
305 474
280 495
257 478
182 321
119 166
3 270
338 401
20...

output:

253

result:

ok "253"

Test #6:

score: 0
Accepted
time: 2ms
memory: 2760kb

input:

499 500
482 497
321 398
286 321
489 494
167 178
296 401
146 373
98 145
232 375
67 376
243 478
354 401
212 393
82 185
385 486
494 495
418 491
296 403
47 492
482 485
131 278
1 104
409 484
403 436
402 461
233 498
243 332
486 497
233 264
353 372
82 455
465 478
218 441
220 395
387 416
394 487
341 412
330...

output:

256

result:

ok "256"

Test #7:

score: 0
Accepted
time: 2ms
memory: 2708kb

input:

499 499
322 433
115 244
492 495
268 389
164 489
352 467
207 436
292 465
63 106
411 460
173 376
479 486
300 479
360 395
415 438
4 391
465 476
349 416
44 111
236 483
294 365
447 456
163 498
323 418
364 379
377 482
294 489
455 494
347 434
267 316
204 443
217 440
85 482
81 128
320 459
108 371
171 422
48...

output:

262

result:

ok "262"

Test #8:

score: 0
Accepted
time: 2ms
memory: 2720kb

input:

500 499
435 468
89 328
194 425
187 286
295 420
418 445
467 496
71 490
246 463
423 460
283 444
421 442
333 416
68 345
354 423
38 211
358 397
182 385
148 487
131 498
311 364
40 351
421 438
438 471
413 448
319 344
450 479
228 385
309 328
149 448
110 291
408 455
131 470
334 485
346 397
254 435
480 489
6...

output:

253

result:

ok "253"

Test #9:

score: 0
Accepted
time: 2ms
memory: 2764kb

input:

499 499
471 486
407 466
38 341
357 396
376 487
367 372
463 472
182 187
245 456
314 495
42 217
16 243
186 357
478 487
477 488
179 338
113 222
72 459
271 280
233 240
486 489
253 356
424 437
206 369
12 319
254 333
208 383
272 395
473 474
154 473
141 202
317 494
242 487
280 333
137 368
484 485
462 467
9...

output:

266

result:

ok "266"

Test #10:

score: 0
Accepted
time: 2ms
memory: 2748kb

input:

500 499
241 358
473 482
483 490
120 355
470 495
16 267
473 478
137 144
50 289
197 356
479 500
212 297
420 427
239 390
380 399
210 321
256 303
256 287
11 294
369 470
480 489
114 415
455 462
451 480
272 337
281 362
143 212
43 290
395 426
156 257
469 496
160 461
147 198
246 255
22 111
371 420
39 454
25...

output:

261

result:

ok "261"

Test #11:

score: 0
Accepted
time: 2ms
memory: 2656kb

input:

500 499
460 475
106 201
149 462
449 470
478 499
33 284
463 468
335 470
101 338
192 277
14 477
127 132
31 262
483 498
79 236
141 372
277 308
80 433
37 212
50 397
177 322
43 64
109 340
485 494
60 269
160 233
27 496
117 318
196 201
133 368
473 480
230 237
115 292
340 447
87 258
131 368
177 296
497 498
...

output:

260

result:

ok "260"

Test #12:

score: 0
Accepted
time: 2ms
memory: 2688kb

input:

499 500
118 391
319 446
291 498
165 334
401 462
23 216
241 372
248 301
280 427
355 450
295 476
238 283
222 411
297 416
365 394
477 478
283 320
32 85
341 424
238 303
109 314
260 421
15 376
30 349
258 365
426 473
331 394
249 482
110 269
11 190
385 462
137 280
391 498
454 477
365 488
441 498
343 474
14...

output:

254

result:

ok "254"

Test #13:

score: 0
Accepted
time: 2ms
memory: 2868kb

input:

500 500
253 414
308 407
340 443
305 378
395 456
32 87
199 446
261 450
304 459
87 338
48 227
25 276
8 221
457 494
402 491
53 308
248 265
18 315
467 492
385 490
69 274
6 175
77 192
179 320
46 379
450 457
263 470
171 330
461 466
297 430
41 112
314 373
337 440
316 385
325 408
242 331
222 449
374 469
395...

output:

261

result:

ok "261"

Test #14:

score: 0
Accepted
time: 1ms
memory: 2520kb

input:

375 309
357 370
68 317
20 99
311 370
55 102
6 37
327 328
373 374
217 334
80 237
25 74
332 345
311 348
252 323
85 244
255 282
345 368
31 188
360 373
193 238
70 85
25 198
275 354
204 249
88 353
101 310
311 354
2 251
133 188
355 372
227 230
304 343
83 180
138 141
366 371
183 236
233 338
249 282
119 202...

output:

166

result:

ok "166"

Test #15:

score: 0
Accepted
time: 1ms
memory: 2604kb

input:

351 347
343 350
110 197
91 144
144 193
7 68
177 328
179 262
285 336
14 349
67 260
334 347
278 287
302 345
332 351
238 267
190 289
284 289
36 269
230 239
195 214
302 307
171 294
28 349
54 341
229 288
133 218
69 336
245 294
113 220
254 335
340 341
210 237
38 93
118 185
13 200
262 273
330 337
63 304
24...

output:

181

result:

ok "181"

Test #16:

score: 0
Accepted
time: 1ms
memory: 2576kb

input:

475 348
422 435
349 386
222 289
43 68
138 251
47 202
72 137
345 400
139 436
204 461
63 392
269 312
359 382
13 434
304 435
166 425
302 465
133 176
107 200
71 430
322 391
115 318
80 457
356 437
240 455
87 260
14 405
214 413
321 424
36 143
453 456
258 327
311 378
290 327
102 245
329 428
82 219
113 282
...

output:

181

result:

ok "181"

Test #17:

score: 0
Accepted
time: 0ms
memory: 2720kb

input:

466 436
205 330
98 241
291 462
289 384
296 373
292 347
38 191
344 451
62 207
233 358
308 435
299 430
246 441
430 447
454 461
266 417
253 452
332 335
63 148
87 234
445 464
201 262
92 203
429 452
393 420
337 344
305 326
69 182
84 441
392 423
350 381
108 153
79 252
165 204
49 190
309 444
278 465
346 36...

output:

234

result:

ok "234"

Test #18:

score: 0
Accepted
time: 1ms
memory: 2468kb

input:

331 247
48 197
224 291
252 307
185 316
101 226
155 184
283 312
199 318
275 298
172 223
3 166
15 300
255 296
269 270
38 223
225 300
22 203
33 240
325 330
121 278
320 329
8 327
202 207
174 281
230 253
318 321
128 209
147 268
120 157
237 308
12 79
131 304
330 331
113 258
230 255
168 229
99 138
70 159
1...

output:

125

result:

ok "125"

Test #19:

score: 0
Accepted
time: 0ms
memory: 2268kb

input:

118 229
61 114
104 107
82 107
25 100
49 52
40 75
74 85
95 110
68 101
28 51
13 100
59 106
92 115
90 99
17 84
81 102
84 101
64 107
50 109
105 118
61 94
41 64
50 113
16 33
40 45
88 111
117 118
27 74
75 112
5 8
100 101
54 91
42 105
15 32
111 116
97 104
37 114
95 110
11 102
111 116
73 74
83 92
42 85
21 4...

output:

126

result:

ok "126"

Test #20:

score: 0
Accepted
time: 1ms
memory: 2740kb

input:

399 354
130 279
317 398
239 266
395 398
108 373
327 344
248 299
52 253
151 318
140 317
93 154
106 151
334 353
24 101
187 300
34 67
291 364
167 322
74 299
154 159
154 325
278 335
22 157
37 108
386 393
271 352
157 214
35 108
109 344
207 308
195 338
367 392
316 335
370 399
301 394
41 354
93 250
274 367...

output:

180

result:

ok "180"

Test #21:

score: 0
Accepted
time: 0ms
memory: 2348kb

input:

103 224
93 102
37 92
44 47
46 69
23 46
11 30
97 102
90 103
82 103
35 42
43 102
27 72
76 95
69 102
75 84
32 71
27 50
72 97
75 84
93 96
42 57
83 94
21 50
33 58
19 34
58 97
55 94
29 38
18 75
81 102
2 15
30 79
30 43
55 96
69 84
88 101
69 70
74 91
8 55
93 100
98 101
10 97
61 78
2 83
97 102
92 99
81 84
14...

output:

123

result:

ok "123"

Test #22:

score: 0
Accepted
time: 1ms
memory: 2284kb

input:

305 235
217 290
152 159
23 182
26 195
204 267
230 271
93 214
46 287
10 179
11 180
201 226
34 61
213 258
282 291
4 279
231 294
31 196
253 304
116 151
193 228
100 101
11 194
18 191
111 304
10 45
289 304
24 71
37 272
175 276
114 211
257 258
112 135
246 261
207 252
158 255
212 219
257 304
274 281
269 30...

output:

121

result:

ok "121"

Test #23:

score: 0
Accepted
time: 1ms
memory: 2856kb

input:

109 410
82 99
55 62
75 90
58 79
4 17
63 78
8 33
70 97
29 74
43 98
45 104
96 99
27 108
83 96
82 95
70 107
56 79
93 104
21 92
42 81
97 102
1 58
86 89
38 91
82 89
33 82
63 70
19 66
77 106
36 63
35 46
55 66
34 73
83 100
66 85
36 81
36 53
12 55
10 31
25 28
90 109
83 100
86 107
37 98
56 87
8 105
42 101
11...

output:

207

result:

ok "207"

Test #24:

score: 0
Accepted
time: 2ms
memory: 2848kb

input:

247 478
186 247
61 196
88 129
198 243
49 178
145 242
231 238
35 50
176 191
26 97
60 99
181 216
100 217
54 193
61 246
144 165
233 242
20 85
158 191
196 227
200 245
106 187
3 88
7 224
201 240
200 247
14 141
200 245
108 133
46 119
186 217
197 230
98 145
177 230
9 78
129 184
67 140
181 246
221 236
48 21...

output:

246

result:

ok "246"

Test #25:

score: 0
Accepted
time: 0ms
memory: 2316kb

input:

222 208
2 177
118 205
149 218
168 171
220 221
12 41
153 218
95 214
178 217
14 27
159 202
53 222
48 211
187 222
116 147
57 178
71 72
132 183
110 189
17 192
101 172
120 215
122 155
115 172
121 220
90 143
55 108
43 212
108 141
128 181
217 220
121 194
164 175
24 103
110 119
85 90
143 208
92 117
35 176
9...

output:

110

result:

ok "110"

Test #26:

score: 0
Accepted
time: 2ms
memory: 2764kb

input:

274 489
128 265
147 248
152 249
198 243
211 254
209 212
155 160
46 177
10 209
5 204
236 241
90 217
115 236
268 269
19 216
81 238
221 250
84 111
166 187
54 155
75 234
268 273
99 150
44 127
51 114
52 235
251 258
168 267
242 255
254 263
55 154
124 273
213 234
90 219
137 212
203 228
251 272
46 71
51 274...

output:

261

result:

ok "261"

Test #27:

score: 0
Accepted
time: 0ms
memory: 2136kb

input:

385 78
304 335
176 307
235 302
17 154
164 277
192 285
382 385
127 226
368 381
356 359
346 375
194 347
337 358
196 211
25 192
132 185
201 354
336 357
335 360
351 378
258 275
370 385
169 268
102 269
326 385
78 113
25 28
359 370
275 306
315 368
382 385
340 353
290 299
313 374
158 185
189 224
319 372
19...

output:

43

result:

ok "43"

Test #28:

score: 0
Accepted
time: 2ms
memory: 2832kb

input:

93 475
57 76
73 76
63 76
74 75
85 92
91 92
49 54
85 92
76 81
82 85
53 92
3 86
5 32
68 93
3 38
76 91
31 76
43 90
89 90
61 88
23 66
92 93
22 83
13 14
33 92
16 31
87 90
50 57
86 93
61 88
5 56
86 91
38 71
80 89
46 47
75 82
52 87
15 50
77 86
40 79
18 33
38 91
38 87
14 39
40 83
51 60
1 48
61 92
40 53
28 9...

output:

242

result:

ok "242"

Test #29:

score: 0
Accepted
time: 1ms
memory: 2908kb

input:

296 443
84 161
136 201
87 134
186 249
152 209
36 155
284 295
180 291
147 266
239 248
12 203
45 134
163 166
210 277
77 126
106 251
41 296
38 67
220 281
41 284
257 284
8 167
291 296
83 96
166 183
138 269
136 145
131 170
18 181
223 254
131 146
73 110
173 284
110 279
135 150
216 239
204 215
8 197
32 169...

output:

239

result:

ok "239"

Test #30:

score: 0
Accepted
time: 1ms
memory: 2564kb

input:

87 295
56 67
78 85
78 83
52 87
26 61
81 86
5 42
31 36
6 57
30 77
48 73
30 77
76 85
66 85
76 81
14 55
26 39
74 83
75 86
83 86
75 86
40 47
27 56
21 24
85 86
6 17
65 66
50 79
33 36
44 71
42 67
48 83
75 78
40 49
54 81
18 41
55 68
70 87
85 86
45 46
86 87
21 46
69 86
86 87
16 41
10 57
53 54
74 81
57 66
25...

output:

160

result:

ok "160"

Test #31:

score: 0
Accepted
time: 1ms
memory: 2776kb

input:

177 394
127 136
87 168
15 134
23 82
31 118
160 169
140 169
13 118
101 120
139 154
176 177
54 113
94 111
14 79
34 51
52 113
152 171
62 161
58 129
20 61
141 142
80 147
93 128
64 113
59 176
145 154
36 153
62 161
60 133
102 115
75 106
103 118
58 99
164 169
169 176
128 131
65 120
95 142
28 143
30 115
158...

output:

207

result:

ok "207"

Test #32:

score: 0
Accepted
time: 0ms
memory: 2340kb

input:

482 216
133 162
280 423
234 317
139 416
289 464
419 440
409 428
153 306
84 219
22 93
272 381
397 470
369 408
432 479
198 269
89 352
447 452
139 272
135 294
63 90
448 465
387 404
70 339
185 450
236 327
235 350
148 455
193 440
352 463
401 456
403 428
25 420
427 472
71 306
325 400
360 479
351 436
377 3...

output:

128

result:

ok "128"

Test #33:

score: 0
Accepted
time: 0ms
memory: 2284kb

input:

411 190
115 340
368 399
170 383
310 313
169 334
68 255
23 270
356 377
218 279
112 265
194 317
130 261
258 345
176 411
154 401
189 268
76 327
83 160
242 399
345 402
61 126
235 384
345 390
83 356
46 157
83 160
348 351
14 341
22 107
258 307
95 292
9 270
400 403
215 348
273 328
372 379
133 146
366 367
2...

output:

101

result:

ok "101"

Test #34:

score: 0
Accepted
time: 0ms
memory: 2392kb

input:

500 250
1 500
2 499
3 498
4 497
5 496
6 495
7 494
8 493
9 492
10 491
11 490
12 489
13 488
14 487
15 486
16 485
17 484
18 483
19 482
20 481
21 480
22 479
23 478
24 477
25 476
26 475
27 474
28 473
29 472
30 471
31 470
32 469
33 468
34 467
35 466
36 465
37 464
38 463
39 462
40 461
41 460
42 459
43 458
...

output:

250

result:

ok "250"

Test #35:

score: 0
Accepted
time: 1ms
memory: 2676kb

input:

500 250
1 250
2 251
3 252
4 253
5 254
6 255
7 256
8 257
9 258
10 259
11 260
12 261
13 262
14 263
15 264
16 265
17 266
18 267
19 268
20 269
21 270
22 271
23 272
24 273
25 274
26 275
27 276
28 277
29 278
30 279
31 280
32 281
33 282
34 283
35 284
36 285
37 286
38 287
39 288
40 289
41 290
42 291
43 292
...

output:

125

result:

ok "125"

Test #36:

score: 0
Accepted
time: 1ms
memory: 3020kb

input:

44 484
1 2
1 4
1 6
1 8
1 10
1 12
1 14
1 16
1 18
1 20
1 22
1 24
1 26
1 28
1 30
1 32
1 34
1 36
1 38
1 40
1 42
1 44
2 3
2 5
2 7
2 9
2 11
2 13
2 15
2 17
2 19
2 21
2 23
2 25
2 27
2 29
2 31
2 33
2 35
2 37
2 39
2 41
2 43
3 4
3 6
3 8
3 10
3 12
3 14
3 16
3 18
3 20
3 22
3 24
3 26
3 28
3 30
3 32
3 34
3 36
3 38...

output:

253

result:

ok "253"

Test #37:

score: 0
Accepted
time: 0ms
memory: 2432kb

input:

500 499
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 ...

output:

250

result:

ok "250"

Test #38:

score: 0
Accepted
time: 0ms
memory: 2192kb

input:

500 497
1 4
2 5
3 6
4 7
5 8
6 9
7 10
8 11
9 12
10 13
11 14
12 15
13 16
14 17
15 18
16 19
17 20
18 21
19 22
20 23
21 24
22 25
23 26
24 27
25 28
26 29
27 30
28 31
29 32
30 33
31 34
32 35
33 36
34 37
35 38
36 39
37 40
38 41
39 42
40 43
41 44
42 45
43 46
44 47
45 48
46 49
47 50
48 51
49 52
50 53
51 54
5...

output:

249

result:

ok "249"

Test #39:

score: 0
Accepted
time: 0ms
memory: 2136kb

input:

43 19
25 36
3 30
10 27
18 43
39 42
34 39
25 42
3 26
2 9
11 20
40 41
39 42
9 42
32 41
36 39
17 24
15 36
31 38
37 40

output:

12

result:

ok "12"

Test #40:

score: 0
Accepted
time: 0ms
memory: 2140kb

input:

48 27
46 47
33 40
4 37
34 47
11 48
23 28
14 47
18 35
28 47
38 45
17 34
22 35
9 18
12 17
12 23
7 42
36 45
38 47
17 30
26 45
39 42
33 48
31 44
38 41
23 30
11 12
15 28

output:

14

result:

ok "14"

Test #41:

score: 0
Accepted
time: 0ms
memory: 2112kb

input:

45 27
20 43
32 39
43 44
36 45
25 28
30 33
11 32
25 44
7 38
24 25
13 26
30 45
43 44
5 34
9 20
38 41
43 44
19 42
18 25
24 37
31 40
37 38
16 41
5 38
1 24
22 29
10 15

output:

15

result:

ok "15"

Test #42:

score: 0
Accepted
time: 0ms
memory: 2160kb

input:

36 40
33 34
10 31
23 30
8 33
7 14
29 32
16 27
3 14
34 35
12 33
18 21
17 30
9 10
29 30
30 33
22 31
5 32
15 26
6 19
20 35
7 8
14 33
29 32
18 35
20 35
17 20
26 33
26 35
31 36
11 14
20 29
32 35
32 35
29 36
33 36
33 34
31 34
10 35
28 35
25 28

output:

20

result:

ok "20"

Test #43:

score: 0
Accepted
time: 0ms
memory: 2088kb

input:

41 38
34 39
12 19
31 40
24 31
18 21
23 30
25 32
5 12
38 41
11 38
28 33
17 18
32 39
6 25
18 33
17 28
28 35
32 39
12 37
26 37
15 26
5 22
10 13
3 40
37 40
7 12
35 40
4 29
34 37
22 37
40 41
21 22
33 34
16 19
11 38
18 39
37 40
7 32

output:

20

result:

ok "20"

Test #44:

score: 0
Accepted
time: 0ms
memory: 2156kb

input:

35 36
7 20
30 35
30 33
15 22
7 32
12 27
19 28
26 29
21 34
23 26
19 24
1 4
28 33
21 30
30 31
9 32
15 20
1 18
10 27
22 27
10 33
32 35
22 33
6 25
19 32
18 21
27 34
30 33
2 23
7 28
16 21
12 31
30 35
9 12
13 26
29 32

output:

18

result:

ok "18"

Test #45:

score: 0
Accepted
time: 0ms
memory: 2360kb

input:

48 34
5 38
30 33
17 46
47 48
26 31
17 20
31 44
23 36
15 40
10 27
20 35
31 46
31 42
8 25
11 22
7 36
3 6
43 46
25 38
16 31
38 43
41 44
2 45
37 46
17 34
17 40
24 39
26 41
14 45
37 44
11 42
27 30
6 7
34 43

output:

21

result:

ok "21"

Test #46:

score: 0
Accepted
time: 0ms
memory: 2112kb

input:

35 22
21 28
10 11
12 29
26 29
22 27
5 24
26 35
4 7
13 30
17 34
24 33
26 29
22 35
20 31
13 16
5 12
20 25
33 34
25 34
7 18
8 19
4 27

output:

14

result:

ok "14"

Test #47:

score: 0
Accepted
time: 0ms
memory: 2164kb

input:

34 24
20 21
15 28
3 34
17 32
1 4
28 31
18 33
23 34
18 31
5 20
14 21
32 33
19 26
14 33
21 26
31 32
33 34
33 34
5 32
8 17
12 31
27 30
31 34
25 34

output:

15

result:

ok "15"

Test #48:

score: 0
Accepted
time: 0ms
memory: 2164kb

input:

31 12
3 26
8 23
23 28
6 11
9 20
9 22
24 29
6 13
22 27
14 17
10 17
28 31

output:

8

result:

ok "8"

Extra Test:

score: 0
Extra Test Passed