题目描述

给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形。同时还支持单点修改。

输入

第一行两个整数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

输出

对每个询问输出一行表示答案,“Y”表示有解,“N”表示无解。

样例输入

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


题解

朴素LCA+暴力

一开始想到了一个$O(n\log^3n)$的数据结构算法,然后发现自己太naive了= =

由于点权是int范围内的,所以如果想让尽量多的边不构成三角形,那么它们的边权应该为1、1、2、3、5、8、...

这显然是斐波那契数列,而斐波那契数列是指数增长的,到第50项左右就爆int了。

所以可以直接拿出两个点之间的路径,当拿出的超过50个时直接判定能构成三角形,否则排序,暴力。

时间复杂度为$O(q·50·\log 50)$

#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)
{
to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
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;
}

最新文章

  1. list&lt;T&gt; 的使用方法。
  2. ExtJS基础知识总结:自定义日历和ComboBox控件(二)
  3. nodejs复习01
  4. C# DataSet
  5. 面向对象(五)super
  6. WPF制作的小型笔记本-仿有道云笔记
  7. 使用iScroll时,input等不能输入内容的解决方法(share)
  8. supersr--九宫格公式(根据多少行多少列排版)
  9. 021QTP之焦点(多思考)
  10. Android RecyclerView使用(一)
  11. ssh框架简单搭建
  12. 感觉tbceditor很不错,如果作者能坚持下来,非常非常看好啊
  13. WebCollector 2.x 新手教程
  14. Nginx 和 IIS 实现动静分离【转载】
  15. Java对象与类中的一个小练习
  16. [国嵌攻略][117][LED驱动程序设计]
  17. pwnable.tw dubblesort
  18. 关于解决Mac使用docker安装SQL server for Linux 中文乱码问题
  19. SQL Server初探
  20. 禁止页面内按F5键进行刷新(扩展知识:禁止复制信息内容)

热门文章

  1. BZOJ 4881: [Lydsy2017年5月月赛]线段游戏
  2. UVA 12905 Volume of Revolution (几何,微积分)
  3. Spark集群任务提交
  4. PHP程序Laravel框架的优化技巧
  5. matlab启动
  6. Java Marker Interface
  7. stm32F042 (二) 按键触发中断
  8. iOS 证书、真机调试、发布 App Store
  9. iOS应用架构谈part3 网络层设计方案
  10. jquery源码学习第一天