标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 321 Accepted Submission(s): 162
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using namespace std; bool ok; vector<int> a[1010]; int num[1010]; void dfs(int u, int fa, int d) { num[d]++; int len = a[u].size(); for(int i=0; i<len; i++) { if(a[u][i]==fa) continue; dfs(a[u][i], u, d+1); } } int main() { int n; while(~scanf("%d", &n)) { for(int i=0; i<1010; i++) a[i].clear(); memset(num, 0, sizeof(num)); for(int i=1; i<n; i++) { int x, y; scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } ok = false; dfs(1, -1, 1); for(int i=1; i<1010; i++) { if(num[i-1]>1&&num[i]) ok = true; } if(ok) printf("NO\n"); else printf("YES\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/acm1314/p/4771156.html