/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 1ms 320.0 KiB
#4 Accepted 1ms 320.0 KiB
#5 Accepted 52ms 18.605 MiB
#6 Accepted 53ms 18.707 MiB
#7 Accepted 62ms 18.84 MiB
#8 Accepted 64ms 19.09 MiB
#9 Accepted 69ms 18.93 MiB
#10 Accepted 69ms 18.863 MiB
#11 Accepted 67ms 18.902 MiB
#12 Accepted 67ms 18.785 MiB
#13 Accepted 126ms 19.215 MiB
#14 Accepted 116ms 19.215 MiB
#15 Accepted 129ms 19.223 MiB
#16 Accepted 111ms 19.191 MiB
#17 Accepted 117ms 19.227 MiB
#18 Accepted 117ms 19.172 MiB
#19 Accepted 53ms 17.988 MiB
#20 Accepted 61ms 24.906 MiB
#21 Accepted 69ms 30.906 MiB
#22 Accepted 1ms 320.0 KiB
#23 Accepted 1ms 324.0 KiB
#24 Accepted 1ms 324.0 KiB

Code

use std::cmp::{max, min};
use std::io::{self, BufRead};

const M: usize = 200010;
const MOD: i64 = 998244353;

static mut LEVEL: [i32; M] = [0; M];
static mut DP: [[i32; 2]; M] = [[0; 2]; M];
static mut LOW: [i32; M] = [0; M];
static mut UP: [i32; M] = [0; M];
static mut BONUS: [i32; M] = [0; M];
static mut MX: i32 = 0;
static mut N: usize = 0;
static mut K: i32 = 0;
static mut EDGE: Vec<Vec<usize>> = Vec::new();

unsafe fn dfs1(x: usize, p: usize) {
    for &it in &EDGE[x] {
        if it != p {
            UP[it] = UP[x] + 1;
            if x == 1 {
                LOW[it] = DP[x][0];
                if DP[x][0] == LEVEL[it] + 1 {
                    LOW[it] = DP[x][1];
                }
                LOW[it] -= 1;
                LOW[it] = max(LOW[it], 0);
            } else {
                LOW[it] = LOW[x];
                let mut cur = DP[x][0];
                if DP[x][0] == LEVEL[it] + 1 {
                    cur = DP[x][1];
                }
                cur -= 1;
                cur = max(0, cur);
                BONUS[it] = BONUS[x];
                BONUS[it] += cur;
                MX = max(MX, UP[it] + min(LOW[x] + BONUS[it], K));
            }
            dfs1(it, x);
        }
    }
}

unsafe fn dfs(x: usize, p: usize) {
    for &it in &EDGE[x] {
        if it != p {
            dfs(it, x);
        }
    }
    LEVEL[p] = max(LEVEL[p], LEVEL[x] + 1);
    if DP[p][0] < LEVEL[p] {
        std::mem::swap(&mut DP[p][0], &mut DP[p][1]);
        DP[p][0] = LEVEL[p];
    } else if LEVEL[x] + 1 > DP[p][1] {
        DP[p][1] = LEVEL[x] + 1;
    }
}

fn main() {
    let stdin = io::stdin();
    let mut input = stdin.lock().lines();

    let t = 1; // Single test case for now
    for _ in 0..t {
        // Read n and k
        let line = input.next().unwrap().unwrap();
        let mut split = line.split_whitespace();
        unsafe {
            N = split.next().unwrap().parse().unwrap();
            K = split.next().unwrap().parse().unwrap();

            // Initialize arrays and vector of edges
            EDGE = vec![Vec::new(); N + 1];
            for i in 1..=N {
                LEVEL[i] = 1;
            }

            // Read edges
            for _ in 1..N {
                let line = input.next().unwrap().unwrap();
                let mut split = line.split_whitespace();
                let x: usize = split.next().unwrap().parse().unwrap();
                let y: usize = split.next().unwrap().parse().unwrap();
                EDGE[x].push(y);
                EDGE[y].push(x);
            }

            DP[1][0] = 1;
            dfs(1, 0);
            MX = DP[1][0];
            UP[1] = 1;
            MX += min(max(0, DP[1][1] - 1), K);
            dfs1(1, 0);
            println!("{}", min(MX, N as i32));
        }
    }
}

Information

Submit By
Type
Submission
Problem
P1111 Thakurs tree game
Language
Rust 2021 (Rust 1.75.0)
Submit At
2024-10-22 08:58:21
Judged At
2024-11-11 02:36:02
Judged By
Score
100
Total Time
129ms
Peak Memory
30.906 MiB