# BZOJ3251: 树上三角形

BZOJ3251: 树上三角形

## Input

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

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

bool solve(int x,int y){
int lca=LCA(x,y);
int top=0,num[100010];
num[++top]=val[lca];
for(int i=x;i!=lca;i=fa[i])num[++top]=val[i];
for(int i=y;i!=lca;i=fa[i])num[++top]=val[i];
sort(num+1,num+top+1);
if(top<3)return false;
for(int i=3;i<=top;i++)if(num[i]<num[i-1]+num[i-2])return true;
return false;
}


if(deep[x]+deep[y]-2*deep[lca]>=50)return true;


#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 100010
using namespace std;
int n,m,c=1;
struct node{
int next,to;
}a[MAXN<<1];
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
}
void dfs1(int rt){
son[rt]=0;size[rt]=1;
int will=a[i].to;
if(!deep[will]){
deep[will]=deep[rt]+1;
fa[will]=rt;
dfs1(will);
size[rt]+=size[will];
if(size[son[rt]]<size[will])son[rt]=will;
}
}
}
void dfs2(int rt,int f){
top[rt]=f;
if(son[rt])dfs2(son[rt],f);
int will=a[i].to;
if(will!=fa[rt]&&will!=son[rt])
dfs2(will,will);
}
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
return x;
}
bool solve(int x,int y){
int lca=LCA(x,y);
if(deep[x]+deep[y]-2*deep[lca]>=50)return true;
int top=0,num[55];
num[++top]=val[lca];
for(int i=x;i!=lca;i=fa[i])num[++top]=val[i];
for(int i=y;i!=lca;i=fa[i])num[++top]=val[i];
sort(num+1,num+top+1);
if(top<3)return false;
for(int i=3;i<=top;i++)if(num[i]-num[i-1]<num[i-2])return true;
return false;
}
void work(){
int f,x,y;
while(m--){
if(f==0){
if(solve(x,y))printf("Y\n");
else printf("N\n");
}
if(f==1)val[x]=y;
}
}
void init(){
int x,y;
for(int i=1;i<n;i++){
}
deep[1]=1;
dfs1(1);
dfs2(1,1);
}
int main(){
init();
work();
return 0;
}


posted @ 2018-05-27 19:47  符拉迪沃斯托克  阅读(123)  评论(0编辑  收藏
Live2D