結果

問題 No.1976 Cut then Connect
ユーザー akakimidoriakakimidori
提出日時 2022-06-10 22:23:11
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 3,240 bytes
コンパイル時間 16,778 ms
コンパイル使用メモリ 380,092 KB
実行使用メモリ 13,300 KB
最終ジャッジ日時 2024-11-24 12:14:49
合計ジャッジ時間 13,217 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 41 ms
12,760 KB
testcase_03 AC 20 ms
8,292 KB
testcase_04 AC 8 ms
5,248 KB
testcase_05 AC 18 ms
7,368 KB
testcase_06 AC 28 ms
10,540 KB
testcase_07 AC 18 ms
7,612 KB
testcase_08 AC 40 ms
13,300 KB
testcase_09 AC 34 ms
11,552 KB
testcase_10 AC 9 ms
5,248 KB
testcase_11 AC 41 ms
13,088 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 30 ms
10,412 KB
testcase_14 AC 26 ms
9,716 KB
testcase_15 AC 25 ms
9,472 KB
testcase_16 AC 36 ms
12,256 KB
testcase_17 AC 11 ms
5,732 KB
testcase_18 AC 3 ms
5,248 KB
testcase_19 AC 28 ms
10,372 KB
testcase_20 AC 38 ms
13,268 KB
testcase_21 AC 21 ms
8,556 KB
testcase_22 AC 1 ms
5,248 KB
testcase_23 AC 1 ms
5,248 KB
testcase_24 AC 0 ms
5,248 KB
testcase_25 AC 1 ms
5,248 KB
testcase_26 AC 0 ms
5,248 KB
testcase_27 AC 1 ms
5,248 KB
testcase_28 AC 1 ms
5,248 KB
testcase_29 AC 1 ms
5,248 KB
testcase_30 AC 1 ms
5,248 KB
testcase_31 AC 1 ms
5,248 KB
testcase_32 AC 0 ms
5,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::Write`
 --> src/main.rs:2:5
  |
2 | use std::io::Write;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `c`
  --> src/main.rs:52:18
   |
52 |             let (c, d) = under[u];
   |                  ^ help: if this is intentional, prefix it with an underscore: `_c`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: type alias `Map` is never used
 --> src/main.rs:4:6
  |
4 | type Map<K, V> = BTreeMap<K, V>;
  |      ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
 --> src/main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

warning: type alias `Deque` is never used
 --> src/main.rs:6:6
  |
6 | type Deque<T> = VecDeque<T>;
  |      ^^^^^

ソースコード

diff #

use std::collections::*;
use std::io::Write;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn run() {
    input! {
        n: usize,
        e: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for (a, b) in e {
        g[a].push(b);
        g[b].push(a);
    }
    let root = 0;
    let mut topo = vec![root];
    let mut parent = vec![n; n];
    for i in 0..n {
        let v = topo[i];
        for u in g[v].clone() {
            parent[u] = v;
            g[u].retain(|p| *p != v);
            topo.push(u);
        }
    }
    // 子までのmax, 直径
    let mut dp = vec![(0, 0); n];
    for &v in topo.iter().rev() {
        let mut val = (0, 0);
        for &u in g[v].iter() {
            let (a, b) = dp[u];
            val = (val.0.max(a + 1), b.max(val.1).max(val.0 + 1 + a));
        }
        dp[v] = val;
    }
    let under = dp;
    let mut up = vec![(0, 0); n];
    let mut ans = n as i32 - 1;
    for &v in topo.iter() {
        let mut stack = vec![up[v]];
        for (i, &u) in g[v].iter().enumerate() {
            let (a, b) = under[u];
            let val = stack[i];
            stack.push((val.0.max(a + 1), b.max(val.1).max(val.0 + 1 + a)));
        }
        let mut val = (0, 0);
        for (i, &u) in g[v].iter().enumerate().rev() {
            let (a, b) = stack[i];
            let (c, d) = under[u];
            let p = a.max(val.0);
            let q = b.max(val.1).max(val.0 + a);
            ans = ans.min(q.max(d).max((q + 1) / 2 + 1 + (d + 1) / 2));
            up[u] = (p + 1, q.max(p + 1));
            let (a, b) = under[u];
            val = (val.0.max(a + 1), b.max(val.1).max(val.0 + 1 + a));
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
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_export]
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_export]
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 ----------
0