結果

問題 No.1424 Ultrapalindrome
ユーザー phsplsphspls
提出日時 2022-11-07 23:12:53
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 3,016 bytes
コンパイル時間 14,864 ms
コンパイル使用メモリ 394,684 KB
実行使用メモリ 16,080 KB
最終ジャッジ日時 2024-07-21 06:24:42
合計ジャッジ時間 18,154 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 69 ms
11,452 KB
testcase_10 AC 66 ms
11,572 KB
testcase_11 AC 43 ms
8,712 KB
testcase_12 AC 59 ms
10,940 KB
testcase_13 AC 14 ms
6,944 KB
testcase_14 AC 50 ms
9,252 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 35 ms
7,784 KB
testcase_17 AC 44 ms
8,920 KB
testcase_18 AC 24 ms
6,944 KB
testcase_19 AC 48 ms
8,780 KB
testcase_20 AC 9 ms
6,944 KB
testcase_21 AC 31 ms
7,688 KB
testcase_22 AC 19 ms
6,940 KB
testcase_23 AC 4 ms
6,944 KB
testcase_24 AC 8 ms
6,940 KB
testcase_25 AC 7 ms
6,940 KB
testcase_26 AC 57 ms
10,024 KB
testcase_27 AC 85 ms
15,164 KB
testcase_28 AC 39 ms
13,608 KB
testcase_29 AC 38 ms
13,612 KB
testcase_30 AC 44 ms
15,948 KB
testcase_31 AC 46 ms
16,080 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
  --> src/main.rs:21:9
   |
21 |     let n = paths.len();
   |         ^ help: if this is intentional, prefix it with an underscore: `_n`
   |
   = note: `#[warn(unused_variables)]` on by default

ソースコード

diff #

use std::collections::{VecDeque, HashSet};

const INF: usize = 1usize << 60;

fn tree_diameter(paths: &Vec<Vec<(usize, usize)>>) -> (usize, usize, usize) {
    fn calc(paths: &Vec<Vec<(usize, usize)>>, start: usize) -> Vec<usize> {
        let n = paths.len();
        let mut costs = vec![INF; n];
        costs[start] = 0usize;
        let mut deq = VecDeque::new();
        deq.push_back(start);
        while let Some(u) = deq.pop_front() {
            for &(v, cost) in paths[u].iter() {
                if costs[v] < INF { continue; }
                costs[v] = costs[v].min(costs[u] + cost);
                deq.push_back(v);
            }
        }
        costs
    }
    let n = paths.len();
    let costs = calc(paths, 0);
    let idx = costs.into_iter().enumerate().max_by_key(|&(_, c)| c).unwrap().0;
    let costs = calc(paths, idx);
    let ridx = costs.into_iter().enumerate().max_by_key(|&(_, c)| c).unwrap();
    // start_point, end_point, cost
    (idx, ridx.0, ridx.1)
}

fn get_leaves(cur: usize, paths: &Vec<Vec<(usize, usize)>>) -> Vec<bool> {
    let n = paths.len();
    let mut ret = vec![true; n];
    let mut checked = vec![false; n];
    checked[cur] = true;
    let mut deque = VecDeque::new();
    deque.push_back(cur);
    while let Some(u) = deque.pop_front() {
        for &(v, _) in paths[u].iter() {
            if checked[v] { continue; }
            checked[v] = true;
            ret[u] = false;
            deque.push_back(v);
        }
    }
    ret[cur] = true;
    ret
}

fn get_dists(cur: usize, paths: &Vec<Vec<(usize, usize)>>) -> Vec<usize> {
    let n = paths.len();
    let mut ret = vec![INF; n];
    ret[cur] = 0;
    let mut deque = VecDeque::new();
    deque.push_back(cur);
    while let Some(u) = deque.pop_front() {
        for &(v, _) in paths[u].iter() {
            if ret[v] < INF { continue; }
            ret[v] = ret[u] + 1;
            deque.push_back(v);
        }
    }
    ret
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut paths = vec![vec![]; n];
    for _ in 0..n-1 {
        let mut ab = String::new();
        std::io::stdin().read_line(&mut ab).ok();
        let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = ab[0]-1;
        let b = ab[1]-1;
        paths[a].push((b, 1));
        paths[b].push((a, 1));
    }

    let (left, right, _) = tree_diameter(&paths);
    let leaves = get_leaves(left, &paths);
    let l_dists = get_dists(left, &paths);
    let r_dists = get_dists(right, &paths);
    let mut result = HashSet::new();
    for i in 0..n {
        if !leaves[i] { continue; }
        if i != left {
            result.insert(l_dists[i]);
        }
        if i != right {
            result.insert(r_dists[i]);
        }
    }
    if result.len() == 1 && paths.iter().filter(|&v| v.len() > 2).count() <= 1 {
        println!("Yes");
    } else {
        println!("No");
    }
}
0