#include using namespace std; #include using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint; using P = pair; using lb = long double; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif // Rerooting // https://youtu.be/zG1L4vYuGrg?t=7092 // TODO: vertex info, edge info struct Rerooting { struct DP { int b, w; DP(int b = 0 , int w = 0) :b(b),w(w) {} DP operator+(const DP& a) const { return {b + max(a.b, a.w), w+a.w}; } DP addRoot() const { return {w+1, b}; } }; int n; vector> to; vector> dp; vector ans; Rerooting(int n=0):n(n),to(n),dp(n),ans(n) {} void addEdge(int a, int b) { to[a].push_back(b); to[b].push_back(a); } void init() { dfs(0); bfs(0); } DP dfs(int v, int p=-1) { DP dpSum; dp[v] = vector(to[v].size()); rep(i,to[v].size()) { int u = to[v][i]; if (u == p) continue; dp[v][i] = dfs(u,v); dpSum = dpSum + dp[v][i]; } return dpSum.addRoot(); } void bfs(int v, const DP& dpP=DP(), int p=-1) { int deg = to[v].size(); rep(i,deg) if (to[v][i] == p) dp[v][i] = dpP; vector dpSumL(deg+1); rep(i,deg) dpSumL[i+1] = dpSumL[i] + dp[v][i]; vector dpSumR(deg+1); for (int i = deg-1; i >= 0; --i) dpSumR[i] = dpSumR[i+1] + dp[v][i]; ans[v] = dpSumL[deg].addRoot(); rep(i,deg) { int u = to[v][i]; if (u == p) continue; DP d = dpSumL[i] + dpSumR[i+1]; bfs(u, d.addRoot(), v); } } }; int main() { int n; cin >> n; Rerooting rt(n); rep(i,n-1){ int u, v; cin >> u >> v; --u;--v; rt.addEdge(u,v); } rt.init(); int f = 1e9; rep(i,n){ f = min(f, rt.ans[i].b); } cout<