結果

問題 No.1254 補強への架け橋
ユーザー akakimidoriakakimidori
提出日時 2020-10-09 22:18:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 3,972 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 154,764 KB
実行使用メモリ 20,400 KB
最終ジャッジ日時 2023-09-27 17:55:41
合計ジャッジ時間 10,965 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 0 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 0 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 1 ms
4,384 KB
testcase_54 AC 1 ms
4,380 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 1 ms
4,380 KB
testcase_58 AC 1 ms
4,380 KB
testcase_59 AC 1 ms
4,376 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 1 ms
4,376 KB
testcase_63 AC 4 ms
4,380 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 3 ms
4,380 KB
testcase_66 AC 2 ms
4,376 KB
testcase_67 AC 1 ms
4,380 KB
testcase_68 AC 3 ms
4,380 KB
testcase_69 AC 3 ms
4,376 KB
testcase_70 AC 2 ms
4,376 KB
testcase_71 AC 1 ms
4,380 KB
testcase_72 AC 3 ms
4,380 KB
testcase_73 AC 1 ms
4,380 KB
testcase_74 AC 3 ms
4,376 KB
testcase_75 AC 3 ms
4,380 KB
testcase_76 AC 1 ms
4,380 KB
testcase_77 AC 2 ms
4,380 KB
testcase_78 AC 3 ms
4,380 KB
testcase_79 AC 4 ms
4,380 KB
testcase_80 AC 3 ms
4,376 KB
testcase_81 AC 3 ms
4,376 KB
testcase_82 AC 3 ms
4,376 KB
testcase_83 AC 40 ms
18,292 KB
testcase_84 AC 39 ms
17,888 KB
testcase_85 AC 21 ms
11,732 KB
testcase_86 AC 30 ms
15,200 KB
testcase_87 AC 35 ms
16,168 KB
testcase_88 AC 5 ms
4,436 KB
testcase_89 AC 42 ms
18,172 KB
testcase_90 AC 22 ms
12,172 KB
testcase_91 AC 17 ms
10,340 KB
testcase_92 AC 10 ms
6,536 KB
testcase_93 AC 30 ms
14,548 KB
testcase_94 AC 29 ms
13,608 KB
testcase_95 AC 27 ms
13,568 KB
testcase_96 AC 38 ms
16,500 KB
testcase_97 AC 15 ms
8,832 KB
testcase_98 AC 35 ms
17,644 KB
testcase_99 AC 19 ms
11,308 KB
testcase_100 AC 39 ms
18,552 KB
testcase_101 AC 9 ms
5,884 KB
testcase_102 AC 5 ms
4,380 KB
testcase_103 AC 10 ms
6,348 KB
testcase_104 AC 13 ms
7,964 KB
testcase_105 AC 27 ms
14,548 KB
testcase_106 AC 15 ms
9,416 KB
testcase_107 AC 38 ms
18,140 KB
testcase_108 AC 38 ms
17,972 KB
testcase_109 AC 29 ms
14,416 KB
testcase_110 AC 28 ms
13,416 KB
testcase_111 AC 27 ms
14,056 KB
testcase_112 AC 12 ms
7,588 KB
testcase_113 AC 23 ms
12,932 KB
testcase_114 AC 15 ms
9,160 KB
testcase_115 AC 6 ms
4,612 KB
testcase_116 AC 18 ms
10,564 KB
testcase_117 AC 11 ms
7,660 KB
testcase_118 AC 33 ms
16,396 KB
testcase_119 AC 19 ms
11,328 KB
testcase_120 AC 33 ms
15,992 KB
testcase_121 AC 11 ms
6,752 KB
testcase_122 AC 19 ms
10,560 KB
testcase_123 AC 1 ms
4,380 KB
testcase_124 AC 60 ms
20,400 KB
testcase_125 AC 63 ms
20,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//---------- begin union_find ----------
pub struct DSU {
    p: Vec<i32>,
}
impl DSU {
    pub fn new(n: usize) -> DSU {
        DSU { p: vec![-1; n] }
    }
    pub fn init(&mut self) {
        for p in self.p.iter_mut() {
            *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());
        assert!(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());
        assert!(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 size(&self, x: usize) -> usize {
        assert!(x < self.p.len());
        let r = self.root(x);
        (-self.p[r]) as usize
    }
}
//---------- end union_find ----------
//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")
    };
}

//

fn run() {
    input! {
        n: usize,
        e: [(usize1, usize1); n],
    }
    let mut dsu = DSU::new(n);
    let mut g = vec![vec![]; n];
    let mut back = (0, 0, 0);
    for (i, &(a, b)) in e.iter().enumerate() {
        if dsu.unite(a, b).is_some() {
            g[a].push((b, i));
            g[b].push((a, i));
        } else {
            back = (a, b, i);
        }
    }
    let mut depth = vec![0; n];
    let mut parent = vec![(n, n); n];
    let mut topo = vec![0];
    for i in 0..n {
        let v = topo[i];
        for &(u, k) in g[v].iter() {
            if u == parent[v].0 {
                continue;
            }
            parent[u] = (v, k);
            depth[u] = depth[v] + 1;
            topo.push(u);
        }
    }
    let (mut x, mut y, k) = back;
    let mut ans = vec![k];
    while x != y {
        if depth[x] < depth[y] {
            std::mem::swap(&mut x, &mut y);
        }
        ans.push(parent[x].1);
        x = parent[x].0;
    }
    ans.sort();
    let mut s = String::from(&format!("{}\n", ans.len()));
    for a in ans {
        s.push_str(&format!("{} ", a + 1));
    }
    s.pop();
    println!("{}", s);
}

fn main() {
    run();
}
0