結果

問題 No.1647 Travel in Mitaru city 2
ユーザー koba-e964koba-e964
提出日時 2021-08-17 15:41:42
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,303 bytes
コンパイル時間 1,787 ms
コンパイル使用メモリ 169,984 KB
実行使用メモリ 37,376 KB
最終ジャッジ日時 2024-04-18 19:52:30
合計ジャッジ時間 10,340 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_04 WA -
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 59 ms
22,400 KB
testcase_13 AC 58 ms
25,088 KB
testcase_14 WA -
testcase_15 AC 72 ms
34,304 KB
testcase_16 AC 68 ms
32,128 KB
testcase_17 AC 55 ms
25,984 KB
testcase_18 AC 52 ms
24,064 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 74 ms
35,840 KB
testcase_24 AC 71 ms
31,104 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 74 ms
37,248 KB
testcase_29 WA -
testcase_30 AC 76 ms
34,048 KB
testcase_31 WA -
testcase_32 AC 69 ms
36,736 KB
testcase_33 AC 63 ms
31,744 KB
testcase_34 AC 75 ms
37,376 KB
testcase_35 AC 69 ms
32,512 KB
testcase_36 WA -
testcase_37 AC 74 ms
37,248 KB
testcase_38 AC 72 ms
28,928 KB
testcase_39 AC 90 ms
24,576 KB
testcase_40 AC 61 ms
28,288 KB
testcase_41 WA -
testcase_42 AC 62 ms
28,416 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 4 ms
6,528 KB
testcase_45 WA -
testcase_46 AC 52 ms
18,048 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます

ソースコード

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];
    let res = dfs(0, h + w, &mut vis, &g);
    if let Err((_, _, path)) = res {
        puts!("{}\n", path.len());
        putvec!(path);
    } else {
        puts!("-1\n");
    }
}
0