UOJ成功爆0 QAQ
把题目想得太简单。。。做遍DFS求最浅的叶子深度,再做遍DFS从下往上涂
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(i=l;i<=r;i++) 3 #define dec(i,l,r) for(i=l;i>=r;i--) 4 #define mem(a) memset(a,0,sizeof(a)) 5 #define inf 1e9 6 #define ll long long 7 #define succ(x) (1<<x) 8 #define NM 100000+5 9 using namespace std; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 13 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 14 return x*f; 15 } 16 struct edge{ 17 int t; 18 edge *next; 19 }e[2*NM],*h[NM]; 20 int n,i,ans,d[NM],s,_x,_y,b[NM]; 21 bool v[NM]; 22 void add(int x,int y){ 23 e[++s].t=y;e[s].next=h[x];h[x]=e+s; 24 } 25 void _dfs(int x){ 26 v[x]++;b[x]=inf; 27 for(edge *j=h[x];j;j=j->next) 28 if(!v[j->t]){ 29 d[j->t]=d[x]+1; 30 _dfs(j->t); 31 b[x]=min(b[x],b[j->t]); 32 } 33 if(b[x]==inf)b[x]=0; 34 b[x]++; 35 } 36 void dfs(int x,int k){ 37 v[x]++;d[x]=0; 38 for(edge *j=h[x];j;j=j->next) 39 if(!v[j->t]){ 40 dfs(j->t,min(b[x]-1,k)); 41 d[x]+=d[j->t]; 42 } 43 if(b[x]==k)d[x]++; 44 } 45 int main(){ 46 // freopen("test.in","r",stdin); 47 n=read(); 48 inc(i,1,n-1){ 49 _x=read();_y=read(); 50 add(_x,_y);add(_y,_x); 51 } 52 d[1]++; 53 _dfs(1); 54 mem(v); 55 dfs(1,b[1]); 56 printf("%d\n",d[1]); 57 return 0; 58 }