結果

問題 No.1647 Travel in Mitaru city 2
ユーザー koba-e964koba-e964
提出日時 2021-08-17 15:50:37
言語 Rust
(1.77.0)
結果
AC  
実行時間 80 ms / 2,500 ms
コード長 3,586 bytes
コンパイル時間 1,896 ms
コンパイル使用メモリ 172,116 KB
実行使用メモリ 37,376 KB
最終ジャッジ日時 2024-04-21 05:02:18
合計ジャッジ時間 7,466 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 73 ms
33,536 KB
testcase_09 AC 63 ms
29,568 KB
testcase_10 AC 69 ms
33,792 KB
testcase_11 AC 59 ms
23,936 KB
testcase_12 AC 54 ms
22,400 KB
testcase_13 AC 57 ms
24,960 KB
testcase_14 AC 79 ms
34,688 KB
testcase_15 AC 72 ms
34,176 KB
testcase_16 AC 66 ms
32,128 KB
testcase_17 AC 55 ms
25,984 KB
testcase_18 AC 52 ms
23,936 KB
testcase_19 AC 70 ms
34,304 KB
testcase_20 AC 61 ms
30,464 KB
testcase_21 AC 69 ms
35,328 KB
testcase_22 AC 74 ms
35,328 KB
testcase_23 AC 79 ms
35,712 KB
testcase_24 AC 65 ms
30,976 KB
testcase_25 AC 70 ms
32,768 KB
testcase_26 AC 77 ms
35,712 KB
testcase_27 AC 72 ms
36,096 KB
testcase_28 AC 79 ms
37,248 KB
testcase_29 AC 77 ms
37,376 KB
testcase_30 AC 73 ms
34,176 KB
testcase_31 AC 76 ms
37,248 KB
testcase_32 AC 80 ms
36,736 KB
testcase_33 AC 66 ms
31,872 KB
testcase_34 AC 77 ms
37,248 KB
testcase_35 AC 67 ms
32,640 KB
testcase_36 AC 77 ms
37,248 KB
testcase_37 AC 74 ms
37,248 KB
testcase_38 AC 69 ms
28,672 KB
testcase_39 AC 61 ms
24,576 KB
testcase_40 AC 68 ms
28,160 KB
testcase_41 AC 63 ms
24,832 KB
testcase_42 AC 66 ms
28,416 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 5 ms
6,656 KB
testcase_45 AC 49 ms
18,432 KB
testcase_46 AC 58 ms
18,304 KB
testcase_47 AC 49 ms
18,304 KB
testcase_48 AC 54 ms
18,304 KB
testcase_49 AC 48 ms
18,432 KB
testcase_50 AC 49 ms
18,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn dfs(v: usize, par: usize, vis: &mut [bool], g: &[Vec<(usize, usize)>],
) -> Result<(), (bool, usize, Vec<usize>)> {
    if vis[v] {
        return Err((true, v, vec![]));
    }
    vis[v] = true;
    for &(w, idx) in &g[v] {
        if w == par {
            continue;
        }
        let sub = dfs(w, v, vis, g);
        if let Err((extending, orig, mut sub)) = sub {
            if extending {
                sub.push(idx + 1);
            }
            return Err((extending && orig != v, orig, sub));
        }
    }
    Ok(())
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    #[allow(unused)]
    macro_rules! putvec {
        ($v:expr) => {
            for i in 0..$v.len() {
                puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "});
            }
        }
    }
    input! {
        h: usize, w: usize, n: usize,
        xy: [(usize1, usize1); n],
    }
    let mut g = vec![vec![]; h + w];
    for i in 0..n {
        let (x, y) = xy[i];
        g[x].push((h + y, i));
        g[h + y].push((x, i));
    }
    let mut vis = vec![false; h + w];
    for i in 0..h {
        if vis[i] {
            continue;
        }
        let res = dfs(i, h + w, &mut vis, &g);
        if let Err((_, _, mut path)) = res {
            path.reverse();
            if xy[path[0] - 1].1 != xy[path[1] - 1].1 {
                let u = path.pop().unwrap();
                path.insert(0, u);
            }
            puts!("{}\n", path.len());
            putvec!(path);
            return;
        }
    }
    puts!("-1\n");
}
0