标签:
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include<vector> #include<queue> #include<algorithm> using namespace std; typedef long long LL; const int maxn=1009; const int INF=0x3f3f3f3f; const int mod=2009; int maps[maxn][maxn]; int dist[maxn]; int cnt[maxn]; int n; void DFS(int u, int d) { dist[u]=d; for(int i=1; i<=n; i++) { if(maps[u][i]&&dist[i]==-1) DFS(i, d+1); } } int main() { while(~scanf("%d", &n)) { int u, v; memset(maps, 0, sizeof(maps)); for(int i=1; i<n; i++) { scanf("%d %d", &u, &v); maps[u][v]=maps[v][u]=1; } memset(dist, -1, sizeof(dist)); memset(cnt, 0, sizeof(cnt)); DFS(1,0); for(int i=1; i<=n; i++) cnt[dist[i]]++; int index=0, j; for(j=1; j<n; j++) { if(cnt[j]) { if(cnt[j]<=cnt[index]&&cnt[index]>1) break; else index=j; } } if(j==n) puts("YES"); else puts("NO"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/w-y-1/p/5798526.html