/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 284.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 3ms 284.0 KiB
#4 Accepted 2ms 284.0 KiB
#5 Accepted 49ms 18.77 MiB
#6 Accepted 49ms 18.77 MiB
#7 Accepted 52ms 18.77 MiB
#8 Accepted 59ms 19.02 MiB
#9 Accepted 59ms 18.77 MiB
#10 Accepted 60ms 18.734 MiB
#11 Accepted 59ms 18.77 MiB
#12 Accepted 59ms 18.77 MiB
#13 Accepted 98ms 19.02 MiB
#14 Accepted 91ms 19.02 MiB
#15 Accepted 93ms 19.02 MiB
#16 Accepted 119ms 19.008 MiB
#17 Accepted 98ms 19.121 MiB
#18 Accepted 95ms 19.023 MiB
#19 Accepted 47ms 18.02 MiB
#20 Accepted 54ms 24.82 MiB
#21 Accepted 60ms 30.93 MiB

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-10-22 08:58:21
Judged By
Score
100
Total Time
119ms
Peak Memory
30.93 MiB