# bzoj3251 树上三角形

Input

n,q<=100000，点权范围[1,2^31-1]

Output

Sample Input

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

Sample Output

N
Y
Y
N

ps.我也不不知道为啥用1ll就挂了，以后还是不要用了把......

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int fa[210000],d[210000],a[210000],dep[210000];
vector<int>v[210000];
void dfs(int x){
int i;
for(i=0;i<v[x].size();i++)
if(v[x][i]!=fa[x]){
fa[v[x][i]]=x;
dep[v[x][i]]=dep[x]+1;
dfs(v[x][i]);
}
return;
}
int main()
{      int n,m,i,j,k,x,y;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%d",&d[i]);
for(i=1;i<n;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
dep[1]=1;
dfs(1);
for(i=1;i<=m;i++){
scanf("%d%d%d",&k,&x,&y);
if(k)d[x]=y;
else {
int cnt=0,ok=0,wh=1;
if(dep[x]<dep[y])swap(x,y);
while(dep[x]>dep[y]){
a[++cnt]=d[x];
x=fa[x];
if(cnt>=50){ok=1;break;}
}
while(x!=y){
a[++cnt]=d[x];
a[++cnt]=d[y];
x=fa[x];
y=fa[y];
if(cnt>=50){ok=1;break;}
}
a[++cnt]=d[x];
if(cnt>=50)ok=1;
if(!ok){
sort(a+1,a+cnt+1);
for(j=1;j<cnt-1;j++)
if((long long)a[j]+a[j+1]>a[j+2]){
ok=1;
break;
}
}
if(!ok)printf("N\n");
else printf("Y\n");
}
}
return 0;
}
posted @ 2018-06-04 14:39  水题收割者  阅读(122)  评论(0编辑  收藏