提出 #9089536


ソースコード 拡げる

// Practice AtCoder
// author: Leonardone

// 解説PDF読後

fn main() {
    let mut stdin = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap();
    let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    macro_rules! get {
        ($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(usize));
    }
    
    let n = get!();
    let u = get!() - 1;
    let v = get!() - 1;
    let mut graph = vec![vec![]; n];
    for _ in 1..n {
        let a = get!() - 1;
        let b = get!() - 1;
        graph[a].push(b);
        graph[b].push(a);
    }
    
    let mut ends = vec![false; n];
    for i in 0..n {
        if graph[i].len() == 1 {
            ends[i] = true;
        }
    }
    
    let mut t_dist = make_dist(&graph, u);
    let mut cl = t_dist.clone();
    let a_dist = make_dist(&graph, v);
    
    for i in 0..n {
        if t_dist[i].0 >= a_dist[i].0 {
            t_dist[i].0 = 0;
        }
    }
    // println!("{:?}", cl);
    // println!("{:?}", t_dist);
    // println!("{:?}", a_dist);
    
    let mut ans = n + n;
    
    let mut stack = vec![u];
    while let Some(x) = stack.pop() {
        if t_dist[x].0 == 0 {
            ans = std::cmp::min(ans, a_dist[x].0 - 1);
            continue;
        }
        let mut cnt = 0;
        for &y in &graph[x] {
            if t_dist[y].0 > t_dist[x].0 {
                stack.push(y);
                cnt += 1;
            }
        }
        if cnt == 0 {
            ans = std::cmp::min(ans, a_dist[x].0 - 2);
        }
    }
    if ans < n + n {
        println!("{}", ans);
        return;
    }
    let e = t_dist
        .iter()
        .enumerate()
        .map(|e| (e.1, e.0))
        .max();
    if let Some(e) = e {
        // let d = ((e.0).0 & 1) ^ (a_dist[e.1].0 & 1);
        ans = std::cmp::min(ans, a_dist[e.1].0 - 1);
    }
    println!("{}", ans);
}

fn make_dist(graph: &Vec<Vec<usize>>, s: usize) -> Vec<(usize,usize)> {
    let mut dist = vec![(0, 0); graph.len()];
    dist[s] = (1, s);
    let mut stack = vec![s];
    while let Some(x) = stack.pop() {
        let c = dist[x].0 + 1;
        for &y in &graph[x] {
            if dist[y].0 == 0 {
                dist[y] = (c, x);
                stack.push(y);
            }
        }
    }
    dist
}

提出情報

提出日時
問題 F - Playing Tag on Tree
ユーザ neetsdkasu
言語 Rust (1.15.1)
得点 0
コード長 2426 Byte
結果 WA
実行時間 33 ms
メモリ 20732 KiB

コンパイルエラー

warning: unused variable: `cl`, #[warn(unused_variables)] on by default
  --> ./Main.rs:35:9
   |
35 |     let mut cl = t_dist.clone();
   |         ^^^^^^

warning: variable does not need to be mutable, #[warn(unused_mut)] on by default
  --> ./Main.rs:35:9
   |
35 |     let mut cl = t_dist.clone();
   |         ^^^^^^

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 600
結果
AC × 4
AC × 7
WA × 19
セット名 テストケース
Sample sample_01, sample_02, sample_03, sample_04
All path1_01, path1_02, path1_03, path2_01, path2_02, path2_03, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, random_11, random_12, random_13, sample_01, sample_02, sample_03, sample_04, star_01, star_02, star_03
ケース名 結果 実行時間 メモリ
path1_01 WA 26 ms 14588 KiB
path1_02 WA 17 ms 10492 KiB
path1_03 WA 23 ms 14588 KiB
path2_01 WA 32 ms 18684 KiB
path2_02 WA 33 ms 18684 KiB
path2_03 WA 33 ms 18684 KiB
random_01 WA 2 ms 4352 KiB
random_02 WA 3 ms 4352 KiB
random_03 WA 4 ms 4352 KiB
random_04 WA 3 ms 4352 KiB
random_05 WA 4 ms 4352 KiB
random_06 WA 17 ms 10492 KiB
random_07 WA 27 ms 14588 KiB
random_08 WA 17 ms 10492 KiB
random_09 WA 20 ms 14588 KiB
random_10 WA 28 ms 18684 KiB
random_11 WA 32 ms 18684 KiB
random_12 WA 30 ms 18684 KiB
random_13 WA 30 ms 18684 KiB
sample_01 AC 2 ms 4352 KiB
sample_02 AC 2 ms 4352 KiB
sample_03 AC 2 ms 4352 KiB
sample_04 AC 2 ms 4352 KiB
star_01 AC 19 ms 20732 KiB
star_02 AC 19 ms 20732 KiB
star_03 AC 21 ms 20732 KiB