#include #include #define rep(i,b) for(int i=0;i=0;i--) #define rep1(i,b) for(int i=1;i=x;i--) #define fore(i,a) for(auto& i:a) #define rng(x) (x).begin(), (x).end() #define rrng(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define pb push_back #define fi first #define se second #define pcnt __builtin_popcountll using namespace std; using namespace atcoder; using ll = long long; using ld = long double; template using mpq = priority_queue, greater>; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (b ll sumv(const vector&a){ll res(0);for(auto&&x:a)res+=x;return res;} bool yn(bool a) { if(a) {cout << "Yes" << endl; return true;} else {cout << "No" << endl; return false;}} #define retval(x) {cout << #x << endl; return;} #define cout2(x,y) cout << x << " " << y << endl; #define coutp(p) cout << p.fi << " " << p.se << endl; #define out cout << ans << endl; #define outd cout << fixed << setprecision(20) << ans << endl; #define outm cout << ans.val() << endl; #define outv fore(yans , ans) cout << yans << "\n"; #define outdv fore(yans , ans) cout << yans.val() << "\n"; #define assertmle(x) if (!(x)) {vi v(3e8);} #define asserttle(x) if (!(x)) {while(1){}} #define coutv(v) {fore(vy , v) {cout << vy << " ";} cout << endl;} #define coutv2(v) fore(vy , v) cout << vy << "\n"; #define coutvm(v) {fore(vy , v) {cout << vy.val() << " ";} cout << endl;} #define coutvm2(v) fore(vy , v) cout << vy.val() << "\n"; using pll = pair;using pil = pair;using pli = pair;using pii = pair;using pdd = pair; using vi = vector;using vd = vector;using vl = vector;using vs = vector;using vb = vector; using vpii = vector;using vpli = vector;using vpll = vector;using vpil = vector; using vvi = vector>;using vvl = vector>;using vvs = vector>;using vvb = vector>; using vvpii = vector>;using vvpli = vector>;using vvpll = vector;using vvpil = vector; using mint = modint998244353; //using mint = modint1000000007; //using mint = dynamic_modint<0>; using vm = vector; using vvm = vector>; vector dx={1,0,-1,0,1,1,-1,-1},dy={0,1,0,-1,1,-1,1,-1}; ll gcd(ll a, ll b) { return a?gcd(b%a,a):b;} ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} #define yes {cout <<"Yes"< struct ReRooting { using F1 = function; using F2 = function; int n; // 頂点数 vvi e,tmp_edge; // e:edge情報 親は最後に入る vector> dp; // dp[x][i]、iはeに連動 vector> lsum, rsum; // 左(右)からの累積和 vector ans; const F1 op; // マージ演算関数 const F2 id; // 単位元 ReRooting(int n,F1 op,F2 id) : n(n),e(n),tmp_edge(n),dp(n),lsum(n),rsum(n),ans(n,INF),op(op),id(id){} void add_edge(int u, int v) { tmp_edge[u].pb(v); tmp_edge[v].pb(u); } T dfs1(int x, int pre) { T ret = id(); int i = 0; fore(y , tmp_edge[x]){ if (y == pre) continue; dp[x].pb(dfs1(y , x)); e[x].pb(y); ret = op(ret , dp[x][i]); lsum[x].pb(id()); rsum[x].pb(id()); i++; } if (pre != -1){ e[x].pb(pre); dp[x].pb(id()); lsum[x].pb(id()); rsum[x].pb(id()); } return ret; } void dfs2(int x,int pre,T preval) { int m = sz(e[x]); if (pre != -1) dp[x][m-1] = preval; rep(i,m){ lsum[x][i] = dp[x][i]; if (i != 0) lsum[x][i] = op(lsum[x][i], lsum[x][i-1]); } rrep(i,m){ rsum[x][i] = dp[x][i]; if (i != m-1) rsum[x][i] = op(rsum[x][i], rsum[x][i+1]); } if (m >= 1) ans[x] = lsum[x][m-1].se; rep(i,m){ int y = e[x][i]; if (y == pre) continue; T nxtval = id(); if (i != 0) nxtval = op(nxtval , lsum[x][i-1]); if (i != m-1) nxtval = op(nxtval , rsum[x][i+1]); dfs2(y , x , nxtval); } return; } void solve(){ dfs1(0,-1); dfs2(0,-1,id()); } }; // 全方位木dp // ReRooting rr(n,op,id) : 初期化。nは頂点数、opはmerge演算関数、idは単位元 // op,idはラムダ式で定義 // add_edge(a,b) a,b間に辺を引く // solve() dp実行、ansに答えが入る。 // dfs内は適宜変更すること void solve(){ int n; cin>>n; auto op = [&](pii a, pii b)->pii{ pii c = {a.fi+max(b.fi, b.se), a.se+b.fi}; return c; }; auto e = [&]()->pii{ return {0,1}; }; ReRooting rr(n,op,e); rep(i,n-1){ int a,b; cin>>a>>b; a--; b--; rr.add_edge(a,b); } rr.solve(); int ans = INF; rep(i,n) chmin(ans, rr.ans[i]); out; return; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int t = 1; //cin>>t; rep(i,t){ solve(); } return 0; }