結果

問題 No.1254 補強への架け橋
ユーザー fukafukatanifukafukatani
提出日時 2020-10-09 22:51:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 5,396 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 167,568 KB
実行使用メモリ 58,808 KB
最終ジャッジ日時 2023-09-27 18:49:36
合計ジャッジ時間 11,900 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,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 0 ms
4,380 KB
testcase_17 AC 1 ms
4,376 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,380 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 0 ms
4,380 KB
testcase_26 AC 0 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,500 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 1 ms
4,376 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 1 ms
4,376 KB
testcase_63 AC 10 ms
7,136 KB
testcase_64 AC 3 ms
4,376 KB
testcase_65 AC 7 ms
5,172 KB
testcase_66 AC 5 ms
4,632 KB
testcase_67 AC 3 ms
4,376 KB
testcase_68 AC 6 ms
4,724 KB
testcase_69 AC 8 ms
6,208 KB
testcase_70 AC 3 ms
4,376 KB
testcase_71 AC 3 ms
4,376 KB
testcase_72 AC 8 ms
6,300 KB
testcase_73 AC 3 ms
4,376 KB
testcase_74 AC 6 ms
5,348 KB
testcase_75 AC 6 ms
4,748 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 5 ms
4,376 KB
testcase_78 AC 9 ms
6,712 KB
testcase_79 AC 9 ms
6,892 KB
testcase_80 AC 8 ms
6,440 KB
testcase_81 AC 10 ms
6,868 KB
testcase_82 AC 8 ms
6,724 KB
testcase_83 AC 92 ms
47,748 KB
testcase_84 AC 90 ms
46,644 KB
testcase_85 AC 51 ms
28,940 KB
testcase_86 AC 79 ms
41,992 KB
testcase_87 AC 85 ms
44,960 KB
testcase_88 AC 13 ms
8,868 KB
testcase_89 AC 90 ms
47,420 KB
testcase_90 AC 61 ms
34,856 KB
testcase_91 AC 44 ms
25,312 KB
testcase_92 AC 23 ms
14,404 KB
testcase_93 AC 71 ms
40,460 KB
testcase_94 AC 65 ms
37,792 KB
testcase_95 AC 67 ms
37,688 KB
testcase_96 AC 85 ms
45,624 KB
testcase_97 AC 38 ms
22,644 KB
testcase_98 AC 89 ms
45,880 KB
testcase_99 AC 51 ms
28,128 KB
testcase_100 AC 93 ms
48,368 KB
testcase_101 AC 21 ms
13,344 KB
testcase_102 AC 12 ms
8,040 KB
testcase_103 AC 23 ms
14,316 KB
testcase_104 AC 34 ms
20,192 KB
testcase_105 AC 72 ms
40,468 KB
testcase_106 AC 42 ms
24,108 KB
testcase_107 AC 90 ms
47,440 KB
testcase_108 AC 88 ms
46,952 KB
testcase_109 AC 73 ms
40,092 KB
testcase_110 AC 66 ms
37,512 KB
testcase_111 AC 71 ms
39,268 KB
testcase_112 AC 33 ms
19,496 KB
testcase_113 AC 64 ms
36,668 KB
testcase_114 AC 40 ms
23,560 KB
testcase_115 AC 16 ms
10,552 KB
testcase_116 AC 47 ms
26,060 KB
testcase_117 AC 32 ms
19,652 KB
testcase_118 AC 82 ms
45,392 KB
testcase_119 AC 53 ms
27,784 KB
testcase_120 AC 83 ms
44,308 KB
testcase_121 AC 24 ms
15,352 KB
testcase_122 AC 42 ms
25,804 KB
testcase_123 AC 1 ms
4,380 KB
testcase_124 AC 132 ms
56,116 KB
testcase_125 AC 135 ms
58,808 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> 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
   --> 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

warning: 2 warnings emitted

ソースコード

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