結果

問題 No.1254 補強への架け橋
ユーザー fukafukatanifukafukatani
提出日時 2020-10-09 22:51:00
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 5,396 bytes
コンパイル時間 13,605 ms
コンパイル使用メモリ 379,424 KB
実行使用メモリ 58,820 KB
最終ジャッジ日時 2024-07-20 13:18:48
合計ジャッジ時間 24,303 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 1 ms
5,376 KB
testcase_49 AC 1 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 1 ms
5,376 KB
testcase_57 AC 2 ms
5,376 KB
testcase_58 AC 2 ms
5,376 KB
testcase_59 AC 1 ms
5,376 KB
testcase_60 AC 1 ms
5,376 KB
testcase_61 AC 2 ms
5,376 KB
testcase_62 AC 1 ms
5,376 KB
testcase_63 AC 11 ms
7,040 KB
testcase_64 AC 3 ms
5,376 KB
testcase_65 AC 6 ms
5,376 KB
testcase_66 AC 6 ms
5,376 KB
testcase_67 AC 4 ms
5,376 KB
testcase_68 AC 6 ms
5,376 KB
testcase_69 AC 9 ms
6,068 KB
testcase_70 AC 4 ms
5,376 KB
testcase_71 AC 3 ms
5,376 KB
testcase_72 AC 8 ms
6,136 KB
testcase_73 AC 4 ms
5,376 KB
testcase_74 AC 7 ms
5,504 KB
testcase_75 AC 6 ms
5,376 KB
testcase_76 AC 2 ms
5,376 KB
testcase_77 AC 6 ms
5,376 KB
testcase_78 AC 10 ms
6,784 KB
testcase_79 AC 10 ms
6,768 KB
testcase_80 AC 9 ms
6,272 KB
testcase_81 AC 10 ms
6,900 KB
testcase_82 AC 8 ms
6,656 KB
testcase_83 AC 121 ms
47,712 KB
testcase_84 AC 85 ms
46,680 KB
testcase_85 AC 48 ms
28,880 KB
testcase_86 AC 76 ms
42,220 KB
testcase_87 AC 83 ms
44,988 KB
testcase_88 AC 12 ms
8,704 KB
testcase_89 AC 87 ms
47,416 KB
testcase_90 AC 58 ms
34,912 KB
testcase_91 AC 43 ms
25,296 KB
testcase_92 AC 22 ms
14,376 KB
testcase_93 AC 72 ms
40,616 KB
testcase_94 AC 79 ms
37,868 KB
testcase_95 AC 77 ms
37,776 KB
testcase_96 AC 89 ms
45,544 KB
testcase_97 AC 38 ms
22,628 KB
testcase_98 AC 102 ms
45,984 KB
testcase_99 AC 47 ms
28,056 KB
testcase_100 AC 98 ms
48,356 KB
testcase_101 AC 20 ms
13,256 KB
testcase_102 AC 11 ms
7,936 KB
testcase_103 AC 23 ms
14,152 KB
testcase_104 AC 33 ms
20,048 KB
testcase_105 AC 71 ms
40,472 KB
testcase_106 AC 40 ms
24,100 KB
testcase_107 AC 90 ms
47,492 KB
testcase_108 AC 87 ms
47,040 KB
testcase_109 AC 70 ms
40,144 KB
testcase_110 AC 64 ms
37,404 KB
testcase_111 AC 67 ms
39,304 KB
testcase_112 AC 30 ms
19,596 KB
testcase_113 AC 61 ms
36,780 KB
testcase_114 AC 38 ms
23,396 KB
testcase_115 AC 14 ms
10,412 KB
testcase_116 AC 43 ms
26,080 KB
testcase_117 AC 32 ms
19,448 KB
testcase_118 AC 85 ms
45,464 KB
testcase_119 AC 47 ms
27,876 KB
testcase_120 AC 83 ms
44,384 KB
testcase_121 AC 26 ms
15,360 KB
testcase_122 AC 43 ms
25,768 KB
testcase_123 AC 1 ms
5,376 KB
testcase_124 AC 131 ms
56,364 KB
testcase_125 AC 131 ms
58,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> src/main.rs:22:9
   |
22 |     for i in 0..n {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: fields `from` and `to` are never read
   --> src/main.rs:158:5
    |
157 | struct Edge {
    |        ---- fields in this struct
158 |     from: usize,
    |     ^^^^
159 |     to: usize,
    |     ^^
    |
    = note: `Edge` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
    = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let n = read::<usize>();

    let mut pairs = vec![];
    for i in 0..n {
        let v = read_vec::<usize>();
        let (a, b) = (v[0] - 1, v[1] - 1);
        pairs.push((a, b));
    }
    let mut uft = UnionFindTree::new(n);
    let mut not_used = 0;
    for i in 0..n {
        let (a, b) = pairs[i];
        if uft.same(a, b) {
            not_used = i;
            break;
        }
        uft.unite(a, b);
    }

    let mut tree = vec![vec![]; n];
    for i in 0..n {
        if i == not_used {
            continue;
        }
        let (a, b) = pairs[i];
        tree[a].push(b);
        tree[b].push(a);
    }

    let lca = LCA::new(&tree);
    let (a, b) = pairs[not_used];
    let lca_node = lca.lca(a, b);

    let mut answers = vec![(a, b)];

    for &start in &[a, b] {
        let mut cur = start;
        while cur != lca_node {
            let parent = lca.parent[0][cur].unwrap();
            answers.push((cur, parent));
            cur = parent;
        }
    }
    // debug!(answers);

    let mut index_map = HashMap::new();
    for i in 0..n {
        let (u, v) = pairs[i];
        index_map.insert((u, v), i);
        index_map.insert((v, u), i);
    }

    println!("{}", answers.len());
    for (u, v) in answers {
        print!("{} ", index_map[&(u, v)] + 1);
    }
    println!("");
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

struct LCA {
    depth: Vec<usize>,
    parent: Vec<Vec<Option<usize>>>,
    log_v: usize,
}

impl LCA {
    fn new(g: &Vec<Vec<usize>>) -> LCA {
        LCA::with_root(g, 0)
    }

    fn with_root(g: &Vec<Vec<usize>>, root: usize) -> LCA {
        let n = g.len();
        let log_v = (1..).find(|i| 1usize << i > n).unwrap();
        let mut lca = LCA {
            depth: vec![0usize; n],
            parent: vec![vec![None; n]; 18 + 1], // support 2 ^ 20
            log_v: log_v,
        };
        lca.dfs(root, None, 0, &g);
        lca.doubling(g.len());
        lca
    }

    fn doubling(&mut self, n: usize) {
        for k in 0..self.log_v {
            for v in 0..n {
                if let Some(parent_v) = self.parent[k][v] {
                    self.parent[k + 1][v] = self.parent[k][parent_v];
                }
            }
        }
    }

    fn dfs(&mut self, cur: usize, p: Option<usize>, cur_depth: usize, g: &Vec<Vec<usize>>) {
        self.parent[0][cur] = p;
        self.depth[cur] = cur_depth;
        for &to in g[cur].iter() {
            if Some(to) == p {
                continue;
            }
            self.dfs(to, Some(cur), cur_depth + 1, g);
        }
    }

    fn lca(&self, mut u: usize, mut v: usize) -> usize {
        if self.depth[u] > self.depth[v] {
            std::mem::swap(&mut u, &mut v);
        }
        while self.depth[v] != self.depth[u] {
            v = self.parent[(self.depth[v] - self.depth[u]).trailing_zeros() as usize][v].unwrap();
        }
        if u == v {
            return u;
        }
        for k in (0..self.parent.len()).rev() {
            if self.parent[k][u] != self.parent[k][v] {
                u = self.parent[k][u].unwrap();
                v = self.parent[k][v].unwrap();
            }
        }
        self.parent[0][u].unwrap()
    }
}

#[derive(Debug, Clone)]
struct Edge {
    from: usize,
    to: usize,
    cost: i32,
}

impl PartialEq for Edge {
    fn eq(&self, other: &Edge) -> bool {
        self.cost == other.cost
    }
}

impl Eq for Edge {}

impl Ord for Edge {
    fn cmp(&self, other: &Self) -> Ordering {
        self.cost.cmp(&other.cost)
    }
}

impl PartialOrd for Edge {
    fn partial_cmp(&self, other: &Edge) -> Option<Ordering> {
        Some(self.cost.cmp(&other.cost))
    }
}

struct UnionFindTree {
    parent_or_size: Vec<isize>,
}

impl UnionFindTree {
    fn new(size: usize) -> UnionFindTree {
        UnionFindTree {
            parent_or_size: vec![-1; size],
        }
    }

    fn find(&self, index: usize) -> usize {
        let mut index = index;
        while self.parent_or_size[index] >= 0 {
            index = self.parent_or_size[index] as usize;
        }
        index
    }

    fn same(&self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }

    fn unite(&mut self, index0: usize, index1: usize) -> bool {
        let a = self.find(index0);
        let b = self.find(index1);
        if a == b {
            false
        } else {
            if self.parent_or_size[a] < self.parent_or_size[b] {
                self.parent_or_size[a] += self.parent_or_size[b];
                self.parent_or_size[b] = a as isize;
            } else {
                self.parent_or_size[b] += self.parent_or_size[a];
                self.parent_or_size[a] = b as isize;
            }
            true
        }
    }
}
0