結果

問題 No.1244 Black Segment
ユーザー o2co2c
提出日時 2020-10-07 08:51:57
言語 Rust
(1.77.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 3,399 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 147,804 KB
実行使用メモリ 11,816 KB
最終ジャッジ日時 2023-09-27 09:08:02
合計ジャッジ時間 2,632 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 11 ms
5,104 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 14 ms
7,940 KB
testcase_19 AC 19 ms
8,484 KB
testcase_20 AC 18 ms
9,448 KB
testcase_21 AC 17 ms
9,872 KB
testcase_22 AC 18 ms
9,460 KB
testcase_23 AC 21 ms
10,248 KB
testcase_24 AC 20 ms
9,844 KB
testcase_25 AC 17 ms
9,392 KB
testcase_26 AC 20 ms
10,412 KB
testcase_27 AC 24 ms
11,816 KB
testcase_28 AC 20 ms
9,980 KB
testcase_29 AC 22 ms
10,688 KB
testcase_30 AC 18 ms
9,668 KB
testcase_31 AC 24 ms
10,896 KB
testcase_32 AC 25 ms
11,020 KB
testcase_33 AC 25 ms
10,912 KB
testcase_34 AC 10 ms
7,600 KB
testcase_35 AC 10 ms
7,072 KB
testcase_36 AC 15 ms
8,576 KB
testcase_37 AC 18 ms
10,148 KB
testcase_38 AC 16 ms
10,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use {std::cmp::*, std::collections::*, std::io::Write, std::ops::*};

#[allow(unused_macros)]
macro_rules !dbg {($($e :expr ) ,*) =>{#[cfg (debug_assertions ) ] $({let (e ,mut err ) =(stringify !($e ) ,std ::io ::stderr () ) ;writeln !(err ,"{} = {:?}" ,e ,$e ) .unwrap () } ) *} ;}
#[allow(unused_macros)]
macro_rules !min {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::min ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::min ($a ,min !($($rest ) ,+) ) } } ;}
#[allow(unused_macros)]
macro_rules !chmin {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_min =min !($($cmps ) ,+) ;if $base >cmp_min {$base =cmp_min ;true } else {false } } } ;}
#[allow(unused_macros)]
macro_rules !max {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::max ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::max ($a ,max !($($rest ) ,+) ) } } ;}
#[allow(unused_macros)]
macro_rules !chmax {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_max =max !($($cmps ) ,+) ;if $base <cmp_max {$base =cmp_max ;true } else {false } } } ;}

#[allow(dead_code)]
mod scanner {
    use std;
    use std::io::Read;
    use std::str::FromStr;
    use std::str::SplitWhitespace;
    pub struct Scanner<'a> {
        it: SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            match self.it.next().unwrap().parse::<T>() {
                Ok(v) => v,
                _ => panic!("Scanner error"),
            }
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
    pub fn read_string() -> String {
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }
}

fn main() {
    let sc = scanner::read_string();
    let mut sc = scanner::Scanner::new(&sc);

    let n: usize = sc.next();
    let m: usize = sc.next();
    let a: usize = sc.next();
    let b: usize = sc.next();
    let mut to = vec![vec![]; n + 2];
    for _ in 0..m {
        let mut l: usize = sc.next();
        let mut r: usize = sc.next();
        if l < a && r < a {
            continue;
        }
        if l > b && r > b {
            continue;
        }
        if l < a {
            l = a;
        }
        if r > b {
            r = b + 1;
        } else {
            r += 1;
        }
        to[l].push(r);
        to[r].push(l);
    }

    let mut d = vec![100000000usize; n + 2];
    let mut visited = vec![false; n + 2];
    let mut q = std::collections::VecDeque::<usize>::new();
    q.push_back(a);
    d[a] = 0;
    while let Some(v) = q.pop_front() {
        dbg!(v);
        if visited[v] {
            continue;
        }
        visited[v] = true;
        for &u in to[v].iter() {
            if visited[u] {
                continue;
            }
            dbg!((v, u));
            d[u] = min(d[u], d[v] + 1);
            q.push_back(u);
        }
    }

    if d[b + 1] <= m {
        println!("{}", d[b + 1]);
    } else {
        println!("-1");
    }
}
0