Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef TRI<int, int, int> tri;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 100005;

vector<int> G[MAX_N];
int dp[MAX_N];

int dfs(int u, int p)
{
    int cnt = 0, num = 0;
    for(int v : G[u]){
        if(v != p){
            dfs(v, u);
            num += dp[v];
            cnt++;
        }
    }
    dp[u] = cnt/2 + num;
    return (cnt+1)/2 + num;
}

bool check(vector<int>& vec, int num, int cri)
{
    int l = 0, r = len(vec)-1;
    for(;;++l, --r){
        if(l == num) ++l;
        if(r == num) --r;
        if(l >= r) break;
        if(vec[l]+vec[r]+2 > cri) return false;
    }
    return true;
}

bool dfs2(int u, int p, int cri)
{
    vector<int> vec;
    for(int v : G[u]){
        if(v != p){
            if(!dfs2(v, u, cri)) return false;
            vec.pb(dp[v]);
        }
    }
    sort(all(vec));
    if(len(vec) % 2){
        if(check(vec, 0, cri)){
            if(vec[0] == cri){
                return false;
            }else{
                dp[u] = vec[0] + 1;
                return true;
            }
        }else if(!check(vec, len(vec)-1, cri)){
            return false;
        }else{
            int l = 0, r = len(vec)-1;
            while(r-l>1){
                int mid = (l+r)/2;
                if(check(vec, mid, cri)){
                    r = mid;
                }else{
                    l = mid;
                }
            }
            if(vec[r] == cri){
                return false;
            }else{
                dp[u] = vec[r] + 1;
                return true;
            }
        }
    }else{
        rep(i,len(vec)/2){
            if(vec[i] + vec[len(vec)-i-1] + 2 > cri) return false;
        }
        return true;
    }
}

bool possible(int cri, int a)
{
    rep(i,n) dp[i] = 0;
    return dfs2(0, -1, cri);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    rep(i,n-1){
        int a, b;
        cin >> a >> b;
        G[a-1].pb(b-1), G[b-1].pb(a-1);
    }
    int a = dfs(0, -1);
    int l = 0, r = n;
    while(r-l>1){
        int mid = (l+r)/2;
        if(possible(mid, a)){
            r = mid;
        }else{
            l = mid;
        }
    }
    cout << a << " " << r << "\n";
    return 0;
}


Compiler output:
a.cc: In function 'bool possible(int, int)':
a.cc:124:11: error: 'n' was not declared in this scope
  124 |     rep(i,n) dp[i] = 0;
      |           ^
a.cc:6:38: note: in definition of macro 'rep'
    6 | #define rep(i,n) for(int i=0;i<(int)(n);++i)
      |                                      ^