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)

use std::io::*;
use std::str::FromStr;
use std::collections::VecDeque;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
struct FenwickTree {
    seq: Vec<i64>,
}
#[allow(dead_code)]
impl FenwickTree {
    fn new(n: usize) -> FenwickTree {
        FenwickTree { seq: vec![0; n + 2] }
    }
    fn add(&mut self, index: usize, value: i64) {
        let mut index = (index + 1) as i32;
        while (index as usize) < self.seq.len() {
            self.seq[index as usize] += value;
            index += index & -index;
        }
    }
    // inclusive
    fn sum(&self, index: usize) -> i64 {
        let mut ret = 0;
        let mut index = (index + 1) as i32;
        while index > 0 {
            ret += self.seq[index as usize];
            index -= index & -index;
        }
        ret
    }
}

type Weight = i32;
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct Edge {
    index: usize,
    src: usize,
    dest: usize,
    weight: Weight,
}
#[allow(dead_code)]
impl Edge {
    fn new(index: usize, src: usize, dest: usize, weight: Weight) -> Edge {
        Edge {
            index: index,
            src: src,
            dest: dest,
            weight: weight,
        }
    }
}
impl std::cmp::Ord for Edge {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (self.weight, self.src, self.dest).cmp(&(other.weight, other.src, other.dest))
    }
}
impl std::cmp::PartialOrd for Edge {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
type Edges = Vec<Edge>;
type Graph = Vec<Edges>;
#[allow(dead_code)]
fn graph_new(n: usize) -> Graph {
    vec![vec![]; n]
}
#[allow(dead_code)]
fn add_bi_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    graph[dest].push(Edge::new(*index, dest, src, weight));
    *index += 1;
}
#[allow(dead_code)]
fn add_uni_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    *index += 1;
}



struct Solver {
    n: usize,
    g: Graph,
    edge_cnt: Vec<usize>,
}

impl Solver {
    fn new() -> Solver {
        Solver {
            n: 0,
            g: vec![],
            edge_cnt: vec![],
        }
    }
    fn init(&mut self) {
        let n: usize = read();
        self.n = n;
        self.g = graph_new(n);
        self.edge_cnt = vec![usize::max_value(); n - 1];
        let mut e = 0;
        for _ in 0..n-1 {
            let mut a: usize = read();
            let mut b: usize = read();
            a -= 1; b -= 1;
            add_bi_edge(&mut self.g, &mut e, a, b, 0);
        }
    }
    fn merge(&self, len: usize, childs: &mut Vec<usize>) -> (usize, usize) {
        let mut rest = 0;
        let mut minus = 0;
        childs.sort();
        childs.reverse();
        let mut front = 0;
        let mut back = childs.len();
        while front < back && childs[back - 1] == 0 { back -= 1; }
        while back - front > 1 {
            if back - front > 2 && childs[front + 1] + childs[back - 1] >= len {
                childs.swap(front, front + 1);
                front += 1;
                back -= 1;
                minus += 1;
            } else if childs[front] + childs[back - 1] >= len {
                front += 1;
                back -= 1;
                minus += 1;
            } else {
                rest = childs[back - 1];
                back -= 1;
            }
        }
        if front != back {
            rest = childs[front];
        }
        (rest, minus)
    }
    fn bfs(&self, len: usize) -> usize {
        let n = self.n;
        let mut childs = vec![vec![]; n];
        let mut used = vec![false; n - 1];
        let mut ret = 0;
        let mut que = VecDeque::new();
        for i in 0..n {
            if self.g[i].len() == 1 { que.push_back(i); }
        }
        while let Some(from) = que.pop_front() {
            let (mut rest, minus) = self.merge(len, &mut childs[from]);
            ret -= minus;
            for edge in self.g[from].iter() {
                if used[edge.index] { continue; }
                used[edge.index] = true;
                if rest == 0 {
                    rest = len;
                    ret += 1;
                }
                rest -= 1;
                childs[edge.dest].push(rest);
                if childs[edge.dest].len() == self.g[edge.dest].len() - 1 {
                    que.push_back(edge.dest);
                }
            }
        }
        return ret;
    }
    fn solve(&mut self) {
        let n = self.n;
        let e = n - 1;
        let cnt = self.bfs(e);
        let mut left = 0;
        let mut right = e;
        while right - left > 1 {
            let mid = (left + right) / 2;
            if self.bfs(mid) == cnt {
                right = mid;
            } else {
                left = mid;
            }
        }
        println!("{} {}", cnt, right);
    }
}

fn main() {
    let mut solver = Solver::new();
    solver.init();
    solver.solve();
}