# 【bzoj3251】树上三角形 朴素LCA+暴力

5 5
1 2 3 4 5
1 2
2 3
3 4
1 5
0 1 3
0 4 5
1 1 4
0 2 5
0 2 3

N
Y
Y
N

#include <cstdio>
#include <algorithm>
#define N 100010
using namespace std;
int w[N] , head[N] , to[N << 1] , next[N << 1] , cnt , fa[N] , deep[N] , a[100] , tot;
void add(int x , int y)
{
}
void dfs(int x)
{
int i;
for(i = head[x] ; i ; i = next[i])
if(to[i] != fa[x])
fa[to[i]] = x , deep[to[i]] = deep[x] + 1 , dfs(to[i]);
}
bool judge(int x , int y)
{
int i;
tot = 0;
if(deep[x] < deep[y]) swap(x , y);
while(deep[x] > deep[y])
{
a[++tot] = w[x] , x = fa[x];
if(tot > 50) return 1;
}
while(x != y)
{
a[++tot] = w[x] , a[++tot] = w[y] , x = fa[x] , y = fa[y];
if(tot > 50) return 1;
}
a[++tot] = w[x] , sort(a + 1 , a + tot + 1);
for(i = 3 ; i <= tot ; i ++ )
if(a[i] - a[i - 1] < a[i - 2])
return 1;
return 0;
}
int main()
{
int n , m , i , opt , x , y;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &w[i]);
for(i = 1 ; i < n ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
dfs(1);
while(m -- )
{
scanf("%d%d%d" , &opt , &x , &y);
if(opt) w[x] = y;
else if(judge(x , y)) puts("Y");
else puts("N");
}
return 0;
}


posted @ 2017-07-16 11:09  GXZlegend  阅读(223)  评论(0编辑  收藏