結果

問題 No.1424 Ultrapalindrome
ユーザー phsplsphspls
提出日時 2022-11-15 00:58:57
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,441 bytes
コンパイル時間 1,138 ms
コンパイル使用メモリ 159,984 KB
実行使用メモリ 19,884 KB
最終ジャッジ日時 2023-10-14 07:49:47
合計ジャッジ時間 4,330 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 0 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 WA -
testcase_09 AC 35 ms
10,044 KB
testcase_10 AC 32 ms
10,128 KB
testcase_11 AC 22 ms
7,720 KB
testcase_12 AC 33 ms
9,612 KB
testcase_13 AC 8 ms
4,352 KB
testcase_14 AC 22 ms
8,276 KB
testcase_15 AC 0 ms
4,352 KB
testcase_16 AC 20 ms
6,944 KB
testcase_17 AC 24 ms
8,012 KB
testcase_18 AC 19 ms
5,968 KB
testcase_19 AC 30 ms
7,764 KB
testcase_20 AC 7 ms
4,348 KB
testcase_21 AC 23 ms
6,688 KB
testcase_22 AC 13 ms
4,780 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 6 ms
4,352 KB
testcase_25 AC 5 ms
4,352 KB
testcase_26 AC 30 ms
9,072 KB
testcase_27 AC 61 ms
12,732 KB
testcase_28 AC 31 ms
11,988 KB
testcase_29 AC 38 ms
19,884 KB
testcase_30 AC 33 ms
13,692 KB
testcase_31 AC 35 ms
13,680 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> 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

warning: 1 warning emitted

ソースコード

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