传送门:http://codeforces.com/problemset/problem/573/c
思路:见官方题解。。。
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> const int maxn=100010; using namespace std; int n,leg[maxn];bool del[maxn]; vector<int> w[maxn]; void dfs(int a,int fa){ if (w[a].size()<=2){ del[a]=1; for (int i=0;i<w[a].size();i++) if (w[a][i]!=fa) dfs(w[a][i],a); } } int main(){ scanf("%d",&n); for (int i=1,a,b;i<n;i++) scanf("%d%d",&a,&b),w[a].push_back(b),w[b].push_back(a); for (int i=1;i<=n;i++) if (w[i].size()==1) dfs(i,0); for (int i=1;i<=n;i++) for (int j=0;j<w[i].size();j++) if (del[w[i][j]]) leg[i]++; for (int i=1;i<=n;i++) if (!del[i]){ int cnt=0; for (int j=0;j<w[i].size();j++){ int b=w[i][j]; if (!del[b]&&w[b].size()>min(leg[b],2)+1) cnt++; } if (cnt>2){puts("No");return 0;} } puts("Yes"); return 0; }