結果

問題 No.1424 Ultrapalindrome
ユーザー phspls
提出日時 2022-11-15 00:59:48
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,442 bytes
コンパイル時間 11,632 ms
コンパイル使用メモリ 378,308 KB
実行使用メモリ 18,432 KB
最終ジャッジ日時 2024-09-16 02:58:46
合計ジャッジ時間 13,490 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> src/main.rs:39:54
   |
39 |         let idx = paths.iter().enumerate().filter(|&(i, v)| v.len() >= 3).nth(0).unwrap().0;
   |                                                      ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

ソースコード

diff #

use std::collections::HashSet;


fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<(usize, usize)>>, depth: &mut usize, dists: &mut HashSet<usize>) {
    let mut is_leaf = true;
    for &(v, _) in paths[cur].iter() {
        if v == prev { continue; }
        *depth += 1;
        dfs(v, cur, paths, depth, dists);
        *depth -= 1;
        is_leaf = false;
    }
    if is_leaf {
        dists.insert(*depth);
    }
}

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 size = paths.iter().filter(|&v| v.len() >= 3).count();
    if size >= 2 {
        println!("No");
    } else if size == 0 {
        println!("Yes");
    } else {
        let idx = paths.iter().enumerate().filter(|&(i, v)| v.len() >= 3).nth(0).unwrap().0;
        let mut leaf_dists = HashSet::new();
        let mut depth = 0usize;
        dfs(idx, idx, &paths, &mut depth, &mut leaf_dists);
        if leaf_dists.len() == 1 {
            println!("Yes");
        } else {
            println!("No");
        }
    }
}
0