結果

問題 No.1424 Ultrapalindrome
ユーザー tonyu0tonyu0
提出日時 2021-03-12 23:13:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,380 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 154,364 KB
実行使用メモリ 17,024 KB
最終ジャッジ日時 2024-04-22 17:15:33
合計ジャッジ時間 2,631 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 26 ms
8,960 KB
testcase_10 AC 21 ms
8,960 KB
testcase_11 AC 15 ms
6,912 KB
testcase_12 AC 23 ms
8,448 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 17 ms
7,296 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 13 ms
6,144 KB
testcase_17 AC 17 ms
7,040 KB
testcase_18 AC 12 ms
5,376 KB
testcase_19 AC 24 ms
7,168 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 14 ms
6,144 KB
testcase_22 AC 8 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 33 ms
8,064 KB
testcase_27 AC 37 ms
11,264 KB
testcase_28 AC 18 ms
10,880 KB
testcase_29 AC 25 ms
17,024 KB
testcase_30 AC 19 ms
11,264 KB
testcase_31 AC 20 ms
11,648 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