判断是否是一个堆,把树构造好遍历一遍就OK了
#include<stdio.h>
#include<iostream>
#include<stack>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
using namespace std;
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define INF 0xfffffff
#define maxn 410 int Tree[maxn][maxn], n, flag, Value[maxn], vis[maxn]; void DFS(int k)
{ vis[k] = ;
for(int i=; i<=n; i++)
{
if(Tree[k][i] && !vis[i])
{
if(Value[k] <= Value[i])
DFS(i);
else
flag = ;
}
}
} int main()
{
int T, i, a, b; cin >> T; while(T--)
{
cin >> n;
flag = ;
memset(Tree, , sizeof(Tree));
memset(vis, , sizeof(vis));
for(i=; i<=n; i++)
cin >> Value[i]; for(i=; i<n; i++)
{
cin >> a >> b;
Tree[a][b] = ;
Tree[b][a] = 1;//谢谢提醒!
} DFS();
if(flag)
printf("No\n");
else
printf("Yes\n");
}
return ;
}