bzoj3251 树上三角形
来源:互联网 发布:北京云计算招聘岗位 编辑:程序博客网 时间:2024/05/07 07:31
Description
给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形。同时还支持单点修改。
Input
第一行两个整数n、q表示树的点数和操作数
第二行n个整数表示n个点的点权
以下n-1行,每行2个整数a、b,表示a是b的父亲(以1为根的情况下)
以下q行,每行3个整数t、a、b
若t=0,则询问(a,b)
若t=1,则将点a的点权修改为b
Output
对每个询问输出一行表示答案,“Y”表示有解,“N”表示无解。
Sample Input
5 51 2 3 4 51 22 33 41 50 1 30 4 51 1 40 2 50 2 3
Sample Output
NYYN
HINT
对于100%的数据,n,q<=100000,点权范围[1,2^31-1]
Key To Problem
这道题实在脑洞好大,如果建树判断一条树上是否能构成三角形,那显然是不行的,所以要先判断在一定的数中最多有哪些数可以构成三角形,那一定就是斐波那契数列,int范围内的斐波那契数一共是46个,所以只要一串数超过46个就一定可以构成三角形,如果不到46个,那就将这串数排序查询,之后就是裸的LCA
Code
#include<vector>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#define N 100010#define M 46using namespace std;typedef long long ll;int n,m,tot;int a[N];int head[N];int tail[N];int next[N];int dfn[N];int fa[N];int Q[50];int cmp(const void *x,const void *y){ return *(int *)x-*(int *)y;}void add(int x,int y){next[++tot]=head[x];head[x]=tot;tail[tot]=y;}void dfs(int u,int deep){ dfn[u]=deep; for(int i=head[u];i;i=next[i]) { int to=tail[i]; dfs(to,deep+1); }}bool LCA(int x,int y){ int top=0; if(dfn[x]<dfn[y]) swap(x,y); while(top<=M&&dfn[x]>dfn[y]) Q[++top]=a[x],x=fa[x]; if(top>M) return true; while(top<=M&&x!=y) Q[++top]=a[x],Q[++top]=a[y],x=fa[x],y=fa[y]; Q[++top]=a[x]; if(top>M) return true; qsort(Q+1,top,sizeof(Q[0]),cmp); for(int i=3;i<=top;i++) { if(Q[i]<(ll)Q[i-1]+Q[i-2]) return true; } return false;}int main(){// freopen("tree.in","r",stdin);// freopen("tree.out","w",stdout); cin>>n>>m; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); fa[y]=x; add(x,y); } dfs(1,1); for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); if(x==1) a[y]=z; else { if(LCA(y,z)) puts("Y"); else puts("N"); } } return 0;}
0 0
- 【bzoj3251】树上三角形
- bzoj3251 树上三角形
- 【BZOJ3251】树上三角形
- 【bzoj3251】【树上三角形】【暴力】
- BZOJ3251 树上三角形
- 树上三角形
- 树上三角形
- Qual_C:树上的三角形
- 练习 树上的三角形
- 树上的三角形
- BZOJ 3251: 树上三角形
- 树上三角形 tri
- JZOJ4090. 树上三角形
- 3251: 树上三角形
- Qual_C:树上的三角形
- BZOJ 3251 树上三角形 暴力
- BZOJ 3251 树上三角形 数学
- 【BZOJ 3251】树上三角形 暴力
- 《剑指offer》重建二叉树
- IOS通过runtime给category添加属性
- 梧桐树下
- HDU 5437 Alisha’s Party
- ImageLoader的基本使用方法
- bzoj3251 树上三角形
- handler机制的原理
- HDU5446(2015年长春网络赛1010)改
- HDU 5438 Ponds (2015年长春赛区网络赛B题)
- STL之空间配置器allocator
- 使用字节流的方式实现文件的复制
- mongodb查询日期
- 十个问题带你了解 Windows Docker
- 大龄程序员