Codeforces Round #506 (Div. 3) E
dfs+贪心
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 200005; int n,u,v; int ans; vector<int>M[maxn]; int dfs(int cur,int pre) { int ret = 2; for(int i=0;i<M[cur].size();i++) { int nex = M[cur][i]; if(pre == nex)continue; ret = min(ret,dfs(nex,cur)); } if(ret == 0 && cur != 1 && pre != 1)ans++; return (ret+1)%3; } int main() { scanf("%d",&n); for(int i=1;i<n;i++){ scanf("%d%d",&u,&v); M[u].push_back(v); M[v].push_back(u); } dfs(1,0); cout<<ans<<endl; }