1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ std::cout<<x; } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } using Pir=std::pair<int,int>; #define fir first #define sec second #define Mkr std::make_pair const int MAXN=5e5+10; int N; struct G { int next,to; }Edge[MAXN<<1]; int Head[MAXN],Deg[MAXN],Total; inline void addEdge(int u,int v) { Edge[++Total]=(G){Head[u],v};Head[u]=Total;++Deg[u]; Edge[++Total]=(G){Head[v],u};Head[v]=Total;++Deg[v]; } int Fa[MAXN],Siz[MAXN],Son[MAXN],Dep[MAXN]; int Leafcnt[MAXN],Depmax[MAXN],ans; bool Tar[MAXN],Leaf[MAXN]; void dfsTree(int x,int last) { Fa[x]=last,Siz[x]=1,Son[x]=0,Dep[x]=Dep[last]+1; Leafcnt[x]=Depmax[x]=0,Leaf[x]=0; bool leaf=0; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last) continue; if(Tar[v]){ leaf=1;continue; } dfsTree(v,x); Siz[x]+=Siz[v],Leafcnt[x]+=Leafcnt[v]; checkMax(Depmax[x],Depmax[v]); if(!Son[x]||Siz[v]>Siz[Son[x]]) Son[x]=v; } if(!Son[x]&&!leaf) Leafcnt[x]=1,Depmax[x]=Dep[x],Leaf[x]=1; } int maxdis,fir,sec; void getFar(int x,int last,int &tar,int dis) { if(Leaf[x]&&checkMax(maxdis,dis)) tar=x; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last||Tar[v]) continue; getFar(v,x,tar,dis+1); } } inline void getTarget(int rt,int u,int v,std::vector<int>&vec) { vec.clear(); while(u!=rt) Tar[u]=1,vec.push_back(u),u=Fa[u]; while(v!=rt) Tar[v]=1,vec.push_back(v),v=Fa[v]; Tar[rt]=1; } void solve(int x) { dfsTree(x,0); maxdis=-1; getFar(x,0,fir,0); maxdis=-1; getFar(fir,0,sec,0); if(fir==sec) return ; ++ans; std::vector<int>leaf; getTarget(x,fir,sec,leaf); for(int u:leaf) { dfsTree(u,0); if(!Son[u]) continue; if(Depmax[u]<=2) { ans+=Leafcnt[u]>>1; continue; } if(Leafcnt[u]>1) solve(u); } } int dpTree(int x,int last) { int cnt1=0,cnt2=0; bool leaf=1; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last) continue; int v0=dpTree(v,x);leaf=0; if(v0==1) ++cnt1; else if(v0==2) ++cnt2; } if(leaf) return 1; if(cnt1>2) { ans+=cnt1>>1,cnt1&=1; if(!cnt1) cnt1+=2,--ans; } if(cnt2>1) ans+=cnt2>>1,cnt2&=1; if(!cnt1&&!cnt2) return 0; if(!cnt1) return 2; if(!cnt2) return cnt1; if(cnt1==1&&cnt2==1) return 2; ++ans; return 1; } int main() { freopen("tree.in","r",stdin); freopen("tree.out","w",stdout); read(N); for(int i=2,u,v;i<=N;++i) read(u,v),addEdge(u,v); for(int rt=1;rt<=N;++rt) if(Deg[rt]>1){ ans+=dpTree(rt,0)==2;break; } write(ans); return 0; }
|