結果

問題 No.1647 Travel in Mitaru city 2
ユーザー ikdikd
提出日時 2021-08-14 16:18:17
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 85 ms / 2,500 ms
コード長 3,714 bytes
コンパイル時間 12,345 ms
コンパイル使用メモリ 394,832 KB
実行使用メモリ 28,032 KB
最終ジャッジ日時 2024-10-13 03:28:21
合計ジャッジ時間 18,970 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 0 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 71 ms
25,840 KB
testcase_09 AC 69 ms
23,296 KB
testcase_10 AC 72 ms
25,744 KB
testcase_11 AC 54 ms
19,840 KB
testcase_12 AC 56 ms
19,524 KB
testcase_13 AC 56 ms
20,736 KB
testcase_14 AC 70 ms
26,240 KB
testcase_15 AC 74 ms
26,104 KB
testcase_16 AC 66 ms
24,704 KB
testcase_17 AC 55 ms
20,960 KB
testcase_18 AC 60 ms
20,352 KB
testcase_19 AC 69 ms
25,976 KB
testcase_20 AC 70 ms
24,064 KB
testcase_21 AC 74 ms
26,700 KB
testcase_22 AC 71 ms
26,964 KB
testcase_23 AC 75 ms
27,136 KB
testcase_24 AC 73 ms
24,444 KB
testcase_25 AC 69 ms
25,424 KB
testcase_26 AC 74 ms
27,136 KB
testcase_27 AC 71 ms
27,392 KB
testcase_28 AC 78 ms
28,032 KB
testcase_29 AC 85 ms
28,032 KB
testcase_30 AC 72 ms
26,240 KB
testcase_31 AC 84 ms
27,928 KB
testcase_32 AC 83 ms
27,520 KB
testcase_33 AC 68 ms
24,936 KB
testcase_34 AC 83 ms
28,032 KB
testcase_35 AC 78 ms
25,224 KB
testcase_36 AC 85 ms
28,032 KB
testcase_37 AC 81 ms
28,032 KB
testcase_38 AC 72 ms
23,296 KB
testcase_39 AC 68 ms
20,992 KB
testcase_40 AC 65 ms
23,000 KB
testcase_41 AC 62 ms
21,120 KB
testcase_42 AC 64 ms
23,168 KB
testcase_43 AC 1 ms
5,248 KB
testcase_44 AC 4 ms
6,528 KB
testcase_45 AC 51 ms
18,304 KB
testcase_46 AC 67 ms
18,304 KB
testcase_47 AC 55 ms
18,304 KB
testcase_48 AC 54 ms
18,304 KB
testcase_49 AC 50 ms
18,300 KB
testcase_50 AC 61 ms
18,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//! # Bundled libraries
//!
//! - `procon_reader 0.1.0 (git+https://github.com/ia7ck/rust-competitive-programming#bd4907c0d20d80d0c7a19f92b3bb682e6326d08d)` licensed under **missing** as `crate::__cargo_equip::crates::procon_reader`

pub use __cargo_equip::prelude::*;

use procon_reader::ProconReader;

fn main() {
    let stdin = std::io::stdin();
    let mut rd = ProconReader::new(stdin.lock());

    let h: usize = rd.get();
    let w: usize = rd.get();
    let n: usize = rd.get();
    let mut cities = Vec::new();
    let mut g = vec![vec![]; h + w];
    for i in 0..n {
        let x: usize = rd.get();
        let y: usize = rd.get();
        cities.push((x, y));
        g[x - 1].push((h + (y - 1), i));
        g[h + (y - 1)].push((x - 1, i));
    }
    let mut seen_node = vec![false; h + w];
    let mut seen_edge = vec![false; n];
    for u in 0..(h + w) {
        if seen_node[u] {
            continue;
        }
        let mut cycle = Vec::new();
        dfs(u, &g, &mut seen_node, &mut seen_edge, &mut cycle);
        if cycle.is_empty() {
            continue;
        }
        assert!(cycle.len() >= 4);
        cycle.reverse();
        let (_, y0) = cities[cycle[0]];
        let (_, y1) = cities[cycle[1]];
        let (_, y2) = cities[cycle[2]];
        if y0 != y1 {
            assert_eq!(y1, y2);
            cycle.rotate_left(1);
        }
        println!("{}", cycle.len());
        for t in cycle {
            // println!("{:?}", cities[t]);
            print!("{} ", t + 1);
        }
        return;
    }
    println!("-1");
}

fn dfs(
    u: usize,
    g: &Vec<Vec<(usize, usize)>>,
    seen_node: &mut Vec<bool>,
    seen_edge: &mut Vec<bool>,
    cycle: &mut Vec<usize>,
) -> Option<(bool, usize)> {
    seen_node[u] = true;
    for &(v, idx) in &g[u] {
        if seen_edge[idx] {
            continue;
        }
        seen_edge[idx] = true;
        if seen_node[v] {
            cycle.push(idx);
            return Some((false, v));
        }
        if let Some((finish, head)) = dfs(v, g, seen_node, seen_edge, cycle) {
            if finish {
                return Some((finish, head));
            }
            cycle.push(idx);
            return Some((u == head, head));
        }
    }
    None
}

// The following code was expanded by `cargo-equip`.

#[rustfmt::skip]
#[allow(unused)]
mod __cargo_equip {
    pub(crate) mod crates {
        pub mod procon_reader {use std::io::BufRead;use std::str::FromStr;pub struct ProconReader<R>{r:R,l:String,i:usize,}impl<R:BufRead>ProconReader<R>{pub fn new(reader:R)->Self{Self{r:reader,l:String::new(),i:0,}}pub fn get<T>(&mut self)->T where T:FromStr,<T as FromStr>::Err:std::fmt::Debug,{self.skip_blanks();assert!(self.i<self.l.len());assert_ne!(&self.l[self.i..=self.i]," ");let rest=&self.l[self.i..];let len=rest.find(' ').unwrap_or_else(| |rest.len());let val=rest[..len].parse().unwrap_or_else(|e|panic!("{:?}, attempt to read `{}`",e,rest));self.i+=len;val}fn skip_blanks(&mut self){loop{match self.l[self.i..].find(|ch|ch!=' '){Some(j)=>{self.i+=j;break;}None=>{let mut buf=String::new();let num_bytes=self.r.read_line(&mut buf).unwrap_or_else(|_|panic!("invalid UTF-8"));assert!(num_bytes>0,"reached EOF :(");self.l=buf.trim_end_matches('\n').trim_end_matches('\r').to_string();self.i=0;}}}}pub fn get_vec<T>(&mut self,n:usize)->Vec<T>where T:FromStr,<T as FromStr>::Err:std::fmt::Debug,{(0..n).map(|_|self.get()).collect()}pub fn get_chars(&mut self)->Vec<char>{self.get::<String>().chars().collect()}}}
    }

    pub(crate) mod macros {
        pub mod procon_reader {}
    }

    pub(crate) mod prelude {pub use crate::__cargo_equip::crates::*;}

    mod preludes {
        pub mod procon_reader {}
    }
}
0