http://codeforces.com/problemset/problem/573/C
给一颗树,问是否能画在两行平行的点格图上(无限长),边长随意,不能相交。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<bits/stdc++.h> const int maxn=1e5+15; using namespace std; vector<int> g[maxn]; int n,leg[maxn]; bool mark[maxn]; void init(){ scanf("%d",&n); for (int u,v,i=1;i<n;++i){ scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } } void dfs(int u,int f){ if (g[u].size()<=2){ mark[u]=1; for (int i=0;i<g[u].size();++i) if (g[u][i]!=f) dfs(g[u][i],u); } } void work(){ for (int i=1;i<=n;++i) if (g[i].size()==1) dfs(i,0); for (int u=1;u<=n;++u) if (!mark[u]) for (int i=0;i<g[u].size();++i) if (mark[g[u][i]]) ++leg[u]; for (int u=1;u<=n;++u) if (!mark[u]){ int cnt=0; for (int i=0;i<g[u].size();++i) if (!mark[g[u][i]]) if (g[g[u][i]].size()>min(leg[g[u][i]],2)+1) ++cnt; if (cnt>2){puts("No");return;} } puts("Yes"); } int main(){ init(); work(); return 0; }