Problem:
Score : 900 points

Problem Statement

Takahashi has decided to make a Christmas Tree for the Christmas party in AtCoder, Inc.

A Christmas Tree is a tree with N vertices numbered 1 through N and N-1 edges, whose i-th edge (1\leq i\leq N-1) connects Vertex a_i and b_i.

He would like to make one as follows:

Specify two non-negative integers A and B.

Prepare A Christmas Paths whose lengths are at most B. Here, a Christmas Path of length X is a graph with X+1 vertices and X edges such that, if we properly number the vertices 1 through X+1, the i-th edge (1\leq i\leq X) will connect Vertex i and i+1.

Repeat the following operation until he has one connected tree:

Select two vertices x and y that belong to different connected components. Combine x and y into one vertex. More precisely, for each edge (p,y) incident to the vertex y, add the edge (p,x). Then, delete the vertex y and all the edges incident to y.

Properly number the vertices in the tree.

Takahashi would like to find the lexicographically smallest pair (A,B) such that he can make a Christmas Tree, that is, find the smallest A, and find the smallest B under the condition that A is minimized.

Solve this problem for him.

Constraints

2 \leq N \leq 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For the lexicographically smallest (A,B), print A and B with a space in between.

Sample Input 1

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

Sample Output 1

3 2

We can make a Christmas Tree as shown in the figure below:

Sample Input 2

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

Sample Output 2

2 5

Sample Input 3

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

Sample Output 3

3 4

Solution in Rust (1.15.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/arc088/tasks/arc088_d
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

type Graph = Vec<Vec<usize>>;

const INF: usize = 100000000;

fn combine(c: &Vec<usize>, limit: usize) -> usize {
    let n = c.len();
    let mut count = 0;
    let mut fr = 0;
    let mut to = n-1;
    while fr <= to {
        if c[fr] == INF {
            fr += 1;
            continue;
        }
        if c[to] == INF {
            to -= 1;
            continue;
        }
        if fr == to {
            count += 1;
            break;
        } else {
            count += 1;
            if c[fr] + c[to] > limit {
                to -= 1;
            } else {
                fr += 1;
                to -= 1;
            }
        }
    }
    count
}

fn combine_except(c: &mut Vec<usize>, i: usize, limit: usize) -> usize {
    let tmp = c[i];
    c[i] = INF;
    let r = combine(&c, limit);
    c[i] = tmp;
    r
}

fn dfs(now: usize, par: Option<usize>, limit: usize, g: &Graph) -> (usize, usize) {
    let mut children = vec![];

    let mut total_count = 0;
    for &to in &g[now] {
        if Some(to) == par {
            continue;
        }
        let (len, count) = dfs(to, Some(now), limit, g);
        assert!(len <= limit);
        if len == limit {
            total_count += count + 1;
        } else {
            total_count += count;
            children.push(len);
        }
    }
    if children.len() == 0 {
        return (1, total_count);
    }

    children.sort();

    let cn = children.len();
    let best0 = combine(&children, limit);
    let mut wbest = combine_except(&mut children, cn-1, limit);
    let mut ok = (cn-1) as i32;
    let mut ng = -1;

    while ok - ng > 1 {
        let med = (ok + ng) / 2;
        assert!(med >= 0);
        if wbest == combine_except(&mut children, med as usize, limit) {
            ok = med;
        } else {
            ng = med;
        }
    }
    if best0 == wbest {
        (1, total_count + best0)
    } else {
        (children[ok as usize] + 1, total_count + wbest)
    }
}

fn isok(a: usize, b: usize, g: &Graph) -> bool {
    let (tail, count) = dfs(0, None, b, g);
    count + ifv!(tail >= 2, 1, 0) <= a
}

fn solve(a: usize, g: Graph) -> usize {
    let mut ok = g.len();
    let mut ng = 0;
    while ok - ng > 1 {
        let med = (ok + ng) / 2;
        if isok(a, med, &g) {
            ok = med;
        } else {
            ng = med;
        }
    }
    ok
}

fn main() {
    input! {
        n: usize,
        edges: [(usize1, usize1); n-1]
    };

    let mut g = vec![vec![]; n];
    for (u, v) in edges {
        g[u].push(v);
        g[v].push(u);
    }
    let mut odddeg = 0;
    for i in 0..n {
        if g[i].len() % 2 == 1 {
            odddeg += 1;
        }
    }
    assert!(odddeg % 2 == 0);
    let a = odddeg / 2;
    let b = solve(a, g);
    println!("{} {}", a, b);
}