QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#745053#9751. 覆盖一棵树gggheWA 21ms2120kbRust4.1kb2024-11-14 02:27:292024-11-14 02:27:30

Judging History

你现在查看的是最新测评结果

  • [2024-11-14 02:27:30]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:2120kb
  • [2024-11-14 02:27:29]
  • 提交

answer

use std::{cmp, collections::*};
use std::cmp::{min, max};

fn solve() {
    input! {
        n: usize,
        fa: [usize; n - 1]
    }
    struct E {
        g: Vec<Vec<usize>>,
        dmx: Vec<i32>,
        dmi: Vec<i32>,
        ans: i32
    }
    let mut env = E {
        g: vec![vec![]; n],
        dmx: vec![0; n],
        dmi: vec![1_000_000; n],
        ans: 0,
    };

    for i in 1..n {
        let u = fa[i - 1] - 1;
        let v = i;
        env.g[u].push(v);
        env.g[v].push(u);
    }


    fn dfs(x: usize, fa: usize, dep: i32, env: &mut E) -> i32 {
        let mut res = 0;
        let mut ds = vec![];
        for &y in &env.g[x].clone() {
            if y == fa {
                continue;
            }
            let d = dfs(y, x, dep + 1, env);
            ds.push(d + 1);
        }

        ds.sort_unstable();
        
        if !ds.is_empty() {
            env.ans = max(env.ans, ds.first().unwrap() + dep);
            env.ans = max(env.ans, *ds.last().unwrap());
        }
        res
    }
    dfs(0, n + 2, 0, &mut env);
    println!("{}", env.ans);
}

fn main() {
    input! {
        t: i32
    }
    for _ in 0..t {
        solve();
    }
}

fn at(i: i64) -> usize {
    i as usize
}

#[macro_export]
macro_rules! input {
    () => {};
    (mut $var:ident: $t:tt, $($rest:tt)*) => {
        let mut $var = __input_inner!($t);
        input!($($rest)*)
    };
    ($var:ident: $t:tt, $($rest:tt)*) => {
        let $var = __input_inner!($t);
        input!($($rest)*)
    };
    (mut $var:ident: $t:tt) => {
        let mut $var = __input_inner!($t);
    };
    ($var:ident: $t:tt) => {
        let $var = __input_inner!($t);
    };
}

#[macro_export]
macro_rules! __input_inner {
    (($($t:tt),*)) => {
        ($(__input_inner!($t)),*)
    };
    ([$t:tt; $n:expr]) => {
        (0..$n).map(|_| __input_inner!($t)).collect::<Vec<_>>()
    };
    ([$t:tt]) => {{
        let n = __input_inner!(usize);
        (0..n).map(|_| __input_inner!($t)).collect::<Vec<_>>()
    }};
    (chars) => {
        __input_inner!(String).chars().collect::<Vec<_>>()
    };
    (bytes) => {
        __input_inner!(String).into_bytes()
    };
    (usize1) => {
        __input_inner!(usize) - 1
    };
    ($t:ty) => {
        $crate::read::<$t>()
    };
}

#[macro_export]
macro_rules! println {
    ($($arg:tt)*) => {
        $crate::write(|w| {
            use std::io::Write;
            std::writeln!(w, $($arg)*).unwrap()
        })
    };
}

#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => {
        $crate::write(|w| {
            use std::io::Write;
            std::write!(w, $($arg)*).unwrap()
        })
    };
}

#[macro_export]
macro_rules! flush {
    () => {
        $crate::write(|w| {
            use std::io::Write;
            w.flush().unwrap()
        })
    };
}

pub fn read<T>() -> T
where
    T: std::str::FromStr,
    T::Err: std::fmt::Debug,
{
    use std::cell::RefCell;
    use std::io::*;

    thread_local! {
        static STDIN: RefCell<StdinLock<'static>> = RefCell::new(stdin().lock());
    }

    STDIN.with_borrow_mut(|r| {
        let mut s = vec![];
        loop {
            let buf = r.fill_buf().unwrap();
            if buf.is_empty() {
                break;
            }
            if let Some(i) = buf.iter().position(u8::is_ascii_whitespace) {
                s.extend_from_slice(&buf[..i]);
                r.consume(i + 1);
                if !s.is_empty() {
                    break;
                }
            } else {
                s.extend_from_slice(buf);
                let n = buf.len();
                r.consume(n);
            }
        }
        std::str::from_utf8(&s).unwrap().parse().unwrap()
    })
}

pub fn write<F>(f: F)
where
    F: FnOnce(&mut std::io::BufWriter<std::io::StdoutLock>),
{
    use std::cell::RefCell;
    use std::io::*;

    thread_local! {
        static STDOUT: RefCell<BufWriter<StdoutLock<'static>>> =
            RefCell::new(BufWriter::new(stdout().lock()));
    }

    STDOUT.with_borrow_mut(f)
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 2120kb

input:

2
8
1 2 3 2 5 1 7
8
1 2 3 4 5 6 7

output:

3
7

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 21ms
memory: 2108kb

input:

33428
10
1 2 3 3 4 6 7 7 9
10
1 2 3 4 5 6 7 8 8
8
1 2 3 4 5 6 7
8
1 2 3 4 4 6 7
4
1 2 3
3
1 2
3
1 1
9
1 2 3 4 5 6 7 8
2
1
3
1 2
10
1 2 3 4 5 6 7 8 9
3
1 2
2
1
10
1 2 3 4 5 6 7 8 9
2
1
5
1 2 2 4
8
1 2 3 4 5 6 7
5
1 2 3 3
2
1
5
1 2 3 4
3
1 2
9
1 2 3 4 5 6 6 8
9
1 2 3 4 5 6 7 8
9
1 2 3 4 5 5 7 8
8
1 2 ...

output:

7
8
7
6
3
2
1
8
1
2
9
2
1
9
1
3
7
3
1
4
2
7
8
7
6
4
1
2
9
7
4
3
4
6
7
3
5
7
6
5
9
3
4
4
8
2
8
6
2
4
8
4
2
1
5
2
5
2
4
7
2
2
5
2
2
6
3
9
2
5
7
4
2
2
1
3
1
9
1
5
1
7
3
6
2
9
7
2
3
1
3
3
1
2
8
6
8
4
7
7
7
6
5
7
8
4
5
2
5
5
1
7
7
7
2
2
2
3
8
3
5
6
4
3
3
3
2
2
4
8
6
7
8
6
4
2
4
7
8
1
6
2
7
7
9
1
2
8
5
2
...

result:

wrong answer 1st lines differ - expected: '4', found: '7'