結果

問題 No.1424 Ultrapalindrome
ユーザー ikdikd
提出日時 2021-03-12 22:17:36
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,911 bytes
コンパイル時間 3,185 ms
コンパイル使用メモリ 160,280 KB
実行使用メモリ 27,904 KB
最終ジャッジ日時 2024-04-22 13:18:48
合計ジャッジ時間 3,638 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 52 ms
12,416 KB
testcase_10 AC 50 ms
12,416 KB
testcase_11 AC 31 ms
9,344 KB
testcase_12 AC 52 ms
11,776 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 34 ms
9,984 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 26 ms
8,448 KB
testcase_17 AC 34 ms
9,728 KB
testcase_18 AC 20 ms
7,168 KB
testcase_19 AC 35 ms
9,728 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 4 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 44 ms
27,904 KB
testcase_29 WA -
testcase_30 AC 32 ms
16,000 KB
testcase_31 AC 33 ms
16,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

macro_rules! chmin {
    ($a: expr, $b: expr) => {
        $a = std::cmp::min($a, $b);
    };
}

macro_rules! chmax {
    ($a: expr, $b: expr) => {
        $a = std::cmp::max($a, $b);
    };
}

const INF: usize = std::usize::MAX;

fn dfs(
    i: usize,
    p: usize,
    g: &Vec<Vec<usize>>,
    par: &mut Vec<usize>,
    max: &mut Vec<usize>,
    min: &mut Vec<usize>,
    order: &mut Vec<usize>,
) {
    par[i] = p;
    max[i] = 0;
    min[i] = INF;
    for &j in &g[i] {
        if j == p {
            continue;
        }
        dfs(j, i, g, par, max, min, order);
        chmax!(max[i], max[j] + 1);
        chmin!(min[i], min[j] + 1);
    }
    if min[i] == INF {
        min[i] = 0;
    }
    order.push(i);
}

fn main() {
    let stdin = std::io::stdin();
    let mut rd = ProconReader::new(stdin.lock());

    let n: usize = rd.get();
    let mut g = vec![vec![]; n];
    for _ in 0..(n - 1) {
        let a: usize = rd.get();
        let b: usize = rd.get();
        g[a - 1].push(b - 1);
        g[b - 1].push(a - 1);
    }
    let mut par = vec![0; n];
    let mut sub_max = vec![0; n];
    let mut sub_min = vec![INF; n];
    let mut order = Vec::new();
    dfs(
        0,
        std::usize::MAX,
        &g,
        &mut par,
        &mut sub_max,
        &mut sub_min,
        &mut order,
    );
    order.reverse();
    let mut par_max = vec![0; n];
    let mut par_min = vec![INF; n];
    par_max[0] = 0;
    par_min[0] = 0;
    let mut max = vec![0; n];
    let mut min = vec![INF; n];
    max[0] = sub_max[0];
    min[0] = sub_min[0];
    assert_eq!(order[0], 0);
    for &i in &order[1..] {
        chmax!(par_max[i], par_max[par[i]] + 1);
        chmin!(par_min[i], par_min[par[i]] + 1);
        chmax!(max[i], par_max[i] + sub_max[i]);
        chmin!(min[i], par_min[i] + sub_min[i]);
    }
    let ok = max.iter().zip(min.iter()).all(|(min, max)| min == max);
    if ok {
        println!("Yes");
    } else {
        println!("No");
    }
}

pub struct ProconReader<R> {
    r: R,
    l: String,
    i: usize,
}

impl<R: std::io::BufRead> ProconReader<R> {
    pub fn new(reader: R) -> Self {
        Self {
            r: reader,
            l: String::new(),
            i: 0,
        }
    }
    pub fn get<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        self.skip_blanks();
        assert!(self.i < self.l.len()); // remain some character
        assert_ne!(&self.l[self.i..=self.i], " ");
        let rest = &self.l[self.i..];
        let len = rest.find(' ').unwrap_or_else(|| rest.len());
        // parse self.l[self.i..(self.i + len)]
        let val = rest[..len]
            .parse()
            .unwrap_or_else(|e| panic!("{:?}, attempt to read `{}`", e, rest));
        self.i += len;
        val
    }
    fn skip_blanks(&mut self) {
        loop {
            match self.l[self.i..].find(|ch| ch != ' ') {
                Some(j) => {
                    self.i += j;
                    break;
                }
                None => {
                    let mut buf = String::new();
                    let num_bytes = self
                        .r
                        .read_line(&mut buf)
                        .unwrap_or_else(|_| panic!("invalid UTF-8"));
                    assert!(num_bytes > 0, "reached EOF :(");
                    self.l = buf
                        .trim_end_matches('\n')
                        .trim_end_matches('\r')
                        .to_string();
                    self.i = 0;
                }
            }
        }
    }
    pub fn get_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.get()).collect()
    }
    pub fn get_chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }
}
0