本文主要是介绍hdu 5423 Rikka with Tree,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参考:http://blog.csdn.net/aaaaacmer/article/details/48093767
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define M 1005using namespace std;struct node{int to, next;
}edge[M*100];int head[M*2];
int n, tot, ans[1005], q, vis[1005];void add(int a, int b){edge[tot].to = b;edge[tot].next = head[a];head[a] = tot++;
}int t=0;void dfs(int x, int deep){int i, j;vis[x] = 1;for(i = head[x]; i != -1; i = edge[i].next){j = edge[i].to;if(vis[j]) continue;vis[j]=1;dfs(j, deep+1);}ans[q++] = deep;return ;
}int main(){int i, u, v;while(scanf("%d", &n) != EOF){q=0;memset(vis, 0, sizeof(vis));memset(head, -1, sizeof(head));tot = 0;for( i = 1; i < n; i++){scanf("%d%d", &u, &v);add(u, v);add(v, u);}int f=0;dfs(1, 0);int as = 0, mark = 0;sort(ans, ans+q-1);for(i =1; i<q-1;i++){if(ans[i] == ans[i-1]){mark=1; as=ans[i];}if(ans[i] > as&&mark) f=1;}if(f) printf("NO\n");else printf("YES\n");}return 0;
}
这篇关于hdu 5423 Rikka with Tree的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!