結果

問題 No.1424 Ultrapalindrome
ユーザー tonyu0tonyu0
提出日時 2021-03-12 23:13:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,380 bytes
コンパイル時間 1,499 ms
コンパイル使用メモリ 144,916 KB
実行使用メモリ 17,848 KB
最終ジャッジ日時 2023-08-04 19:11:31
合計ジャッジ時間 2,281 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 18 ms
9,196 KB
testcase_10 AC 16 ms
9,200 KB
testcase_11 AC 12 ms
7,352 KB
testcase_12 AC 16 ms
8,652 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 13 ms
7,576 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 10 ms
6,760 KB
testcase_17 AC 13 ms
7,580 KB
testcase_18 AC 9 ms
5,448 KB
testcase_19 AC 15 ms
7,328 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 12 ms
6,504 KB
testcase_22 AC 8 ms
4,716 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 18 ms
8,364 KB
testcase_27 AC 21 ms
12,076 KB
testcase_28 AC 17 ms
11,748 KB
testcase_29 AC 22 ms
17,848 KB
testcase_30 AC 16 ms
11,580 KB
testcase_31 AC 17 ms
12,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

fn dfs(v: usize, p: usize, g: &Vec<Vec<usize>>) -> usize {
    let mut res = 1;
    for &nv in g[v].iter() {
        if nv != p {
            res += dfs(nv, v, g);
        }
    }
    res
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let mut g: Vec<Vec<usize>> = vec![Vec::new(); n];
    let mut deg: Vec<usize> = vec![0; n];
    for _ in 0..n - 1 {
        let a: usize = itr.next().unwrap().parse::<usize>().unwrap() - 1;
        let b: usize = itr.next().unwrap().parse::<usize>().unwrap() - 1;
        deg[a] += 1;
        deg[b] += 1;
        g[a].push(b);
        g[b].push(a);
    }
    let mut big = n;
    for i in 0..n {
        if deg[i] > 2 {
            if big != n {
                println!("No");
                return;
            }
            big = i;
        }
    }
    if big == n {
        println!("Yes")
    } else {
        let mut same = n;
        let mut ok = true;
        for &v in g[big].iter() {
            if same == n {
                same = dfs(v, big, &g);
            } else {
                ok &= same == dfs(v, big, &g);
            }
        }
        if ok {
            println!("Yes");
        } else {
            println!("No");
        }
    }
}
0