結果

問題 No.1647 Travel in Mitaru city 2
ユーザー akakimidoriakakimidori
提出日時 2021-08-13 21:44:25
言語 Rust
(1.77.0)
結果
AC  
実行時間 75 ms / 2,500 ms
コード長 4,883 bytes
コンパイル時間 2,773 ms
コンパイル使用メモリ 166,656 KB
実行使用メモリ 30,080 KB
最終ジャッジ日時 2024-04-21 04:47:25
合計ジャッジ時間 8,155 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 64 ms
27,904 KB
testcase_09 AC 59 ms
25,856 KB
testcase_10 AC 61 ms
27,776 KB
testcase_11 AC 51 ms
22,272 KB
testcase_12 AC 56 ms
24,064 KB
testcase_13 AC 45 ms
21,632 KB
testcase_14 AC 68 ms
28,544 KB
testcase_15 AC 67 ms
28,160 KB
testcase_16 AC 57 ms
26,880 KB
testcase_17 AC 51 ms
21,888 KB
testcase_18 AC 46 ms
23,040 KB
testcase_19 AC 58 ms
23,680 KB
testcase_20 AC 51 ms
23,936 KB
testcase_21 AC 51 ms
24,448 KB
testcase_22 AC 67 ms
29,312 KB
testcase_23 AC 69 ms
29,056 KB
testcase_24 AC 64 ms
25,856 KB
testcase_25 AC 66 ms
27,520 KB
testcase_26 AC 62 ms
28,800 KB
testcase_27 AC 68 ms
29,568 KB
testcase_28 AC 73 ms
30,080 KB
testcase_29 AC 69 ms
29,696 KB
testcase_30 AC 49 ms
23,808 KB
testcase_31 AC 74 ms
29,568 KB
testcase_32 AC 65 ms
27,264 KB
testcase_33 AC 61 ms
26,496 KB
testcase_34 AC 70 ms
29,824 KB
testcase_35 AC 57 ms
24,192 KB
testcase_36 AC 74 ms
29,568 KB
testcase_37 AC 75 ms
29,952 KB
testcase_38 AC 62 ms
26,880 KB
testcase_39 AC 53 ms
24,192 KB
testcase_40 AC 63 ms
26,880 KB
testcase_41 AC 60 ms
25,216 KB
testcase_42 AC 69 ms
26,880 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 8 ms
10,496 KB
testcase_45 AC 32 ms
19,840 KB
testcase_46 AC 47 ms
19,968 KB
testcase_47 AC 47 ms
23,680 KB
testcase_48 AC 35 ms
19,840 KB
testcase_49 AC 51 ms
23,424 KB
testcase_50 AC 39 ms
21,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//---------- begin union_find ----------
pub struct DSU {
    p: Vec<i32>,
}
impl DSU {
    pub fn new(n: usize) -> DSU {
        assert!(n < std::i32::MAX as usize);
        DSU { p: vec![-1; n] }
    }
    pub fn init(&mut self) {
        self.p.iter_mut().for_each(|p| *p = -1);
    }
    pub fn root(&self, mut x: usize) -> usize {
        assert!(x < self.p.len());
        while self.p[x] >= 0 {
            x = self.p[x] as usize;
        }
        x
    }
    pub fn same(&self, x: usize, y: usize) -> bool {
        assert!(x < self.p.len() && y < self.p.len());
        self.root(x) == self.root(y)
    }
    pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
        assert!(x < self.p.len() && y < self.p.len());
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return None;
        }
        if self.p[x] > self.p[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.p[x] += self.p[y];
        self.p[y] = x as i32;
        Some((x, y))
    }
    pub fn parent(&self, x: usize) -> Option<usize> {
        assert!(x < self.p.len());
        let p = self.p[x];
        if p >= 0 {
            Some(p as usize)
        } else {
            None
        }
    }
    pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
    where
        F: FnMut(usize),
    {
        while let Some(p) = self.parent(x) {
            f(x);
            x = p;
        }
        x
    }
    pub fn size(&self, x: usize) -> usize {
        assert!(x < self.p.len());
        let r = self.root(x);
        (-self.p[r]) as usize
    }
}
//---------- end union_find ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

fn run() {
    input! {
        h: usize,
        w: usize,
        n: usize,
        e: [(usize1, usize1); n],
    }
    let mut dsu = DSU::new(h + w);
    let mut g = vec![vec![]; h + w];
    for (i, &(a, b)) in e.iter().enumerate() {
        let b = b + h;
        if dsu.unite(a, b).is_some() {
            g[a].push((b, i));
            g[b].push((a, i));
        } else {
            let root = dsu.root(a);
            let mut topo = vec![root];
            let mut depth = vec![0; h + w];
            let mut parent = vec![(root, n); h + w];
            for i in 0.. {
                if i >= topo.len() {
                    break;
                }
                let v = topo[i];
                for &(u, k) in g[v].iter() {
                    if u != parent[v].0 {
                        parent[u] = (v, k);
                        depth[u] = depth[v] + 1;
                        topo.push(u);
                    }
                }
            }
            let mut a = a;
            let mut b = b;
            let mut x = vec![];
            let mut y = vec![];
            while a != b {
                if depth[a] >= depth[b] {
                    let (p, k) = parent[a];
                    x.push(k);
                    a = p;
                } else {
                    let (p, k) = parent[b];
                    y.push(k);
                    b = p;
                }
            }
            x.extend(y.iter().rev().cloned());
            x.push(i);
            if e[x[0]].1 != e[x[1]].1 {
                x.rotate_left(1);
            }
            let s = x.iter().map(|x| (*x + 1).to_string()).collect::<Vec<_>>().join(" ");
            println!("{}", x.len());
            println!("{}", s);
            return;
        }
    }
    println!("-1");
}

fn main() {
    run();
}
0