結果

問題 No.1424 Ultrapalindrome
ユーザー RheoTommyRheoTommy
提出日時 2021-03-12 21:45:44
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,856 bytes
コンパイル時間 1,400 ms
コンパイル使用メモリ 166,656 KB
実行使用メモリ 19,840 KB
最終ジャッジ日時 2024-04-22 12:35:42
合計ジャッジ時間 3,102 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 40 ms
7,424 KB
testcase_10 AC 42 ms
7,552 KB
testcase_11 AC 26 ms
5,888 KB
testcase_12 AC 37 ms
7,168 KB
testcase_13 AC 9 ms
5,376 KB
testcase_14 AC 31 ms
6,272 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 23 ms
5,376 KB
testcase_17 AC 27 ms
6,144 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 3 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 62 ms
10,112 KB
testcase_28 AC 40 ms
19,840 KB
testcase_29 WA -
testcase_30 AC 30 ms
11,264 KB
testcase_31 AC 31 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_macros)]
#![allow(dead_code)]
#![allow(unused_imports)]

// # ファイル構成
// - use宣言
// - libモジュール
// - main関数
// - basicモジュール
//
// 常に使うテンプレートライブラリはbasicモジュール内にあります。
// 問題に応じて使うライブラリlibモジュール内にコピペしています。
// ライブラリのコードはこちら → https://github.com/RheoTommy/at_coder
// Twitterはこちら → https://twitter.com/RheoTommy

use std::collections::*;
use std::io::{stdout, BufWriter, Write};

use crate::basic::*;
use crate::lib::*;

pub mod lib {}

fn main() {
    let out = stdout();
    let mut writer = BufWriter::new(out.lock());
    let mut sc = Scanner::new();

    let n = sc.next_usize();
    let mut vertexes = vec![vec![]; n];
    for _ in 0..n - 1 {
        let a = sc.next_usize() - 1;
        let b = sc.next_usize() - 1;
        vertexes[a].push(b);
        vertexes[b].push(a);
    }

    let ans = if dfs(0, &vertexes, 0, U_INF as usize).is_some() {
        "Yes"
    } else {
        "No"
    };
    writeln!(writer, "{}", ans).unwrap();
}

fn dfs(
    now: usize,
    vertexes: &Vec<Vec<usize>>,
    depth: usize,
    before: usize,
) -> Option<(usize, bool)> {
    if vertexes[now].len() == 1 && vertexes[now][0] == before {
        return Some((depth, true));
    }

    let mut v = vec![];
    for &next in &vertexes[now] {
        if next == before {
            continue;
        }
        v.push(dfs(next, vertexes, depth + 1, now));
    }
    if v.contains(&None) {
        return None;
    }

    let v = v.into_iter().map(|vi| vi.unwrap()).collect::<Vec<_>>();
    let vi = v[0].0;
    if v.iter().all(|(_, b)| *b) && v.iter().all(|(vii, _)| *vii == vi) || v.len() == 1 {
        Some((vi, false))
    } else {
        None
    }
}

pub mod basic {
    pub const U_INF: u64 = (1 << 60) + (1 << 30);
    pub const I_INF: i64 = (1 << 60) + (1 << 30);

    pub struct Scanner {
        buf: std::collections::VecDeque<String>,
        reader: std::io::BufReader<std::io::Stdin>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self {
                buf: std::collections::VecDeque::new(),
                reader: std::io::BufReader::new(std::io::stdin()),
            }
        }

        fn scan_line(&mut self) {
            use std::io::BufRead;
            let mut flag = 0;
            while self.buf.is_empty() {
                let mut s = String::new();
                self.reader.read_line(&mut s).unwrap();
                let mut iter = s.split_whitespace().peekable();
                if iter.peek().is_none() {
                    if flag >= 5 {
                        panic!("There is no input!");
                    }
                    flag += 1;
                    continue;
                }

                for si in iter {
                    self.buf.push_back(si.to_string());
                }
            }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            self.scan_line();
            self.buf
                .pop_front()
                .unwrap()
                .parse()
                .unwrap_or_else(|_| panic!("Couldn't parse!"))
        }

        pub fn next_usize(&mut self) -> usize {
            self.next()
        }

        pub fn next_int(&mut self) -> i64 {
            self.next()
        }

        pub fn next_uint(&mut self) -> u64 {
            self.next()
        }

        pub fn next_chars(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }

        pub fn next_string(&mut self) -> String {
            self.next()
        }

        pub fn next_char(&mut self) -> char {
            self.next()
        }

        pub fn next_float(&mut self) -> f64 {
            self.next()
        }
    }
}
0